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:

image

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).

Print | posted on Saturday, January 12, 2008 9:13 AM

Feedback


Gravatar

# re: Creating Partial Domain Models 1/12/2008 11:58 AM Udi Dahan

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.


Gravatar

# re: Creating Partial Domain Models 1/12/2008 12:02 PM Ayende Rahien

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


Gravatar

# re: Creating Partial Domain Models 1/12/2008 12:51 PM Stiiifff

Indeed, I used the same approach ... and it has also another use:
http://blog.yoot.be/2008/01/partial-domain-models-and-more.html


Gravatar

# re: Creating Partial Domain Models 1/12/2008 5:42 PM Walker

Hmm

Isnt it possible to write the mapping to a interface in NH and let a inteceptor create the correct instance?


Gravatar

# re: Creating Partial Domain Models 1/12/2008 5:43 PM Ayende Rahien

Walker,
Yes, it is. But then you force the shape of the entity as the shape of the interface.


Gravatar

# re: Creating Partial Domain Models 1/13/2008 10:18 PM l0t3k

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...


Gravatar

# re: Creating Partial Domain Models 1/13/2008 10:41 PM Ayende Rahien

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

Comments have been closed on this topic.