Tuesday, August 30, 2011

What I did today!

Today:
  1. Today Kar-Khan pointed out to me the my get/setter-based approach to jumping through a bunch of hoops immediately upon model binding which I crafted here, refined here, and then had Joel refine here, is just goofy. It is better to just have plain Jane get/setters and then call a method to do more complicated things with them beyond get and set. It didn’t want to do this because it entails putting an extra line of code in the Controller, but boy is that ever the lesser of the two evils compared to the mess I've been making.
  2. I added the following to our Base Class for Repositories:

    public virtual IQueryable<T> FindFilteredSet(List<Specification<T>> specifications)

    {

       Specification<T> currentSpecification =

             new AdHocSpecification<T>(t => t != null);

       IQueryable<T> results =

             Session.Query<T>().Where(currentSpecification.IsSatisfiedBy());

       foreach(Specification<T> specification in specifications)

       {

          IQueryable<T> concatenatedResults =

                Session.Query<T>().FindAll(specification).FindAll(currentSpecification);

          currentSpecification = specification;

          results = concatenatedResults;

       }

       return results;

    }

     
     

    Joel refactored my work like so:

    public virtual IQueryable<T> FindFilteredSet(List<Specification<T>> specifications)

    {

       var query = Session.Query<T>();

       specifications.ForEach(spec => query.Where(spec.IsSatisfiedBy()));

       return query;

    }

     
     

    Joel told me that foreach is old school and that I should try to use Select instead where applicable although it is not applicable in this case.
  3. We are taking the approach above in lieu of chaining specs in the name of a cleaner process outside of logic nested in Controllers which may also be encapsulated to play nicely with a comparable means devised for filtering based upon custom attributes. (I am building specifications for filtering on the static parameters of objects and the static parameters of their children in this example.)

No comments:

Post a Comment