views:

1065

answers:

4

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!

+15  A: 

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

John Polling
In that case, one generic repository then needs to know how to load all types of objects.
Peter J
and because it's generic it can handle loading all types of objects. Also makes unit testing simpler as well
John Polling
I also like repository-style because data transfer objects are more flexible. You can use them everywhere, no dependency on frameworks, layers, etc. They represent simply the concept, the model. If you want a bridge between the model and the BD you build persistence layer. You want a bridge between the model and the user you build the UI. You want to calculate things, to implement use cases, you build the business logic. All relates to simply model objects that are not attached to nothing more than the business concept itself.
helios
+6  A: 

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 separing the persistence concerns from your object concerns is a good way to have Single Responsibility.

Think Before Coding
No, sure - my busobj aren't totally "dumb" - I just meant "dumb" in that they don't know about how to load and save themselves - they do have other functionality, obviuosly :-)
marc_s
This is sane design. I personally see "bo knows to load himself" as reason not to hire a developer - e obviously never heard of separation of sresponsibility and never has seen a properly encapsulaed DAL.
TomTom
+1  A: 

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).

joshperry
+1  A: 

Here are two interesting articles that I have come across

Repository-is-the-new-singleton

The DAL should go all the way to UI

ashraf