Filters in DDD Repository

前端 未结 4 1106
遥遥无期
遥遥无期 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:26

    I would go with creating two Specifications: TopCampaignSpec and CampaingCreatedSpec.

    var spec = CampaignCreatedSpec.ThisYear();
    var campaigns = CampaignsRepository.FindSatisfying(spec);
    

    CampaingCreatedSpec can also be replaced with more generic DateRange class if you need this functionality elsewhere:

    var thisYear = DateRange.ThisYear();
    var campaigns = CampaignsRepository.FindByDateRange(spec);
    

    I also highly recommend staying away from 'generic' repositories and entities. Please read this

    From DDD perspective it does not matter whether data access code is implemented as SQL/HQL/ICriteria or even web service call. This code belongs to repository implementation (data access layer). This is just a sample:

    public IList FindByDateRange(CampaignCreatedSpec spec) {
        ICriteria c = _nhibernateSession.CreateCriteria(typeof(Campaign));
        c.Add(Restrictions.Between("_creationDate", spec.StartDate, spec.EndDate));
        return c.List();
    }
    

提交回复
热议问题