How to go about getting the link entity (foreign key) value in nhibernate when using event listeners to implement audit logging. I am able to log all entity properties except for the link entity, which just log the name of the link entity instead of the value.
It would be great if somebody could point me in the right direction or provide sample code. Here is what I have currently.
public void OnPostInsert(PostInsertEvent @event)
{
if (@event.Entity is AuditLog)
{
return;
}
ISession session = @event.Session.GetSession(EntityMode.Poco);
string username = httpContextHelper.GetCurrentUsersLoginName();
var props = @event.State;
for (int i = 0; i < props.Length; i++)
{
string newValue = string.Empty;
//Tried this but obj always null............
//var obj = props[i] as INHibernateProxy;
//if (obj != null)
//{
// newValue = obj.HibernateLazyInitializer.Identifier.ToString();
// session.Evict(props[i]);
//}
newValue = (props[i] == null) ? string.Empty : props[i].ToString();
if (props[i] != null)
{
session.Save(new AuditLog
{
AuditEntryType = "Insert",
DomainFullName = @event.Entity.GetType().FullName,
DomainShortName = @event.Entity.GetType().Name,
OldValue = string.Empty,
FieldName = @event.Persister.PropertyNames[i],
NewValue = newValue,
DomainId = (int)@event.Id,
Timestamp = DateTime.Now,
Username = username
});
}
}
}
Why dont get the property value by name in @event.Entity. Im not sure where the names are in hibernate, because cheking my src in nhibernate (c#) is a parameter called propertyNames i can get them using reflection.