Logo

NHibernate

The object-relational mapper for .NET

NHibernate Mapping - <component/>

<component/> is an interesting feature of NHibernate, which map more or less directly into the notion of a Value Type in DDD. This is a way to create an object model with higher granularity than the physical data model.

For example, let us take the following table:

And this object model:

image

They are quite different, where the physical data model put all the data in a single table, we want to treat them in our object model as two separate classes. This is where <component/> comes into play:

<class name="Person"
	table="People">

	<id name="Id">
		<generator class="identity"/>
	</id>
	<property name="Name" />
	<component name="Address">
		<property name="Line1"/>
		<property name="Line2"/>
		<property name="City"/>
		<property name="Country"/>
		<property name="ZipCode"/>
	</component>
</class>

This mapping will translate between the physical data model and the object model. And the mapping is complete, so even queries are done in the way you would expect it:

image

And then we let NHibernate sort it out and give us our pretty object graph.


Posted Wed, 08 April 2009 05:20:00 AM by Ayende
Filed under:

comments powered by Disqus
© NHibernate Community 2024