Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pros and cons of having static repositories in ASP.NET MVC application

What are the pros and cons of using static repositories in an ASP.NET MVC application?

Wouldn't it be better to have all the methods available all the time -> means that the class get's instantiated only once instead of having multiple controllers referencing to the same repository class and its methods?

Or do I get something wrong here?

All help is more than appreciated!

like image 215
Faizan S. Avatar asked Feb 11 '10 10:02

Faizan S.


People also ask

Why we use repository pattern in MVC?

The repository pattern is intended to create an abstraction layer between the data access layer and the business logic layer of an application. It is a data access pattern that prompts a more loosely coupled approach to data access.

Do we need repository pattern with Entity Framework?

No, the repository/unit-of-work pattern (shortened to Rep/UoW) isn't useful with EF Core. EF Core already implements a Rep/UoW pattern, so layering another Rep/UoW pattern on top of EF Core isn't helpful.

Should repository be a singleton?

Don't use static or singleton repositories because of: It affects testablility, you can not mock it when unit testing. It affects extensibility, you can not make more than one concrete implementation and you can not replace behavior without re-compiling.

Why would you implement a generic repository?

It reduces redundancy of code. It force programmer to work using the same pattern. It creates possibility of less error. If you use this pattern then it is easy to maintain the centralized data access logic.


1 Answers

Pros:

  • Repository is accessible everywhere

Cons:

  • Repositories don't implement a contract which leads to a strong coupling between consumers of the repository and the implementation
  • Impossible to unit test
  • Might run into threading issues

Remark: Instantiating the repository on every request shouldn't be regarded as a performance issue.

like image 102
Darin Dimitrov Avatar answered Oct 06 '22 01:10

Darin Dimitrov