Creating Partial Domain Models
Here is an interesting problem. I want to package a domain model with specific functionality, but I don't want to constrain the model users of this functionality will use.
For example, let us take security systems and the User entity. I have yet to see any two projects that had identical users entities.
Now, the concept of a user in a security system is pretty important, I would say. But no important enough that I would want to force my idea about what a user looks like on an innocent domain model.
Problem, right?
Now really. We can use the usual OO abstractions to handle this. Check this domain model:
The only requirement that this security system has is to have a User entity that implements the IUser interface. The only requirement that this has is to expose a single property, SecurityInfo, which describe the user. Notice the SecurityIdPropertyName there, it is there to allow us to do queries based on the security id without forcing a structure on the user entity.
How do it actually work, however, is more interesting.
At initialization time, we perform Mapping Rewrite and tell NHibernate that the concrete implementation of IUser in all its associations is mapped to the User entity from our project.
This means that we get to have the cake (separate domain models) and eat it (rich domain models without constraints).
Comments
I see you're coming around to my way of thinking around interfaces for domain objects. Remember that once you have those how easy things like custom fetching strategies becomes.
Nope,
I don't think so.
It is just a way to plug an entity into a different model without requiring that both models will be tied to one another
Indeed, I used the same approach ... and it has also another use:
http://blog.yoot.be/2008/01/partial-domain-models-and-more.html
Hmm
Isnt it possible to write the mapping to a interface in NH and let a inteceptor create the correct instance?
Walker,
Yes, it is. But then you force the shape of the entity as the shape of the interface.
I just refreshed RhinoTools from svn and noticed the security framework, just 1 day after i started porting this: http://tinymarbles.net/msp/talos/documentation.html
Just a question.. is there a problem generally in mixing AR code with naked NHibernate. I tend to favour the latter, but i may want to use your code in an upcoming project...
l0t3k,
Since I didn't implement the IsAllowed() yet, this is a compliment :-)
I am going to support both AR and NH, so that shouldn't be a problem
Comment preview