首先使用 EF4 代码制作存储库类的最佳实践是什么?
似乎有很多方法可以应用存储库模式,这就是为什么我需要您的意见以及关于应用存储库模式的最佳方法的清晰答案。它是通用存储库?但是存在一个问题,某些域对象不具有与其他域对象相同的行为。它是指定存储库吗?存储库之间的代码重复怎么样?它是两者的结合吗?对于这两种实现都使用 di 容器怎么样?
谢谢
编辑:我使用的 orm 是实体框架 4。如果有 EF4 的示例就好了。
There seem to be many ways to apply the repository patterns that is why i need your opinion and a good clear answer on what seem the best way of applying the repository pattern. It is the generic repository ? But there is a problem where some domain object dont have the same behaviour as others. It is specify repository ? What about code repeatition between repositories? it is both combinaison ? how about using a di container for both kind of implementation ?
Thanks
Edit: the orm i am using is entity framework 4. Would be nice to have an example with EF4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
存储库模式本身在那些需要规范的领域非常明确。其余的部分是故意省略的,因为这取决于我的外部因素,例如您的编程语言、项目范围和您的个人编程风格。
换句话说:你似乎在要求一个关于如何应用该模式的模式,但最终在编程时你将不得不做出自己的决定。
不过,我将尝试回答您更具体的问题:
The Repository pattern itself is pretty explicit in those areas that require specification. The rest is left out on purpose, because it depends on my outside factors, such as your programming language, project scope and your personal programming style.
In other words: You seem to be asking for a pattern on how to apply the pattern, but in the end you will have to take your own decisions when programming.
I will try to reply to your more specific questions though:
Gabriel Schenker 的这篇文章很好地解释了存储库模式。他用 NHibernate 实现它(你没有说你正在使用哪个 ORM)并提供了一个通用的基础存储库。对我来说,存储库模式与测试以及能够伪造或模拟它密切相关,这篇博客文章还展示了如何制作自己的伪造品。
根据我的经验,大多数存储库都共享一些常见的功能,例如 GetById、Add、Remove。所以拥有一个可以继承的基类是很有用的。当您开始考虑动态 Linq 查询以及如何测试这些查询时,事情会变得有点棘手。
使用 Linq 的存储库模式的示例可以在 此处 和 这里 但我会从简单开始,因为这些对于某些项目来说可能有点过头了。
This post by Gabriel Schenker is a good explanation of the repository pattern. He implements it with NHibernate (you don't say which ORM you are using) and provides a generic base repository. For me the repository pattern is very connected to testing and being able to fake it or mock it and this blog post also shows how to make your own fake.
In my experience most repositories share some common functionality e.g. GetById, Add, Remove. So it is useful to have a base class that can be inherited. Where it can get a little trickier is when you start thinking about dynamic Linq queries and how those can be tested.
Examples of repository patterns with Linq can be found here and here but I would start simple as these could be overkill for some projects.