查询对象模式与LINQ [英] Query Object Pattern vs LINQ

查看:174
本文介绍了查询对象模式与LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在忙于一个新的项目,正在我的存储库层。这将是我使用存储库模式的第二个项目。我一直在阅读Scott Millett的书籍专业ASP.NET设计模式在引用本书中的案例研究时,Scott在他的存储库界面中使用了查询对象模式。在我以前的项目中,我使用LINQ来实现这一目的,并且运行良好。



我的问题如下:使用查询对象的优缺点是什么?模式实现与您的存储库中使用LINQ



以下是两种方案,说明了两种不同的方法:



1。查询对象模式方法

  public interface IReadOnlyRepository< T,TId>其中T:IAggregateRoot 
{
T FindBy(TId id);
IEnumerable< T>的FindAll();
IEnumerable< T> FindBy(查询查询);
}

2。 LINQ方法

  public interface IReadOnlyRepository< T,TId>其中T:IAggregateRoot 
{
T FindBy(TId id);
IQueryable< T>的FindAll();
IQueryable< T> FindBy(表达式< Func< T,bool>>查询);
}

任何贡献都将有所帮助。

解决方案

虽然我认为LINQ的表达式树只是查询对象模式的一种特殊形式,因为它们具有更大的灵活性,它们允许您在代码中引入错误只在运行时可见。
我正在谈论成功转换为表达式树的C#代码,但供应商不能理解它应该将其转换为SQL。



示例:

  var a = new SpecialObject(); 
yourRepository.FindBy(x => a.IsCorrect(x));

这样的错误对于普通的查询对象是不可能的。


I am busy with a new project and am working on my repository layer. This will be my 2nd project using the Repository Pattern. I have been reading Scott Millett's book Professional ASP.NET Design Patterns In referencing the case study in the book, Scott has used the Query Object Pattern in in his repository interfaces. In my previous project I used LINQ for this purpose and it worked well.

My question is as follows: What are the pros and cons of using a Query Object Pattern implementation versus using LINQ in your repository?

Here are 2 scenarios which illustrate the 2 different approaches:

1. Query Object Pattern approach

public interface IReadOnlyRepository<T, TId> where T : IAggregateRoot
{
    T FindBy(TId id);
    IEnumerable<T> FindAll();
    IEnumerable<T> FindBy(Query query);
}

2. LINQ approach

public interface IReadOnlyRepository<T, TId> where T : IAggregateRoot
{
    T FindBy(TId id);
    IQueryable<T> FindAll();
    IQueryable<T> FindBy(Expression<Func<T, bool>> query);
}

Any contributions would be helpful.

解决方案

Although I think the expression trees of LINQ are just a special form of the query object pattern, because of their greater flexibility, they allow you to introduce bugs in your code that only are visible at runtime. I am talking about C# code that gets successfully converted to an expression tree, but is not understood by the provider that is supposed to translate it to SQL.

Example:

var a = new SpecialObject();
yourRepository.FindBy(x => a.IsCorrect(x));

An error like this would be impossible with an ordinary query object.

这篇关于查询对象模式与LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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