简体   繁体   English

MVC 4具有存储库和工作单元模式

[英]MVC 4 with repository and unit of work pattern

I knew that in mvc it is good to use repository pattern and unit of work class as design pattern in ASP.net MVC project.(Business logic in service layer.) 我知道在mvc中使用存储库模式和工作单元类作为ASP.net MVC项目中的设计模式是很好的。(服务层中的业务逻辑。)

My question is, I am new to MVC 4 introduce by Microsoft.can I apply above mention pattern(repository and unit of work) in MVC 4 environment also,Is it good approach to start my project in that design pattern. 我的问题是,我是微软推出MVC 4的新手。我在MVC 4环境中也应用了上述模式(存储库和工作单元),这是在该设计模式中启动项目的好方法。

In MVC do I need to meet another type of pattern for large MVC project. 在MVC中我是否需要满足大型MVC项目的另一种模式。

can anyone know about the MVC 4 please comment on that highly appreciate. 任何人都可以知道MVC 4请评论,高度赞赏。

"in mvc it is good to use repository pattern and unit of work class" - this is highly questionable, while saying things like "considered one of the best pattern used out today" is plain wrong: patterns are only good or bad within some context, without which you can't really tell whether they are good choice or not. “在mvc中,使用存储库模式和工作单元类是很好的” - 这是非常值得怀疑的,虽然说“被认为是今天使用的最佳模式之一”这样的事情是完全错误的:模式在某些情况下只是好的或坏的没有它,你无法确定它们是否是一个好的选择。

Starting with the question "which patterns should I use" without first thinking about the specifics of the application you need to write is a sure-fire way to create an overdesigned application. 从问题“我应该使用哪些模式”开始,而不首先考虑您需要编写的应用程序的细节,这是创建过度设计的应用程序的可靠方法。

Repository and UoW have been popularized by Domain driven design , and they make a lot of sense if you go down that path. 存储库和UoW已经被域驱动设计推广,如果沿着这条路走下去,它们会很有意义。 DDD is excellent approach for some applications, but I would argue that this is not a majority of applications. DDD对于某些应用程序来说是很好的方法,但我认为这不是大多数应用程序。 Many web apps are just CRUD apps which don't benefit from DDD or wrapping every access to entities in a repository - eg choosing a micro-ORM is often a saner approach here. 许多Web应用程序只是CRUD应用程序,它们不会受益于DDD或包装对存储库中实体的每次访问 - 例如,选择微ORM通常是一种更为理智的方法。

If you are still sure that your app really needs such an approach, instead of implementing all this stuff yourself, I suggest using a framework like NHibernate , as you will need more then just UoW and repo - they will require identity map implementation, etc. NHibernate provides a lot of things out of the box (eg its Session type is actually an excellent implementation of UoW plus much more), and covers many details which you will only find a necessity once after you go into details of implementing your own version. 如果您仍然确定您的应用确实需要这样的方法,而不是自己实现所有这些内容,我建议使用像NHibernate这样的框架,因为您需要更多只需要UoW和repo - 它们将需要身份图实现等。 NHibernate提供了很多开箱即用的功能(例如,它的Session类型实际上是UoW的优秀实现以及更多功能),并且涵盖了许多细节,在您详细介绍实现自己的版本后,您将只能找到这些细节。 Entity Framework might also be a good choice here, although it is not my favorite. 实体框架也可能是一个不错的选择,虽然它不是我的最爱。

Of course, writing your own is a great way to extend your knowledge, but if you need this for a production application, you will save a lot of time, money and headaches by picking an existing implementation. 当然,编写自己的知识是扩展知识的好方法,但如果您需要将其用于生产应用程序,则可以通过选择现有实现来节省大量时间,金钱和麻烦。

Just because the Repository pattern is shown in every single Tutorial, doesn't mean its a defacto standard to solve problems in the MVC world. 仅仅因为每个教程中都显示了Repository模式,并不意味着它是解决MVC世界中问题的事实标准。

First off you need to understand the complexity of your application.A Repository may add unneeded complexity to your application.BTW you have not mentioned how are you going to talk to the database.If you have planned to use any of the ORM tools like (Nhibernate,Entity framework),then you already have a good abstraction in place.Why would you need one more level of abstraction. 首先,您需要了解应用程序的复杂性。存储库可能会给您的应用程序增加不必要的复杂性。但是您没有提到如何与数据库通信。如果您计划使用任何ORM工具,例如( Nhibernate,Entity框架),那么你已经有了一个很好的抽象。为什么你需要一个更多的抽象层次。

There is a lot of debate on whether or not to use Repository patterns.Here are some really good explanations 关于是否使用Repository模式存在很多争论。这里有一些非常好的解释

Do we need Repository ? 我们需要存储库吗?

Life without repositories - Ayende 没有知识库的生活 - 艾恩德

All famous ORM tools comes implemented with the patterns that you have mentioned(Unit of work and repositories). 所有着名的ORM工具都使用您提到的模式(工作单元和存储库)实现。

看看在ASP.NET MVC教程中实现ASP.NET MVC应用程序中的存储库和工作单元模式 ,这应该足以让您朝着正确的方向前进。

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

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