Inheritance mix and tree structure using free nHibernate

The part of the model that I design is the geographic location of the hierarchy. Since there are several levels and you can share some information, I decided to use the class hierarchy as follows:

public class GeographicNode
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual GeographicNode ParentNode { get; set; }
    public virtual IList<GeographicNode> ChildNodes { get; set; }
}


public class Region : GeographicNode
{
    public virtual int SomeRegionData { get; set; }
}

public class Country : GeographicNode
{
    public virtual int SomeCountryData { get; set; }
}

To match this, I use the table-per-class-hierarchy method. The free display of nHibernate is as follows:

public class GeographicNodeMap : ClassMap<GeographicNode>
{
    public GeographicNodeMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        References(x => x.ParentNode);
        HasMany(x => x.ChildNodes).KeyColumn("Id").Cascade.All();

        DiscriminateSubClassesOnColumn("Type");
    }
}

public class RegionMap : SubclassMap<Region>
{
    public RegionMap()
    {
        Map(x => x.SomeRegionData)
    }
}

public class CountryMap : SubclassMap<Region>
{
    public CountryMap()
    {
        Map(x => x.SomeCountryData)
    }
}

Here is my question:

When I get a node and try to access the ParentNode (or child), its type is actually a GeographicNode, not the corresponding subclass. For example, if I get a Region node, and its parent must be a country node, I cannot use the ParentNode class for the Country class.

nHibernate ParentNode Child ? "", nHibernate .

- ? ( , ), , , (, ) ..

!

+3
2

. nhibernate , , , lazyloading - . :

  • Lazyloading : References(x => x.ParentNode).Not.LazyLoad();
  • : ReferenceAny<(x => x.ParentNode).TypeColumn("parenttype")..., NH

i 1,

0

Source: https://habr.com/ru/post/1734751/


All Articles