Sunday, July 20, 2014

protected internal

Anne Epstein has a great NHibernate article on making an entity with a composite primary key be lazy-loading-capable and cache-ready. Just forgot to hide the ORM low-level plumbing concerns (the composite primary key) from the domain model


Similar to AdventureWorks' EmployeePayHistory. Just use protected internal to hide that ORM low-level plumbing concern
public class PersonPayHistory
{
    PersonPayHistoryCompositePK _pk = new PersonPayHistoryCompositePK();
    protected internal PersonPayHistoryCompositePK PersonPayHistoryCompositePK 
    { 
        get { return _pk; }
        set { _pk = value; }
    }

    Person _person;
    public virtual Person Person 
    { 
        get { return _person; }
        set 
        {
            _person = value;
            _pk.PersonId = _person.PersonId;
        }
    }                    

    public virtual DateTime RateDate
    {
        get { return _pk.RateDate; }
        set { _pk.RateDate = value; }
    }

    public virtual decimal Rate { get; set; }
}


So we will not make the mistake of saving our entity through those composite primary key:

var ph = new PersonPayHistory
{     
    Person = session.Load<Person>(1),
    RateDate = DateTime.Today,

    Rate = 1337M
};


session.Save (ph);
session.Flush ();

No comments:

Post a Comment