Warning this post is not well formulated but has some useful random information :)
There has been alot of talk lately about people refactorring from the repository pattern to the Active Record pattern. For instance Sam Gentile brings it up here
How prevalent is this movement? I personally prefer the repository pattern over ActiveRecord for quite a few reasons.
- My db objects and domain objects rarely match up.
- I often use polymorphism on my repositories (especially for testing).
- I can write a generic repository (reusing code for general methods such as Delete or FetchByQuery)
- I can allow repository injection by the domain host.
- A Repository shows explicitly a decision to make the object an aggregate root
- I use alot of AOP and its nice being able to place aspects on my repositories, this could not be done with active record's static methods
- ActiveRecord seems to defeat the entire concept of DDD, ActiveRecord promotes a database centric model
skeleton example of generic repository
public class Repository {
public virtual List FetchByQuery(QueryObject _Query) {
}
public virtual void Save(T _Object) {
}
public virtual void Delete(T _Object) {
}
}
I guess the big question in choosing which way to go is whether or not your object creations varies for a given object. If you only ever have a single repository then perhaps you are probably better off with the static accessors. Personally I prefer the seperation (and the code saving).
Unit Testing
The very concept of a repository is that it gives the impression of an in memory database. This works perfectly for unit testing as I can just use a repository that actually holds all of its data in memory for testing purposes! I can in fact even make a generic based one that can operate on my query objects using reflections.
also .. some interesting links I happenned accross:
http://www.xprogramming.com/xpmag/jatSustainablePace.htm
http://www.joelonsoftware.com/items/2006/04/11.html
http://media.spikedhumor.com//805/zoomquilt.swf very dali'esque