2

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.)

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.

In MVC do I need to meet another type of pattern for large MVC project.

can anyone know about the MVC 4 please comment on that highly appreciate.

tereško
  • 58,060
  • 25
  • 98
  • 150
Prageeth godage
  • 4,054
  • 3
  • 29
  • 45

3 Answers3

7

"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.

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. DDD is excellent approach for some applications, but I would argue that this is not a majority of applications. Many web apps are just CRUD apps which don't benefit from DDD or wrapping every access to entities in a repository - e.g. choosing a micro-ORM is often a saner approach here.

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 (e.g. 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. 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.

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • 1
    Thank you very much for your valuable comments.but my exact question was that is that design pattern good for MVC 4 environment ? – Prageeth godage Apr 28 '13 at 10:35
  • 2
    Patterns are not good/bad for an environment, They are bad/good for an application. Sure, these patterns can be used very successfully within an ASP.NET MVC application, but they can also be quite a poor choice. Personally, I sometimes use them and sometimes I don't. I have also been burned by overdesigning my applications, when it was not justified by the requirements. – Zdeslav Vojkovic Apr 28 '13 at 11:57
  • 1
    @Prageeth - the real answer to your question. It depends. We know nothing about what you want to build. It could be worth while, it could be poor choice. So it just depends. – Phill Apr 28 '13 at 18:40
5

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.

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.

There is a lot of debate on whether or not to use Repository patterns.Here are some really good explanations

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).

Community
  • 1
  • 1
Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
  • as i mention in my description. they arrange meetings with microsoft while making these tutorials. You should check `Dan Whalin` & `John Papa` background. They are `Microsoft MVP`. It is also recommended by `Scott Guthrie` (during their meeting). – Idrees Khan Apr 28 '13 at 07:08
  • i am not anti repository.I have used it myself and didn't really benefit much.If use a ORM tool you can mock them as well. – Prabhu Murthy Apr 28 '13 at 07:16
  • +1 for promoting thinking before jumping to whatever is the buzzword of the day. – Zdeslav Vojkovic Apr 28 '13 at 07:16
  • 3
    @DotNetDreamer the repository pattern is not the be-all-end-all pattern of data access. Its complete over-kill for small sites. And can add complexity to large sites by making controllers do too much when the controllers should be dumb. Just because an MVP or Scott Gu recommends it, does not mean its the absolute correct and only way to do things. Your answer is miss-leading. – Phill Apr 28 '13 at 07:16
  • 1
    @DotNetDreamer, so you want to say that ScottGu already knows exactly what your app needs? Wow, I thought only Chuck Norris is capable of that. – Zdeslav Vojkovic Apr 28 '13 at 07:17
  • @Phill, if you read the question, `In MVC do I need to meet another type of pattern for large MVC project`. he is asking about large projects – Idrees Khan Apr 28 '13 at 07:19
  • Large doesn't necessarily mean complex, or that it really is a kind of app which will benefit from that approach. – Zdeslav Vojkovic Apr 28 '13 at 07:21
  • @Phill. Nothing is absolute. But for large projects, i have used this and i am telling my experience. Here everyone share their experience. It benefits alot to me. And i'm not just talking about `Repository` pattern, i'm also talking about integrating it with `Unit of Work` pattern. – Idrees Khan Apr 28 '13 at 07:21
  • @DotNetDreamer its fine to share your experiences and suggest the use of the Repository pattern. But I don't see the need to suggest it as an absolute sound way of designing software. If you removed the first and last paragraph of your answer I would have upvoted. – Phill Apr 28 '13 at 07:25
  • Thank you very much for your valuable arguments.but my exact question was that is that design pattern good for MVC 4 environment ?(MVC 4) – Prageeth godage Apr 28 '13 at 10:36
1

Take a look at Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application on the ASP.NET MVC tutorials, this should be enough to get you moving in the right direction.

Kevin Obee
  • 1,489
  • 1
  • 12
  • 22