在 Fluent NHibernate Automapping 中自动生成创建日期
Automatically generate Created date in Fluent NHibernate Automapping
我正在使用 Fluent NHibernate 的自动映射/约定功能将我的实体映射到数据库表。我有这个摘要 class,我的应用程序中的每个域实体都来自:
public abstract class Entity
{
public virtual int Id { get; protected internal set; }
public virtual DateTime Created { get; set; }
}
如何修改我的 Fluent 配置,以便 Created
属性 的值在记录保存到数据库时自动设置为 DateTime.Now
的值。
这是我的 Fluent 配置供参考:
private FluentNHibernate.Cfg.FluentConfiguration GetConfiguration()
{
return Fluently
.Configure()
.Database(() =>
{
return MsSqlConfiguration.MsSql2012.ConnectionString(x => x.FromConnectionStringWithKey("main"));
}
)
.Mappings(
x =>
{
var persistenceModel = new PersistenceModel() { ValidationEnabled = false };
x.UsePersistenceModel(persistenceModel)
.AutoMappings.Add(
AutoMap.AssemblyOf<Entity>().Where(Exceptions.Load)
.Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never())
.Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultCascade.SaveUpdate())
.Conventions.Add(new FluentNHibernate.Conventions.Helpers.Builders.JoinConventionBuilder().Always(a => a.Fetch.Join()))
);
}
)
.ExposeConfiguration(BuildSchema);
}
你应该为此使用 NHibernate 的事件监听器;
public class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener
{
public bool OnPreUpdate(PreUpdateEvent @event)
{
var audit = @event.Entity as IHaveAuditInformation;
if (audit == null)
return false;
var time = DateTime.Now;
var name = WindowsIdentity.GetCurrent().Name;
Set(@event.Persister, @event.State, "UpdatedAt", time);
Set(@event.Persister, @event.State, "UpdatedBy", name);
audit.UpdatedAt = time;
audit.UpdatedBy = name;
return false;
}
public bool OnPreInsert(PreInsertEvent @event)
{
var audit = @event.Entity as IHaveAuditInformation;
if (audit == null)
return false;
var time = DateTime.Now;
var name = WindowsIdentity.GetCurrent().Name;
Set(@event.Persister, @event.State, "CreatedAt", time);
Set(@event.Persister, @event.State, "UpdatedAt", time);
Set(@event.Persister, @event.State, "CreatedBy", name);
Set(@event.Persister, @event.State, "UpdatedBy", name);
audit.CreatedAt = time;
audit.CreatedBy = name;
audit.UpdatedAt = time;
audit.UpdatedBy = name;
return false;
}
private void Set(IEntityPersister persister, object[] state, string propertyName, object value)
{
var index = Array.IndexOf(persister.PropertyNames, propertyName);
if (index == -1)
return;
state[index] = value;
}
}
https://ayende.com/blog/3987/nhibernate-ipreupdateeventlistener-ipreinserteventlistener
我正在使用 Fluent NHibernate 的自动映射/约定功能将我的实体映射到数据库表。我有这个摘要 class,我的应用程序中的每个域实体都来自:
public abstract class Entity
{
public virtual int Id { get; protected internal set; }
public virtual DateTime Created { get; set; }
}
如何修改我的 Fluent 配置,以便 Created
属性 的值在记录保存到数据库时自动设置为 DateTime.Now
的值。
这是我的 Fluent 配置供参考:
private FluentNHibernate.Cfg.FluentConfiguration GetConfiguration()
{
return Fluently
.Configure()
.Database(() =>
{
return MsSqlConfiguration.MsSql2012.ConnectionString(x => x.FromConnectionStringWithKey("main"));
}
)
.Mappings(
x =>
{
var persistenceModel = new PersistenceModel() { ValidationEnabled = false };
x.UsePersistenceModel(persistenceModel)
.AutoMappings.Add(
AutoMap.AssemblyOf<Entity>().Where(Exceptions.Load)
.Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never())
.Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultCascade.SaveUpdate())
.Conventions.Add(new FluentNHibernate.Conventions.Helpers.Builders.JoinConventionBuilder().Always(a => a.Fetch.Join()))
);
}
)
.ExposeConfiguration(BuildSchema);
}
你应该为此使用 NHibernate 的事件监听器;
public class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener
{
public bool OnPreUpdate(PreUpdateEvent @event)
{
var audit = @event.Entity as IHaveAuditInformation;
if (audit == null)
return false;
var time = DateTime.Now;
var name = WindowsIdentity.GetCurrent().Name;
Set(@event.Persister, @event.State, "UpdatedAt", time);
Set(@event.Persister, @event.State, "UpdatedBy", name);
audit.UpdatedAt = time;
audit.UpdatedBy = name;
return false;
}
public bool OnPreInsert(PreInsertEvent @event)
{
var audit = @event.Entity as IHaveAuditInformation;
if (audit == null)
return false;
var time = DateTime.Now;
var name = WindowsIdentity.GetCurrent().Name;
Set(@event.Persister, @event.State, "CreatedAt", time);
Set(@event.Persister, @event.State, "UpdatedAt", time);
Set(@event.Persister, @event.State, "CreatedBy", name);
Set(@event.Persister, @event.State, "UpdatedBy", name);
audit.CreatedAt = time;
audit.CreatedBy = name;
audit.UpdatedAt = time;
audit.UpdatedBy = name;
return false;
}
private void Set(IEntityPersister persister, object[] state, string propertyName, object value)
{
var index = Array.IndexOf(persister.PropertyNames, propertyName);
if (index == -1)
return;
state[index] = value;
}
}
https://ayende.com/blog/3987/nhibernate-ipreupdateeventlistener-ipreinserteventlistener