Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Repository pattern an overkill

I have been using Repository pattern (DDD and POEAA) for some time. However some of our team members have argued that it is just an extra layer of abstraction and unnecessary. I can seen some benefit in their arguments. Modern ORM solutions (NHibernate or EF) have almost everything you need. I searched and found some article like this and counterargument on this topic. So Is repository pattern an overkill?

like image 269
Tinku Avatar asked Aug 04 '10 11:08

Tinku


People also ask

Should you use Repository pattern?

The Repository pattern makes it easier to test your application logic. The Repository pattern allows you to easily test your application with unit tests. Remember that unit tests only test your code, not infrastructure, so the repository abstractions make it easier to achieve that goal.

What problem does repository pattern solve?

The Repository Pattern is used to simplify the Application Layer and is defined by the Application Layer . The repositories evolve with the Business Use Cases . The Repository handles domain objects and is agnostic of the technical details. Generics can still be used to implement the repositories.

Why should you use a repository pattern for your project?

The benefit of using The Repository Pattern in this instance is that you can write the Repository interface at the beginning of the project without really thinking about the actual technical details of how you are going to store your data.

Should I use Repository pattern laravel?

A key benefit of the Repository pattern is that it allows us to use the Principle of Dependency Inversion (or code to abstractions, not concretions). This makes our code more robust to changes, such as if a decision was made later on to switch to a data source that isn't supported by Eloquent.


2 Answers

It depends, mostly on the complexity of your problem and the role your Domain Model plays in the solution. For simple solutions, Repository is probably overkill. But for complex domains with a robust language and evolving needs/requirements, the Repository is a nice, clean abstraction that owns the domain object lifecycle. Many ORMs will do much of this, but, in a complex domain, there will always be some domain activity that makes sense in a repository and which is not supported by an ORM out of the box.

Bottom line: it depends on the context.

like image 123
Brandon Satrom Avatar answered Oct 13 '22 20:10

Brandon Satrom


Mocking data access in unit tests is the main reason I use repository interfaces. Another reason maintainability - you can easily implement caching strategies, or switch to other data access implementation, like getting data from the service instead of DB.

like image 30
Kimi Avatar answered Oct 13 '22 21:10

Kimi