Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate AutoMapping with discriminator

I'm trying to map inheritance with discriminator, but subclasses don't have discriminator value. How to solve it using AutoMappings?

Domain objects are as following:

public abstract class Item : GuidIdentityEntity {
   public virtual string Name { get; set; }
}

public class Product : Item {}
public class RawMaterial : Item {}

Configuration looks like:

AssemblyOf<Item>()
    .IgnoreBase<GuidIdentityEntity>();
    .IncludeBase<Item>();
    .Setup(setup => {
        setup.DiscriminatorColumn = type => "Discriminator";
        setup.IsDiscriminated = type => type == typeof(Item);
        setup.SubclassStrategy = type => (type == typeof(Item)) 
            ? SubclassStrategy.Subclass 
            : SubclassStrategy.JoinedSubclass;
    });

Outcome of mappings are:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="Solution.Core.Products.Item, Solution.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e5ef41be3839ad7" table="`Item`">
    <id name="Id" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="guid.comb" />
    </id>
    <discriminator type="String">
      <column name="Discriminator" />
    </discriminator>
    <property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" />
    </property>
    <subclass name="Solution.Core.Products.RawMaterial, Solution.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e5ef41be3839ad7" />
    <subclass name="Solution.Core.Products.Product, Solution.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e5ef41be3839ad7" />
  </class>
</hibernate-mapping>
like image 474
Marek Tihkan Avatar asked Nov 10 '09 13:11

Marek Tihkan


1 Answers

I found that this works:

    public class SubclassConvention : ISubclassConvention, ISubclassConventionAcceptance
{
    #region IConvention<ISubclassInspector,ISubclassInstance> Members

    public void Apply(ISubclassInstance instance)
    {
        if (instance.Name == typeof(SalesInvoice).AssemblyQualifiedName)
            instance.DiscriminatorValue("SAL");
    }

    #endregion

    #region IConventionAcceptance<ISubclassInspector> Members

    public void Accept(IAcceptanceCriteria<ISubclassInspector> criteria)
    {
        criteria.Expect(subclass => Type.GetType(subclass.Name).BaseType == typeof(Invoice));
    }

    #endregion
}

In this scenario I have SalesInvoice class deriving from Invoice class. The acceptance criteria for subclass is true when current subclass derives from Invoice. Discriminator value is assigned based on type of subclass. As you can see, I compare types using names. Unfortunately for some reason subclass.EntityType property is null at runtime so I'm unable to compare types directly.

like image 56
Marcin Budny Avatar answered Oct 15 '22 08:10

Marcin Budny