存储库模式 - POCO 还是 IQueryable? [英] Repository Pattern - POCOs or IQueryable?

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

问题描述

我是存储库模式的新手,在网上阅读了大量内容后,我对正在发生的事情有一个粗略的了解,但似乎存在一些想法冲突.

I'm new to the Repository Pattern and after doing a lot of reading on the web I have a rough understanding of what is going on, but there seems to be a conflict of ideas.

一个是 IRepository 应该返回的内容.

One is what the IRepository should return.

我只想处理 Pocos,因此我将为每个聚合根提供一个 IRepository 实现,如下所示:

I would like to deal in ONLY Pocos so I would have an IRepository implementation for every aggregate root, like so:

public class OrangeRepository: IOrangeRepository
{
  public Orange GetOrange(IOrangeCriteria criteria);
}

其中 IOrangeCriteria 需要一些特定于查找橙色的参数.

where IOrangeCriteria takes a number of arguments specific to finding an Orange.

我拥有的另一件事是许多数据后端 - 这就是我首先进入这种模式的原因.我想我会为每一个实现,例如

The other thing I have is a number of data back-ends - this is why I got into this pattern in the first place. I imagine I will have an implementation for each, e.g

OrangeRepositoryOracle、OrangeRepositorySQL、OrangeRepositoryMock 等

OrangeRepositoryOracle, OrangeRepositorySQL, OrangeRepositoryMock etc

我想让它保持打开状态,以便我可以使用 EF 或 NHibernate - 如果我的 IOrangeRepository 处理 POCO,那么我将通过实现 OrangeRepositoryNHibernate 等将其封装在存储库本身中.

I would like to keep it open so that I could use EF or NHibernate - again if my IOrangeRepository deals in POCOs then I would encapsulate this within the Repository itself, by implementing a OrangeRepositoryNHibernate etc.

我在正确的路线上吗?

谢谢

感谢您的反馈,我目前没有其他人可以反对这些想法,因此不胜感激!

Thanks for the feedback, I don't have anyone else to bounce these ideas off at the moment so it is appreciated!

推荐答案

是的,您的版本是最安全/最兼容的版本.您仍然可以将它用于任何资源,不仅是数据访问资源,还可以用于网络服务、文件等.

Yes, your version is the safest / most compatible one. You can still use it with about any resources, not only data access ones, but with web services, files, whatever.

请注意,使用 IQueryable 版本,您仍然可以根据 POCO 类工作,但您已绑定到 IQueryable.还要考虑到您可能拥有使用 IQueryable 的代码,然后结果证明您遇到了存储库的 ORM 之一不能很好处理它的情况.

Note that with the IQueryable version you still get to work based on your POCOs classes, but you are tied to the IQueryable. Also consider that you could be having code that uses the IQueryable and then turns out it you hit a case where one of the repository's ORM doesn't handle it well.

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

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