首先使用 EF4 代码制作存储库类的最佳实践是什么?

发布于 2024-10-13 09:28:51 字数 203 浏览 3 评论 0原文

似乎有很多方法可以应用存储库模式,这就是为什么我需要您的意见以及关于应用存储库模式的最佳方法的清晰答案。它是通用存储库?但是存在一个问题,某些域对象不具有与其他域对象相同的行为。它是指定存储库吗?存储库之间的代码重复怎么样?它是两者的结合吗?对于这两种实现都使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲世九天 2024-10-20 09:28:51

存储库模式本身在那些需要规范的领域非常明确。其余的部分是故意省略的,因为这取决于我的外部因素,例如您的编程语言、项目范围和您的个人编程风格。

换句话说:你似乎在要求一个关于如何应用该模式的模式,但最终在编程时你将不得不做出自己的决定。

不过,我将尝试回答您更具体的问题:

  • 是的,使用泛型对于存储库来说是一个好主意。另一种方法是为所有应存储的对象提供一个公共接口。您也可以同时选择两者,具体取决于您的要求。
  • 尝试通过调用要存储的对象的多态方法来处理对象行为的差异。
  • 如果你做得正确(通过使用泛型和/或接口),就不应该有代码重复。

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:

  • Yes, using Generics is a good idea for a repository. Another approach would be to have a common interface for all objects that should be stored. You might also go with both, depending on your requirements.
  • Try to deal with differences in object behaviour by calling polymorphic methods on the objects to be stored.
  • There should be no code repetition if you do it right (by using generics and / or interfaces).
枕花眠 2024-10-20 09:28:51

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文