且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DDD存储库中的过滤器

更新时间:2023-02-13 11:06:37

您是否考虑过实施规范一个>模式在您的应用程序?也许看似矫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/