NHibernate Mapping - <component/>

time to read 2 min | 330 words

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


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"

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

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:


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