Iai*_*der 5 c# nhibernate nhibernate-mapping
当该关系具有属性本身时,如何将类映射到同一类的其他实例?
我有一个名为Person的类,它被映射到表Person
PersonID PersonName PersonAge
----------------------------------
1 Dave Dee 55
2 Dozy 52
3 Beaky 45
4 Mick 55
5 Tich 58
Run Code Online (Sandbox Code Playgroud)
我希望使用名为PersonPerson的连接表在Person和Person之间建立多对多关系:
PersonPersonID PersonID RelatedPersonID RelationshipID
--------------------------------------------------------
1 1 5 1
2 3 4 2
3 2 1 3
Run Code Online (Sandbox Code Playgroud)
我想在PersonPerson表中使用以下属性:
RelationshipID RelationshipName
--------------------------------
1 Colleague
2 Manager
3 Tutor
Run Code Online (Sandbox Code Playgroud)
这个问题和Billy McCafferty的链接帖子解释了由于PersonPerson表中的其他列,PersonPerson关系必须从正常的JOIN升级到实体本身.但是它没有解释什么时候它是自我加入.不同的是,如果我向Dave Dee(ID = 1)询问所有相关人员,我不仅应该得到Tich(ID = 5),而且我也应该得到Dozy(ID = 2),因为Dave Dee也在RelatedPersonID列中.
到目前为止,我的解决方案是在Person类中有两个属性.
public virtual IList<PersonPerson> PersonPersonForward {get;set;}
public virtual IList<PersonPerson> PersonPersonBack {get;set;}
private List<PersonPerson> personPersonAll;
public virtual List<PersonPerson> PersonPersonAll
{
get
{
personPersonAll = new List<PersonPerson>(PersonPersonForward);
personPersonAll.AddRange(PersonPersonBack);
return personPersonAll;
}
}
Run Code Online (Sandbox Code Playgroud)
并在hbm中有以下内容:
<bag name="PersonPersonForward" table="PersonPerson" cascade="all">
<key column="PersonID"/>
<one-to-many class="PersonPerson" />
</bag>
<bag name="PersonPersonBack" table="PersonPerson" cascade="all">
<key column="RelatedPersonID"/>
<one-to-many class="PersonPerson" />
</bag>
Run Code Online (Sandbox Code Playgroud)
这看起来有点笨拙而且不够优雅.NHibernate通常为大多数日常问题提供优雅的解决方案.上述是明智的做法还是有更好的方法?
我想我也会这样做,但是,我认为这样建模有点“笨拙”。我的意思是:你有一个与某个人相关的人的集合,但你也有一个“反向关系”。
这真的有必要吗?是否可以选择删除此反向集合,而是在 PersonRepository 上指定一个方法,该方法可以返回与给定人员有某种关系的所有人员?
嗯,这可能听起来有点晦涩,所以这里有一些代码(请注意,为了简洁起见,我省略了“虚拟”修饰符等...(我也不想有这些修饰符,所以在 99 %的时间,我在我的类映射中指定“lazy=false”)。
public class Person
{
public int Id {get; set;}
public string Name {get; set;}
public IList<PersonPerson> _relatedPersons;
public ReadOnlyCollection<PersonPerson> RelatedPersons
{
get
{
// The RelatedPersons property is mapped with NHibernate, but
// using its backed field _relatedPersons (can be done using the
// access attrib in the HBM.
// I prefer to expose the collection itself as a readonlycollection
// to the client, so that RelatedPersons have to be added through
// the AddRelatedPerson method (and removed via a RemoveRelatedPerson method).
return new List<PersonPerson) (_relatedPersons).AsReadOnly();
}
}
public void AddRelatedPerson( Person p, RelationType relatesAs )
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,Person 类只剩下一个集合,即表示此 Person 所具有的关系的 PersonPerson 对象的集合。为了获取与给定 Person 具有关系的 Person,您可以在 PersonRepository 上创建返回这些 Person 的特定方法,而不是将它们放在 Person 类的集合中。我认为这也会提高性能。
public class NHPersonRepository : IPersonRepository
{
...
public IList<Person> FindPersonsThatHaveARelationShipWithPerson( Person p )
{
ICriteria crit = _session.CreateCriteria <Person>();
crit.AddAlias ("RelatedPersons", "r");
crit.Add (Expression.Eq ("r.RelatedWithPerson", p));
return crit.List();
}
}
Run Code Online (Sandbox Code Playgroud)
“反向引用”不是 Person 类的成员;必须通过存储库访问它。这也是 Eric Evans 在他的 DDD 书中所说的:在某些情况下,最好在存储库上有一个专门的方法,可以让您访问相关对象,而不是让它们(=相关对象)随身携带与物体本身。
我没有测试代码,我只是在这里输入它,所以我也没有检查语法错误等......但我认为它应该澄清我如何看待这一点。
| 归档时间: |
|
| 查看次数: |
1970 次 |
| 最近记录: |