Sunday, October 12, 2008

Cleaning up bad NHibernate/DDD practices

I have of late managed to be involved in a new green-fields project at work. I am sure everyone can relate to the elation you get when you have the opportunity to work on something new an "do it properly this time". I have had the privilege of being able to set everything up how I wanted. Not being a complete ass I have tried to get the team involved as much as possible and explain why/how I am doing things along the way. I think the thing I have found most enjoyable is setting up the means to creating a decent domain.

For the first time we have

  • A clear delineation between value types and entities
  • real value types
  • generic repositories that only allow entities*
  • protected empty constructors on all entities forcing you to construct the entity with the more public greedy constructor that actually puts the entity in a valid state
  • Corrected sub classing of entities when behaviour is different for different types
  • Non insertable type field on subclasses (that match the discriminator column) so the guys can still query "the old way"**
  • Protected setters by default
  • DBC that matches our DB & business constraints, down to the setter level
  • A serious drive to push DOMAIN logic in to the DOMAIN objects
  • Specifications

This is all pretty basic but these key aspect were not in our last big project. It may sound like having one repository per aggregate root would be a hard thing, but to be honest its a good thing and the headaches it could have saved my on the last project; NHibernate issues (transient state/dif object same id etc) and more importantly undetected business rule errors in the code.

What I am really hoping is that now the the hard work has been done that the team can more clearly see why we do things in the way we do them.

*I need buy in from the team on the whole Entity/Value type thing before dropping the Aggregate Root bomb shell

**Again this is perhaps not the best way to do it, but I feel for the moment its the best way for the team, while moving in the right direction

2 comments:

nobodybutca said...

Kjetil,

How's your project doing, I also on the verge of adopting NHibernate and DDD. for the past 1 week now, I am on reading stuff on DDD.

br,

nobody

Unknown said...

Hi... i guess you think this is someone elses blog? but yeah the project went well and i have to recommend you reading Eric Evans DDD book and the (N)Hibernate in Action from manning. Also look at Bill Mcaffertys work with NH best practices and S#arp Architecture (Asp.net MVC + NH)