Fluent NHibernate 存储库及其子类
难以理解使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NHibernate 将为您管理这个。您唯一要做的就是查询 ID 为 23 的 Person。NHibernate 将返回此人作为其基类 Person 强制转换的任何类型。
这当然是一个简单的例子,但我希望它能展示多态继承的原理。策略模式和泛型是对抗这些丑陋的类型检查的强大武器。
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.
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.