Fluent NHibernate 存储库及其子类

发布于 2024-08-30 12:37:27 字数 1240 浏览 5 评论 0原文

难以理解使用 Fluent NHibernate 通过通用存储库实现子类的最佳方法。

我有一个基类和两个子类,例如:

public abstract class Person
{
    public virtual int PersonId { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}

public class Student : Person
{
    public virtual decimal GPA { get; set; }
}

public class Teacher : Person
{
    public virtual decimal Salary { get; set; }
}

我的映射如下:

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Table("Persons");

        Id(x => x.PersonId).GeneratedBy.Identity();

        Map(x => x.FirstName);
        Map(x => x.LastName);
    }
}


public class StudentMap : SubclassMap<Student>
{
    public StudentMap()
    {
        Table("Students");
        KeyColumn("PersonId");
        Map(x => x.GPA);
    }
}

public class TeacherMap : SubclassMap<Teacher>
{
    public TeacherMap()
    {
        Table("Teachers");
        KeyColumn("PersonId");
        Map(x => x.Salary);
    }
}

我使用通用存储库来保存/检索/更新实体,并且它工作得很好 - 假设我正在使用存储库 - 我已经知道我正在与学生或老师一起工作。

我遇到的问题是:当我有 ID 并且需要确定人的类型时会发生什么?如果用户以 PersonId = 23 的身份访问我的网站,我该如何确定他属于哪种类型?

Having some difficulty understanding the best way to implement subclasses with a generic repository using Fluent NHibernate.

I have a base class and two subclasses, say:

public abstract class Person
{
    public virtual int PersonId { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}

public class Student : Person
{
    public virtual decimal GPA { get; set; }
}

public class Teacher : Person
{
    public virtual decimal Salary { get; set; }
}

My Mappings are as follows:

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Table("Persons");

        Id(x => x.PersonId).GeneratedBy.Identity();

        Map(x => x.FirstName);
        Map(x => x.LastName);
    }
}


public class StudentMap : SubclassMap<Student>
{
    public StudentMap()
    {
        Table("Students");
        KeyColumn("PersonId");
        Map(x => x.GPA);
    }
}

public class TeacherMap : SubclassMap<Teacher>
{
    public TeacherMap()
    {
        Table("Teachers");
        KeyColumn("PersonId");
        Map(x => x.Salary);
    }
}

I use a generic repository to save/retreive/update the entities, and it works great--provided I'm working with Repository--where I already know that I'm working with students or working with teachers.

The problem I run into is this: What happens when I have an ID, and need to determine the TYPE of person? If a user comes to my site as PersonId = 23, how do I go about figuring out which type of person it is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滿滿的愛 2024-09-06 12:37:27

NHibernate 将为您管理这个。您唯一要做的就是查询 ID 为 23 的 Person。NHibernate 将返回此人作为其基类 Person 强制转换的任何类型。

Person p = dao.FindById<Person>(23);
if(p is Teacher)
{
   Teacher t = (Teacher)p;
}
else if(p is Student)
{
   Studet s =(Student)p;
}

这当然是一个简单的例子,但我希望它能展示多态继承的原理。策略模式和泛型是对抗这些丑陋的类型检查的强大武器。

NHibernate will manage this for you. The only thing you have to do is query the Person with Id 23. NHibernate will return whatever type this person is casted as its baseclass Person.

Person p = dao.FindById<Person>(23);
if(p is Teacher)
{
   Teacher t = (Teacher)p;
}
else if(p is Student)
{
   Studet s =(Student)p;
}

This is of course a simple example but i hope it shows the principle of polymorph inheritance. Strategy-pattern and generics are powerfull weapoons against these ugly type checks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文