0

I have seen in many MVC project concept of Repository but i dont know why many mvc developers use repository.

What is the advantage of creating repository ?

What is the use of repository?

If it improves performance then how it improves performance?

Are there any guidelines to create repository??

Maria Pithia
  • 35
  • 1
  • 7

3 Answers3

4

The concept of repositories is referred to DDD.

It is best to read the book about DDD by Eric Evans.

In short, the repository allows you to hide details of loading objects from a database. This is especially useful for complex composite objects.

Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29
  • does it increase performance r it is just to seperate data access layer from business logic??? – Maria Pithia Dec 24 '14 at 08:41
  • the link you have given does not give any explaination regarding repository – Maria Pithia Dec 24 '14 at 08:49
  • Repositories is just one of concepts of domain-driven design. I recommend you to read the book. As a quick start you can read this [article](http://lostechies.com/jimmybogard/2009/09/03/ddd-repository-implementation-patterns/). – Mark Shevchenko Dec 24 '14 at 09:06
  • take a look at this link:http://stackoverflow.com/questions/10925257/best-repository-pattern-for-asp-net-mvc?rq=1 – Maria Pithia Dec 24 '14 at 09:26
  • The author refers to Microsoft documentation about repositories, but his examples are not repositories. Or, maybe, the term repository has too many meanings. If your domain classes are well-mapped to relational tables, just use Entity Framework. If not, you'll need to construct each composite object (Evans calls them aggregate roots) from several tables and maybe from several databases even. Hide this construction in repository. If you are using SQL, but think about NoSQL in future you fix the interface of "getting data from somewhere" in your repositories. – Mark Shevchenko Dec 24 '14 at 11:04
  • You may take a look at this implementation example: https://stackoverflow.com/a/10616188/7389293 – carloswm85 Aug 01 '22 at 15:34
1
  1. Advantage of using Reository : Clean code, strongly typed view.
  2. Use of Repository: Repository class may expose DB Table structure or it may be view model that can be passed to view to render data.
  3. No performance gain.
  4. Guidelines

    i. Create repository to represent table in database, columns will be properties of that repository.

    ii. Create repository which to bind strongly typed view. Its properties will be all attribute you want to display using view.

Imad
  • 7,126
  • 12
  • 55
  • 112
0

@lmad, what you described in you guideline is not repository - it's just a view model in context of MVVM pattern. Repository is a pattern for data access logic, where you define your own contract for data access. In C#, for instance, once you defined repository interfaces, then distinct implementations of every interface can be easily changed by each other.
@Maria Pithia, it will be enough for you: msdn - repository description. Hope, this will help.

Ryan
  • 609
  • 6
  • 13