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!
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.
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.
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.
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.
Pros:
Cons:
Remark: Instantiating the repository on every request shouldn't be regarded as a performance issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With