流利的NHibernate继承映射类型 [英] Fluent NHibernate Inheritance mapping type

查看:89
本文介绍了流利的NHibernate继承映射类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Fluent NHibernate的新手,到目前为止,除继承部分外,我设法使映射正常工作.有谁可以帮助我完成映射?我已尽可能简化了代码.

I'm new to Fluent NHibernate, thus far I managed to get my mapping working except for the inheritance part. Is there anybody who could help me finish the mapping? I have simplified the code as much as possible.

谢谢!

我的数据库:

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),
);

我的课程:

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
}

映射:

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);    
    }
}

推荐答案

Internet上很少有关于 Fluent mapping 和NHibernate继承的好文章.其中之一是关于mapping-by-code的信息,但是它提供了有关 Fluent mapping 以及(向下滚动)

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

映射继承的第二种策略是每个类具有联接子类的表.在此选项中,子类存储在单独的表中,这些表具有基类表的外键,并在需要时与基类表联接.在这种情况下,在按代码映射中,我们必须通过继承JoinedSubclassMapping来映射子类.这是带有所有可用选项的联接子类映射的示例:

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:

Fluent Nhibernate中的继承映射策略 ,作者:伊戈尔·伊格纳托夫(Igor Ignatov)

Inheritance mapping strategies in Fluent Nhibernate by Igor Ignatov

但是,我建议:

不要这样.如果可能,请不要使用继承.如果需要-不要使用那么深的继承.

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

请阅读以下内容:

小引用:

好处

相对于继承,主张组合是一种设计原则,该原则赋予设计更高的灵活性,从长远来看,提供业务域类和更稳定的业务域.换句话说,HAS-A可能比IS-A关系更好.

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确实是一个强大的工具,几乎可以满足我们的任何愿望……但这仍然不意味着我们应该使用它.

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

这篇关于流利的NHibernate继承映射类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆