简体   繁体   English

EF4 和 MVC3 - 我需要存储库模式吗?

[英]EF4 with MVC3 - Do I need The Repository Pattern?

I have recently learned of the Repository and Unit of Work Design Patterns and thought that I would implement them in a new EF4 MVC3 project, since abstraction is generally good.我最近了解了存储库和工作单元设计模式,并认为我会在一个新的 EF4 MVC3 项目中实现它们,因为抽象通常是好的。

As I add them to the project, I am wondering if the juice is worth the proverbial squeeze, given the following:当我将它们添加到项目中时,我想知道果汁是否值得众所周知的挤压,鉴于以下情况:

  • It is EXTREMELY unlikely that the underlying data access mechanism will change from EF4.底层数据访问机制极不可能从 EF4 更改。
  • This level of abstraction will require more overhead/confusion to the project and to other developers on the team.这种抽象级别将需要更多的开销/混淆项目和团队中的其他开发人员。

The only real benefit I see to using the Repository pattern is for unit testing the application.我看到使用存储库模式的唯一真正好处是对应用程序进行单元测试。 Abstracting away the data store doesn't seem useful since I know the datastore won't change, and further, that EF4 already provides a pretty good abstraction (I just call.AddObject() and it looks like I am modifying an in-memory collection and I just call.SaveChanges() which already provides the unit of work pattern).抽象出数据存储似乎没有用,因为我知道数据存储不会改变,而且,EF4 已经提供了一个非常好的抽象(我只是调用.AddObject(),看起来我正在修改一个内存集合,我只是调用.SaveChanges(),它已经提供了工作单元模式)。

Should I even bother implementing this abstraction?我什至应该费心实现这个抽象吗? I feel like there must be some massive benefit that I am missing, but it just doesn't feel like I need to go down this route.我觉得一定有一些我错过的巨大好处,但我觉得我不需要沿着这条路线走 go。 I am willing to be convinced otherwise;我愿意被说服,否则; can someone make a case?有人可以提出案子吗? Thanks.谢谢。

I recommend you reading this answer and all linked questions .我建议您阅读此答案和所有相关问题 The repository is very popular pattern and it really makes your application nice and clean.存储库是非常流行的模式,它确实使您的应用程序变得漂亮和干净。 It make you feel that your architecture is correct but some assumptions about repository pattern with EF are not correct.它让您觉得您的架构是正确的,但是关于使用 EF 的存储库模式的一些假设是不正确的。 In my opinion (described in those answers):在我看来(在那些答案中描述):

  • It will make some more complex EF related task much harder to achieve or your repository and UoW implementation will need to have public interface very similar to EF's这将使一些更复杂的 EF 相关任务更难实现,或者您的存储库和 UoW 实现将需要具有与 EF 非常相似的公共接口
  • It will not make your code better unit testable because all interactions with repository must still be covered by integration tests.它不会使您的代码更好地进行单元测试,因为与存储库的所有交互仍必须由集成测试覆盖。 Not only my experience proved that mocking EF code by replacing linq-to-entities with linq-to-objects does not test your code.我的经验不仅证明了 mocking EF 代码通过将 linq-to-entities 替换为 linq-to-objects 并不会测试您的代码。

yes yes yes: ) - first of all - the repository pattern helps to inject your dependencies for unit testing.是的,是的:) - 首先 - 存储库模式有助于注入您的依赖项以进行单元测试。 Secondly, it gives a very clear view of exactly what data access methods are available to get something rather than people misc.其次,它可以非常清楚地了解哪些数据访问方法可用于获取某些东西,而不是人们的杂项。 coding against the EF layer directly.直接针对 EF 层进行编码。 Download the POCO templates though for EF4 so your classes don't carry the EF properties around with them if you happen to use them as models and/or don't want any EF dependency libraries references in your mvc app assuming your repository work is in a separate project (which I recommend).尽管为 EF4 下载 POCO 模板,因此如果您碰巧将它们用作模型和/或不希望在您的 mvc 应用程序中引用任何 EF 依赖库引用,则您的类不会随身携带 EF 属性(假设您的存储库工作在)一个单独的项目(我推荐)。 If you are using all viewmodels then its not as much of a concern, but its nice working with a "Customer" object without extra methods on them.如果您正在使用所有视图模型,那么它就不是那么重要了,但是它可以很好地与“客户”object 一起工作,而无需额外的方法。 Its cleaner in my opinion.我认为它更清洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM