Home > Castle Project, DDD, ORM, Patterns, Yoot > ActiveRecord & Repository pattern

ActiveRecord & Repository pattern

Hammett has a post where he, among others, advises:

I don’t encourage people to use AR with the AR base class. Use the
ARMediator and use the repository pattern or a dao pattern to increase
isolation and testability.

Yes ! It seems not many people knew it (I did, I'm a Reflector addict lol) but you can use ActiveRecord without base classes for your entities (ActiveRecordBase), just use the [ActiveRecord] attribute.

That way, you get:

  • Attribute-based mapping declaration (instead of manual edition of NHibernate mapping files, which can be cumbersome).
  • Well-tested plumbing code like session & transaction scopes.
  • Easy querying through ActiveRecordMediator static methods, instead of direct use of NHibernate ISession.

Now, if you want the best ORM experience in a DDD-context:

  • Use the Repository pattern, to get an additional layer of abstraction between your application code & your particular ORM.
  • Use Linq queries in your repositories, for rich ORM-independent querying capabilities (your ORM has to support Linq of course).

I've been refactoring & cleaning up some code lately, I will soon drop the first draft of Yoot.DomainDriven, a non-intrusive framework for pragmatic Domain-Driven Design with nice abstraction of Units of work & Linq-friendly repositories, and implementations for mainstream ORM tools. :)

  1. Casey
    May 19th, 2008 at 08:30 | #1

    Indeed few know about this, and it was why I discounted AR recently on a project I wanted to do in a DDD way … because although I had used AR without the base classes way in the distant pastI had totally forgotten you could, and recollected only that a comment on a blog or forum somewhere or other said it was very anti-DDD … I am now wallowing in .hbm files and wishing I had remebered this earlier :)

    Thanks for the reminder, and I look forward to your Yoot.DomainDriven!

  1. No trackbacks yet.