Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Mapping Null Object / Special Case Pattern

I'd like to have an 'UnassignedDepartment' object instead of letting employees have a null Department:

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

This is accessible by a static convenience field in the Department class:

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

    ....    
} 

I'm using S#rpArch's framework as the base Entity, with a mix of FNH automapping, overrides & conventions. From a persistence standpoint, it seems logical to keep this with the other Departments with a 'special' Id, but I don't know how to do that properly. Please enlighten me!

Thx, Berryl

like image 209
Berryl Avatar asked Nov 06 '22 21:11

Berryl


1 Answers

I don't understand what you're trying to accomplish but maybe this will help. Map Department as a private field in Employee and return UnassignedDepartment if it's null.

private Department _department; // map this in FNH

public Department Department
{
    get { return _department ?? _department.UNASSIGNED; }
}
like image 59
Jamie Ide Avatar answered Nov 17 '22 14:11

Jamie Ide