与nhibernate双向关系模式 [英] bidirectional relationship patterns with nhibernate

查看:155
本文介绍了与nhibernate双向关系模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的领域里,一个员工和一个部门有一个一对多的双向关系;为了让子Employee同步这个,我有一个内部访问字段的集合(Iesi的NHibernate)的雇员在该部门,否则将只读公开。像这样:

部门类:

 受保护的内部ISet<员工> _员工; 
public virtual ReadOnlyCollection< Employee>员工{
获得{返回新列表<员工>(_ staff).AsReadOnly(); }
}
public virtual void AddStaff(Employee emp){
emp.Department = this; }
}

员工类:

  private Department _department; 
公共虚拟部门部门{
set {
//检查有效值等
value._staff.Add(this); (FNH)映射AsField(







$ b

Prefix.Underscore),但由于我不能使Department._staff字段虚拟NH不开心。我想我可以让这个领域成为一个虚拟的财产,并强制喂它,但感觉就像我让域类过度地意识到持久性。



我正在学习NH和FNH,我知道我需要一个关系映射,但我的主要问题是这个职位的主要问题是在我的领域类的逻辑:

1)这是一个很好的c#编程模式只读设置在一个biderectional关系?

2)什么是最好的方法,使这个更有用的NHibernate?



感谢分享!

Berryl

解决方案

<我使用这种模式实现了一对多的关系:

$ p $ public class Department
{
私人IList<员工> _staff = new List< Staff>();

公共虚拟IEnumerable<员工>职员
{
得到{return _staff; }
}

//添加和删除根据需要添加额外的支票

public virtual void AddStaff(Employee staff)
{
部门=这个;
_staff.Add(staff);


public virtual void RemoveStaff(Employee staff)
{
staff.Department = null;
_staff.Remove(staff);


$ / code $ / pre


$ b

  public class Employee 
{
public virtual Department Department {get;内部设置; }



$ b $在这种情况下,Department是关系的反面。 p>

In my domain, an Employee and a Department have a one-to-many bidirectional relationship; to let the child Employee synchronize this, I have an 'internal' access field for the Set (Iesi for NHibernate) of Employees that are in the Department, which would otherwise be readonly public. Like so:

Department class:

protected internal ISet<Employee> _staff;  
public virtual ReadOnlyCollection<Employee> Staff {  
   get { return new List<Employee>(_staff).AsReadOnly(); }   
}  
public virtual void AddStaff(Employee emp) {
    emp.Department = this;  } 
}

Employee class:

private Department _department;
public virtual Department Department {
   set {
       // check valid value, etc.
       value._staff.Add(this);
   }
}

I made access in my (FNH) mapping AsField(Prefix.Underscore), but since I can't make the Department._staff field virtual NH is not happy. I guess I could make the field a virtual property and force feed it, but that feels like I'm making the domain class overly aware of persistence.

I'm learning both NH and FNH and I know I need a good primer in relationship mapping, but my primary question for this post is the logic in my domain classes:
1) Is this a good c# programming pattern for a readonly set in a biderectional relationship?
2) What is the best way to make this more usable to NHibernate?

Thanks for sharing!
Berryl

解决方案

I implement one-to-many relationships using this pattern:

public class Department
{
    private IList<Employee> _staff = new List<Staff>();

    public virtual IEnumerable<Employee> Staff
    {
        get { return _staff; }
    }

    // Add and Remove have additional checks in them as needed

    public virtual void AddStaff(Employee staff)
    {
        staff.Department = this;
        _staff.Add(staff);
    }

    public virtual void RemoveStaff(Employee staff)
    {
        staff.Department = null;
        _staff.Remove(staff);
    }
}

 

public class Employee
{
    public virtual Department Department { get; internal set; }
}

In this case, Department is the inverse side of the relationship.

这篇关于与nhibernate双向关系模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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