11

Being fairly new to Asp.Net MVC I am following the SportsStore example from the book Pro ASP.Net MVC 3 Framework.

All is going fine, but now I start to enhance the base example with some extra entities. Which is the better approach: - should each entity have its own repository (which would seem to duplicate code) or - should there be a generic repository for similar entities?

Are there any other projects out there that the same architecture are more complete samples?

Josh Mein
  • 28,107
  • 15
  • 76
  • 87
simon831
  • 5,166
  • 7
  • 33
  • 50
  • Take look at this http://stackoverflow.com/a/1231473/105445 – Wahid Bitar Jun 18 '12 at 16:26
  • 1
    possible duplicate of [Advantage of creating a generic repository vs. specific repository for each object?](http://stackoverflow.com/questions/1230571/advantage-of-creating-a-generic-repository-vs-specific-repository-for-each-obje) – FoamyGuy Jun 18 '12 at 20:51
  • Seen questions like this more than once. – Danny Varod Jun 19 '12 at 09:14
  • 1
    I think a lot of mvc projects are over designed or too ideal, from my limited experience, business logic are far more complex than CURD for each entity, entities are connected closely. In the end, how much we can gain from these fancy patterns is a question mark. – Timeless Apr 22 '15 at 14:47

1 Answers1

3

This is something that is heavily debated on the internet. If you use the search here at SO you can find plenty of threads about it. In the end is just comes down to your personal preference. Try both options and decide which one is best for you.

I started creating separate repositories for every entity, but it felt like loads of unnecessary work (and lots of code duplication), so recently I started using generic repositories, and that's working out perfectly for me.

See this thread for more information: Advantage of creating a generic repository vs. specific repository for each object?

Community
  • 1
  • 1
Leon Cullens
  • 12,276
  • 10
  • 51
  • 85
  • With separate repositories, how do you join between entities or do aggregations across joins? – usr Jun 18 '12 at 17:48
  • 1
    You can just do the joins in your repository if you like. So for instance: if you have a UserRepository and BlogPostRepository, you could create a method 'GetBlogPostsByUserId' in your BlogPostRepository, and join the 'User' and 'BlogPost' tables in that method. But that's just one way to do it, there are lots of other ways. – Leon Cullens Jun 18 '12 at 17:51
  • 1
    You may use UnitOfWork as well – Wahid Bitar Jun 18 '12 at 18:08