views:

247

answers:

3

In my domain driven design I have a containing entity, say Car, which has a list of constituent entities, e.g. Wheel.

Now, I want to add a ChangeWheel method to my car entity. Note that changing the wheel does not actually modify the car entity as such. It only affects one of the entites in its wheel collection. I.e. the Car entity has an IList attribute. In my database schema I have a car entity which has no reference to Wheels - rather, the Wheels table has a foreign key of CarId.

When I change the wheel on a car, I update the particular wheel record. The only other table I want to update is a version audit table in the database.

If I, via NHibernate, extract a Car entity from the repository, call ChangeWheel(...), and then save back down again, can I configure NHibernate to not save down a new version of the Car record?

I hope all this makes sense - please ask if not and thanks in advance.

+1  A: 

NHibernate wraps up properties which are collections (like Wheels in your case), with its own collection which derives from Iesi.Collections.Generic.ISet. Once this is done, it can track changes to the collection and this of course means that it only issues the needed updates.

Keep in mind that you should implement Equals and GetHashCode for Wheel in this case. Here's a blog post on implementing Equals and GetHashCode:

Implemeting Equals and GetHashCode

Praveen Angyan
+1  A: 

NHibernate will do this naturally. Do you have an existing mapping or is this just something hypothetical?

ShaneC
Just something hypothetical at this point. A new project for which I'm considering NHibernate.
ng5000
+1  A: 

From the NHibernate docs:

  • When we remove / add an object from / to a collection, the version number of the collection owner is incremented.

However... you could try persisting the newly added Wheel, but not persist the Car itself. This should give the desired effect, but NHibernate will always consider the Car to be dirty (watch your ISession management)

Vijay Patel
Thanks - does every entity (either/both business object and DB tables) need to have a version number column/field when working with nhibernate?
ng5000
No. Version numbers are used for optimistic locking.
ShaneC