Sunday, December 18, 2011

the current way we aggregate LinqSpecs

This shows how Rafael is aggregating LinqSpec specifications:

using System.Collections.Generic;

using System.Linq;

using OurApp.Core.Entities;

using OurApp.Core.Specs;

using OurApp.Core.Utils;

using OurApp.Core.Utils.LinqSpecifications;

namespace OurApp.Web.UI.LinqSpecifications

{

   public abstract class FilterModelBasedSpecificationBuilderBase<T> :

         IFilterModelBasedSpecificationBuilder<T> where T : BaseEntity

   {

      public IList<IFilterModelBasedSpecificationRule<T>>

            FilterModelBasedSpecificationRules { get; set; }

      public SpecificationBuilder<T> Build(SpecificationBuilder<T> specificationBuilder,

            IFilterModel filterModel)

      {

         FilterModelBasedSpecificationRules

               .Where(rule => rule.ShouldApply(filterModel))

               .ForEach(rule => rule.Apply(filterModel, specificationBuilder));

         if (specificationBuilder.ToSpecification() == null)

               specificationBuilder.AddOr(BaseSpecs<T>.Noop());

         return specificationBuilder;

      }

   }

}

 
 

The class above will be a parent in inheirtance for an object-level concern.

using OurApp.Core.Entities;

namespace OurApp.Web.UI.LinqSpecifications.OrderSpecifications

{

   public class OrderFilterModelBasedSpecificationBuilder :

      FilterModelBasedSpecificationBuilderBase<Order>

   {

   }

}

 
 

The Spring.NET wireup:

<object id="orderSpecificationAggregator"

      type="OurApp.Core.Utils.LinqSpecifications.Aggregators

      .OrderSpecificationAggregator,OurApp.Core" singleton="false">

   <property name="FilterModelBasedSpecificationBuilder"

         ref="orderFilterModelBasedSpecificationBuilder" />

</object>

<object id="orderFilterModelBasedSpecificationBuilder"

      type="OurApp.Web.UI.LinqSpecifications.OrderSpecifications

      .OrderFilterModelBasedSpecificationBuilder,OurApp.Web.UI" singleton="false">

   <property name="FilterModelBasedSpecificationRules">

      <list element-type="OurApp.Core.Utils.LinqSpecifications

            .IFilterModelBasedSpecificationRule<OurApp.Core.Entities.Order>

            ,OurApp.Core">

         <object type="OurApp.Web.UI.LinqSpecifications.OrderSpecifications.

               ApprovedStatusSpecificationRule,OurApp.Web.UI" />

         <object type="OurApp.Web.UI.LinqSpecifications.OrderSpecifications.

               StatusSpecificationRule,OurApp.Web.UI" />

         <object type="OurApp.Web.UI.LinqSpecifications.OrderSpecifications.

               PendingMeSpecificationRule,OurApp.Web.UI" />

         <object type="OurApp.Web.UI.LinqSpecifications.OrderSpecifications.

               PendingMeCostCenterSpecificationRule,OurApp.Web.UI" />

         <object type="OurApp.Web.UI.LinqSpecifications.OrderSpecifications.

               PendingMeAccountSpecificationRule,OurApp.Web.UI" />

         <object type="OurApp.Web.UI.LinqSpecifications.OrderSpecifications.

               RequesterSpecificationRule,OurApp.Web.UI" />

      </list>

   </property>

</object>

 
 

The sanity checking for one of the concerns:

using OurApp.Core.Entities;

using OurApp.Core.Specs;

using OurApp.Core.Utils;

using OurApp.Core.Utils.LinqSpecifications;

using OurApp.Web.UI.Models;

using OurApp.Web.UI.Util;

namespace OurApp.Web.UI.LinqSpecifications.OrderSpecifications

{

   public class ApprovedStatusSpecificationRule :

         IFilterModelBasedSpecificationRule<Order>

   {

      public bool ShouldApply(IFilterModel filterModel)

      {

         var orderFilterModel = (OrderFilterModel)filterModel;

         return orderFilterModel.Status == OrderStatusFilterState.PendingDelivery.ToString();

      }

      public SpecificationBuilder<Order> Apply(IFilterModel filterModel,

            SpecificationBuilder<Order> specificationBuilder)

      {

         specificationBuilder.AddAnd(OrderSpecs.ApprovedStatusCriteria());

         return specificationBuilder;

      }

   }

}

 
 

One of the concerns:

using System.Linq;

using OurApp.Core.Entities;

using OurApp.Core.Infrastructure;

using LinqSpecs;

namespace OurApp.Core.Specs

{

   public class OrderSpecs:BaseSpecs<Order>

   {

      public static Specification<Order> ApprovedStatusCriteria()

      {

         return new AdHocSpecification<Order>(o => o.Status_private

               == (int)OrderStatusCode.Approved);

      }

No comments:

Post a Comment