1
votes

How can I create generic component mapping in NHibernate, I have a class Pair{virtual T First{get;set}, virtual T Second{get;set;}}, I am trying to map it with something like this which is not working

2

2 Answers

1
votes

It worked, trick was I had to use `, whereas I was using '

0
votes

I think you can map the class, but you in the mapping you have to explicitly declare which type will be use as the generic parameter, like this:

    <class name="GenericClass`1[ConcreteType], assembly"
         table="table">
        <id name="Id">
            <generator class="identity"/>
        </id>
    </class>

in your case, I guess it will be:

    <class name="Pair`1[Namespace.TheClass], assembly"
         table="table">
        <id name="Id">
            <generator class="identity"/>
        </id>
    <many-to-one name="First" class="Namespace.TheClass, assembly"/>
    </class>

as a component:

<component name="Property" class="Pair`1[Namespace.TheClass], assembly">
    <many-to-one name="First" class="Namespace.TheClass, assembly"/>
</component>

And if you want to use the same generic class, or component with another type as generic parameter, you have to create another mapping for that specific case.