Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository pattern vs. "smart" business objects [closed]

I use the repository pattern because of the Single Responsibility Principle. I don't want each individual object to have to know how to save, update, delete itself, when this can be handled by one single generic repository


The repository pattern doesn't necessary lead to dumb objects. If the objects have no logic outside Save/Update, you're probably doing too much outside the object.

Idealy, you should never use properties to get data from your object, compute things, and put data back in the object. This is a break of encapsulation.

So the objects should not be anemic except if you use simple DTO objects with CRUD operations.

Then separating the persistence concerns from your object concerns is a good way to have Single Responsibility.


Here are two interesting articles I came across

Repository-is-the-new-singleton

The DAL should go all the way to UI


I think the most important side-effect of using the Repository pattern vs. the ActiveRecord pattern is testing and extensibility.

Without having your ActiveRecord object contain a repository itself how would you isolate data retrieval in your test cases? You can't really fake or mock it easily.

Besides testing it would also be much more difficult to swap out data access technologies, for example from Linq-to-SQL to NHibernate or EntityFramework (though this doesn't happen often admittedly).