NHibernate映射空对象/特殊情况模式

Ber*_*ryl 5 nhibernate-mapping fluent-nhibernate

我想要一个'UnassignedDepartment'对象,而不是让员工有一个null部门:

public class UnassignedDepartment : Department
{
    public UnassignedDepartment() : base("not yet assigned") {
        Id = -99; <-- just some Id that can be held constant, not be generated..
    }
}
Run Code Online (Sandbox Code Playgroud)

这可以通过Department类中的静态便捷字段访问:

public class Department : Entity
{
    public static readonly Department UNASSIGNED = new UnassignedDepartment();

    ....    
} 
Run Code Online (Sandbox Code Playgroud)

我使用S#rpArch的框架作为基本实体,混合了FNH自动化,覆盖和约定.从持久性的角度来看,将其与具有"特殊"ID的其他部门保持一致似乎是合乎逻辑的,但我不知道如何正确地做到这一点.请赐教!

Thx,Berryl

Jam*_*Ide 2

我不明白你想要完成什么,但这也许会有所帮助。将 Department 映射为 Employee 中的私有字段,如果为 null,则返回 UnassignedDepartment。

private Department _department; // map this in FNH

public Department Department
{
    get { return _department ?? _department.UNASSIGNED; }
}
Run Code Online (Sandbox Code Playgroud)