Filters in DDD Repository

前端 未结 4 1111
遥遥无期
遥遥无期 2021-02-03 12:01

There is Campaign Entity and for that, I have CampaignRepository which have this functions

  1. public IList FindAll();
  2. public Campaign FindByCampaignNumber(st
4条回答
  •  你的背包
    2021-02-03 12:42

    Have you considered implementing Specification pattern in your application? Maybe it looks like an overkill, but it may prove useful if your app will have some complex user filter options.

    class CampaignSpecification
    {
        public CampaignSpecification Number(string number);
        public CampaignSpecification DateBetween(DateTime from, date to);
        public CampaignSpecification Year(DateTime year);
    } //I have omitted all the AND/OR stuff it can be easily implemented with any SQL like query language
    

    Here is an example how loading from the repository may look like

    var  campaignList = CampaignRepository.load(
                new CampaignSpec()
                    .Number("2")
                    .Year(DateTime.Now);
    

    Also I'd like to add that it depends much on what kind of data access solution you are using, it makes implementing easier when you know what kind of API you will be using(Criteria API, SQL or whatever) so you can tweak your Specification interface to make its implementation simpler.

    UPDATE: if you are implementing specifications in .NET using linq and nHibernate please check out http://linqspecs.codeplex.com/

提交回复
热议问题