Repository pattern vs. "smart" business objects

Viewed 6397

I see two main "schools of thoughts" when it comes to creating larger-scale enterprise-wide apps on .NET (Winforms, WPF, ASP.NET).

Some folks use the "repository pattern" which uses a repository that knows how to fetch, insert, update and delete objects. Those objects are rather "dumb" in that they don't necessarily contain a whole lot of logic - e.g. they're more or less data-transfer objects.

The other camp uses what I call "smart" business objects that know how to load themselves, and they typically have a Save(), possibly Update() or even Delete() method. Here you really don't need any repository - the objects themselves know how to load and save themselves.

Big question is: which do you use or prefer? And why?

Do you use the same approach in all your apps, or do you have any particular criteria when to choose one approach over the other? If so - what are those criteria?

I'm not trying to start a flame-war here - just trying to find out what everyone thinks about this and what your opinion is, and why you use one (or both) patterns over the other.

Thanks for any constructive input!

5 Answers

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.

Related