tags:

views:

28

answers:

3

Is there any way to reuse NHibernate components (<component>) in more than one mapping? For example: an Address class in Employee and Customer classes - The only way to do this which I see now is to copy-paste the Address component mapping from one of the class mappings into another.

A: 

Unfortunally not. I really need that feature too ;)

Pierre 303
+1  A: 

I haven't tried this with NHibernate but it worked with another OR Mapper that used XML configuration files. Create the component mapping in it's own XML file, then in the XML file that uses the component use an XML external entity to reference it. Here's a snippet from my Company mapping file:

<!DOCTYPE mappings [
    <!ENTITY Address SYSTEM "MyCompany.MyApplication.Mappings.Address.xml">
]>

<mappings version="4.2" defaultNamespace="MyCompany.Model" defaultSchema="dbo">
    <entity type="Company" table="Company" keyMember="CompanyId" keyType="Auto">
    &Address;
Jamie Ide
Thanks, Jamie, I'll try it
Serge
A: 

Apart from the solution that Jamie proposed, using code-based mappings provides better possibilities for reuse.

Currently, there are two such projects, Fluent NHibernate and ConfORM.

Diego Mijelshon