Monday, August 8, 2011

Why am I getting a null-equals-false error? Why do I have to account for it?

Experimenting with AdHocSpecification<T> like so:

var foo = FooRepository.FindAll(ObjectSpecs.MyCriteria(filter) && ObjectSpecs.MyOtherCriteria(filter)).ToPagedList(bar, baz);

 
MyCriteria looks like so:

public static Specification<Foo> MyCriteria(Filter filter)
{

   if (filter.IsOk != null)

   {

      var spec = new AdHocSpecification<Foo>(p => p.IsGood == filter.IsOk);

      return spec;

   } else {

      var spec = new AdHocSpecification<Foo>(p => p != null);

      return spec;

   }

}

 
...and I am having to write some logic to keep false from being mistaken as null. Does anyone know why this is? filter.IsOk is a nullable boolean (Boolean?). Maybe the way the repository interfaces with the MSSQL allows for a false-equals-null match. Maybe the issue is of MSSQL itself. Maybe she's born with it. Maybe it's Maybelline. I dunno.

public static Specification<Foo> MyCriteria(Filter filter)

{

   if (filter.IsOk != null)

   {

      if (filter.IsOk == false)

      {

         var spec = new AdHocSpecification<Foo>(p => p.IsGood == false);

         return spec;

      }

      else

      {

         var spec = new AdHocSpecification<Foo>(p => p.IsGood == true);

         return spec;

      }

   } else {

      var spec = new AdHocSpecification<Foo>(p => p != null);

      return spec;

   }

}

 
AdHocSpecification<T> seems pretty cool.

FooRepository is empty...

namespace Whatever

{

   public class FooRepository : BaseObject<Foo, Guid>, IFooRepository

   {
   

   }

}

 
...but it inheirts from:

namespace Whatever

{

   public abstract class BaseObject<T, TId> : HibernateDaoSupport

   where T : BaseEntity<TId>

   {

      

      
methods here...

      

      public IQueryable<T> FindAll(Specification<T> spec)

      {

         return GetQuery(spec);

      }

      

      
more methods here...

      

      private IQueryable<T> GetQuery(Specification<T> spec)

      {

          return Session.Query<T>().Where(spec.IsSatisfiedBy());

      }

No comments:

Post a Comment