Fluent NHibernate Inheritance mapping type

故事扮演 提交于 2019-12-06 14:38:12

There are few really good articles on the internet about Fluent mapping and NHibernate inheritance. One of them is about mapping-by-code, but it provides detailed explanation about the Fluent mapping as well (just scroll down)

Mapping-by-Code - inheritance by Adam Bar

small extract related to your scenario

... Table per class

The second strategy for mapping inheritance is table per class with joined subclasses. In this option subclasses are stored in separate tables that have foreign key to base class table and are joined with the table for base class, if needed. In this case, in mapping-by-code, we have to map subclasses by inheriting from JoinedSubclassMapping. Here is the example of joined subclass mapping with all available options:

public class CompanyMap : JoinedSubclassMapping<Company>
{
    public CompanyMap()
    {
        Key(k =>
        {
            k.Column("PartyId");
            // or...
            k.Column(c =>
            {
                c.Name("PartyId");
                // etc.
            });

            k.ForeignKey("party_fk");
            k.NotNullable(true);
            k.OnDelete(OnDeleteAction.Cascade); // or OnDeleteAction.NoAction
            k.PropertyRef(x => x.CompanyName);
            k.Unique(true);
            k.Update(true);
        });

        Property(x => x.CompanyName);
    }
}

Another really good and comprehensive article:

Inheritance mapping strategies in Fluent Nhibernate by Igor Ignatov


BUT, I would suggest:

Do not go this way. Do NOT use inheritance if possible. If you have to - do not use so deep inheritance.

Please, do read this:

Composition over inheritance

small cite:

Benefits

To favor composition over inheritance is a design principle that gives the design higher flexibility, giving business-domain classes and more stable business domain in the long term. In other words, HAS-A can be better than an IS-A relationship.

Initial design is simplified by identifying system object behaviors in separate interfaces instead of creating a hierarchical relationship to distribute behaviors among business-domain classes via inheritance. This approach more easily accommodates future requirements changes that would otherwise require a complete restructuring of business-domain classes in the inheritance model. Additionally, it avoids problems often associated with relatively minor changes to an inheritance-based model that includes several generations of classes.

NHibernate is really tremendous tool, supporting almost any kind of our wish... but it still should not mean, that we should use it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!