存储库设计模式 [英] Repository design pattern

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

问题描述

我看到许多仓库模式实现。特别是2种类型


  1. 他们暴露了可查询的通用存储库,并期望从服务类获取数据的lamba表达式


  2. 编写方法以根据业务需求从数据库获取数据,并封装检索数据的逻辑(甚至lambda)。


哪一种是更好的方法?

解决方案

我真的很喜欢第二个。



即使我已经看到.NET世界上的顶级博客的文章,对我来说,可疑的是在一个存储库中的恶意。



原因:


  1. 存储库就像一组对象但存储在其实现定义的任何地方。


  2. 存储库提取数据映射到对象的方式。数据存储可以是任何的,但业务依赖于存储库,以便检索,添加,更新或删除域对象。


  3. 对底层的任何读取或写入访问存储必须由存储库本身或任何其他底层进行管理。


可查询消除了大部分这些点, /或原因。



例如,为什么要设计GetProductByName ,GetProductByCode ,如果可以这样做LINQ on IQueryable?



并且Queryable在n层场景中效果不佳,因为您不会访问另一层中的数据库连接,而不是返回延迟集的数据库连接。 / p>

我不认为可查询的概念和存储库应该适用于任何软件设计,因为它告诉存储库是无用的。



可查询就像设计一个 GetAll 方法。 GetAll在存储库中有什么意义?存储库将检索所有域对象,您将在业务过滤它们。但是...等等...存储库不应该用某些标准来检索域对象,是吗?



尽管我发现可查询与存储库不兼容,设计和实现一些接受lambda表达式或任何委托以获得某种过滤器或行为的存储库方法对我来说是好的。这不是延期执行,它是委托。


I have seen many repository pattern implementations. Specifically of 2 types

  1. They expose the generic repository which is queryable and expects a lamba expression from service class to get data from database.

  2. Write methods to get the data from database based on business requirements and encapsulate the logic (even lambda) of retrieving the data.

Which one is a better approach?

解决方案

I really prefer the second one.

Even I've seen articles from top bloggers on .NET world, queryables, for me, are evil in a repository.

Reasons:

  1. A repository is like a collection of objects but stored wherever its implementation has defined.

  2. A repository abstracts the way data is mapped to object. Data store could be whatever, but business relies on repository in order to retrieve, add, update or remove domain objects.

  3. Any read or write access to the underlying store must be managed by the repository itself - or any other underlying layer -.

Queryable destroys most of these points and/or reasons.

For example, why you would design a GetProductByName, GetProductByCode, if you can do it with LINQ on IQueryable?

And Queryable works bad in n-tier scenarios, since you won't be having access to the database connection in another tier than the one which returned a deferred set.

I don't think "queryable" concept and repository should be good for any software design, because it's telling that repository is useless.

Queryable is like designing a GetAll method. What's the point of a GetAll in a repository? Repository will be retrieving all domain objects and you'll be filtering them in business. But... Wait... Repository isn't supposed to retrieve domain objects with some criteria, is it?

Although I find queryable incompatible with repository, designing and implementing some repository method that accepts a lambda expression or any delegate in order to give some kind of filter or behavior, for me, is fine. This isn't deferred execution, it's delegation.

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

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