All of my NH entities derive from a type called BusinessEntity, it has the most basic values ID, Created, CreatedBy, Updated, UpdatedBy.
CreatedBy / UpdatedBy takes a User
I have a IPreUpdateEventListener and IPreInsertEventListener that fire to get the current DateTime for the audit values. Also in here I have my logic to get the current user running which I grab by running a criteria query against the windows user principle. From what I understood from all of post on the NH user group on this subject my User class needs to be eagerly loaded for this to work correctly in my EventListeners this is how I load the user
public User GetByDomainPrinciple(string domainPrinciple)
{
var domainPrincipleCriteria = DetachedCriteria.For<User>()
.Add(Restrictions.Eq("DomainPrinciple", domainPrinciple))
.SetFetchMode("Roles", FetchMode.Eager)
.SetFetchMode("Groups", FetchMode.Eager)
.SetFetchMode("Groups.Roles", FetchMode.Eager)
.SetCacheable(true);
return Repository.QuerySingle(domainPrincipleCriteria);
}
Repository.QuerySingle(domainPrincipleCriteria);
is just
return detachedCriteria
.GetExecutableCriteria(_conversation.Session).UniqueResult<T>();
Am I missing something or is my criteria query wrong? I guess absolute worst case scenario I could change CreatedBy to be a Guid instead of a User and just manually assign the FK like that, but that seems very dirty.