在Fluent NHibernate Automapping中自动生成创建日期

时间:2016-06-23 09:00:53

标签: c# nhibernate fluent-nhibernate automapping

我正在使用Fluent NHibernate的自动化/约定功能将我的实体映射到数据库表。我有这个抽象类,我的应用程序中的每个域实体都来自:

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);
}

1 个答案:

答案 0 :(得分:1)

你应该使用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