简体   繁体   English

流利的NHibernate继承映射类型

[英]Fluent NHibernate Inheritance mapping type

I'm new to Fluent NHibernate, thus far I managed to get my mapping working except for the inheritance part. 我是Fluent NHibernate的新手,到目前为止,除了继承部分,我设法使映射正常工作。 Is there anybody who could help me finish the mapping? 有谁可以帮助我完成映射? I have simplified the code as much as possible. 我已尽可能简化了代码。

Thank you! 谢谢!

My database: 我的数据库:

CREATE TABLE [User] (
UserID                  INT             NOT NULL IDENTITY(1,1),
Type                    CHAR(1)         NOT NULL,
Email                   VARCHAR(255)    NOT NULL,
PRIMARY KEY(UserID)
);

CREATE TABLE [Student] (
UserID                  INT             NOT NULL,
Firstname               VARCHAR(255)    NOT NULL,
PRIMARY KEY(UserID),
FOREIGN KEY(UserID) REFERENCES [User](UserID)               
);

CREATE TABLE [Company] (
UserID                  INT             NOT NULL,
Name                    VARCHAR(255)    NOT NULL,
PRIMARY KEY(UserID),
FOREIGN KEY(UserID) REFERENCES [User](UserID),
);

My classes: 我的课程:

public class User
{
    public virtual int UserID { get; set; }
    public virtual UserType Type { get; set; }
    public virtual string Email { get; set; }
    public User()
    {
    }
}

public class Student : User
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public Student() 
        : base()
    {

    }
}

public class Company : User
{
    public virtual string Name { get; set; }
    public Company() 
        : base()
    {
    }
}

public enum UserType
{
    STUDENT = 0,
    COMPANY = 1
}

Mapping: 对应:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("[User]");
        Id(x => x.UserID);
        Map(x => x.Type).CustomType<int>();
        Map(x => x.Email);
    }
}

public class CompanyMap : ClassMap<Company>
{
    public CompanyMap()
    {
        Table("Company");
        Id(x => x.UserID);
        Map(x => x.Name);

    }
}

public class StudentMap: ClassMap<Student>
{
    public StudentMap()
    {
        Table("Student");
        Id(x => x.UserID);
        Map(x => x.Firstname);
        Map(x => x.Lastname);    
    }
}

There are few really good articles on the internet about Fluent mapping and NHibernate inheritance. 互联网上很少有关于Fluent mapping和NHibernate继承的好文章。 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 ,但是它也提供了有关Fluent mapping详细说明(向下滚动)。

Mapping-by-Code - inheritance by Adam Bar 代码映射 -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. 在这种情况下,在按代码映射中,我们必须通过继承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 Igor Ignatov编写的Fluent Nhibernate中继承映射策略


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. 换句话说,HAS-A可能比IS-A关系更好。

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. NHibernate是一个非常强大的工具,几乎可以支持我们的任何愿望……但是它仍然不意味着我们应该使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM