DDD存储库中的过滤器 [英] Filters in DDD Repository

查看:76
本文介绍了DDD存储库中的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Campaign实体,为此,我有一个具有此功能的CampaignRepository

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


  1. public IList FindAll();

  2. 公共竞选FindByCampaignNumber(字符串编号);

但是现在我想要这个条件-:

But now i want this criterias -:


  1. 查找今天创建的广告系列。

  2. 查找本月创建的广告系列

  3. 查找最近的5个广告系列。

  4. 查找今年创建的广告系列。

  1. Find campaigns that are created today.
  2. Find campaigns that are created in this month
  3. Find top 5 latest campaigns.
  4. Find campaigns that are created in this year.

因此,对于所有这些广告系列过滤器,

So for all these campaigns filters,

我是否要在存储库中为每个过滤器创建单独的功能?

并以这种方式实施。

Getall广告系列,然后过滤所需的广告系列,但我不希望所有广告系列。在Google中搜索时,我发现此解决方案的

Getall campaigns and then filter required campaigns, but i do not want all campaigns. While searching in google i find this solution's

1: http://russelleast.wordpress.com/2008/09/20/implementing-the-repository-and-finder-patterns/

有什么方法可以避免使用多个函数,还是可以为每个过滤器创建单独的函数?

推荐答案

您是否考虑过实施规范一个>模式在您的应用程序?也许看似矫kill过正,但如果您的应用程序具有一些复杂的用户过滤器选项,则可能会有用。

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);

我还要补充一点,它很大程度上取决于您使用的是哪种数据访问解决方案,当您知道将使用哪种API时,它使实现变得更加容易(标准API, SQL或其他方法),因此您可以调整规范接口以简化实现。

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 :如果要使用.NET实现规范linq和nHibernate,请查看 http://linqspecs.codeplex.com/

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

这篇关于DDD存储库中的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆