Fluent NHibernate生成的审计

本文关键字:审计 NHibernate Fluent | 更新日期: 2023-09-27 18:36:47

我需要一些帮助来设置我的Fluent NHibernate POCO类结构。我试图简单地有一种使对象可审计的方法。在创建时,我希望设置我创建和修改的日期时间,在更新时,我希望更新我修改的日期时间。我一直在遵循一些例子,但我遇到了障碍。这是我当前的设置:

可依.cs

namespace ZeroBase.Domain.Entities
{
public interface IAuditable
{
    DateTime Created
    {
        get;
    }
    DateTime Modified
    {
        get;
    }
    string CreatedPropertyName
    {
        get;
    }
    string ModifiedPropertyName
    {
        get;
    }
    void SetCreationDate(DateTime created);
    void SetModifiedDate(DateTime modified);
}
}

可审计实体.cs

namespace ZeroBase.Domain.Entities
{
public class AuditableEntity<T> : IAuditable
{
    public DateTime Created { get; private set; }
    public DateTime Modified { get; private set; }
    void IAuditable.SetCreationDate(DateTime created)
    {
        this.Created = created;
    }
    void IAuditable.SetModifiedDate(DateTime modified)
    {
        this.Modified = modified;
    }
    string IAuditable.CreatedPropertyName
    {
        get
        {
            string createdPropName = "Created";
        #if DEBUG
            CheckIfPropertyExists(createdPropName);
        #endif
            return createdPropName;
        }
    }
    string IAuditable.ModifiedPropertyName
    {
        get
        {
            string modifiedPropName = "Modified";
        #if DEBUG
            CheckIfPropertyExists(modifiedPropName);
        #endif
            return modifiedPropName;
        }
    }
    private void CheckIfPropertyExists(string propertyName)
    {
        PropertyInfo pi = this.GetType().GetProperty(propertyName);
        Debug.Assert(pi != null, String.Format("There exists no property {0}", propertyName));
    }
}
}

用户.cs

namespace ZeroBase.Domain.Entities
{
public class User : AuditableEntity<User>
{
    public virtual Guid Id { get; set; }
    public virtual string Username { get; set; }
    public virtual string Password { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string EmailAddress { get; set; }
    public virtual IEnumerable<Comment> Comments { get; set; }
}
}

审计拦截器.cs

namespace ZeroBase.Infrastructure.Data
{
public class AuditInterceptor : EmptyInterceptor
{
    public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState,
        string[] propertyNames, NHibernate.Type.IType[] types)
    {
        IAuditable auditableObject = entity as IAuditable;
        if (auditableObject != null)
        {
            for (int i = 0; i < propertyNames.Length; i++)
            {
                if (propertyNames[i] == auditableObject.ModifiedPropertyName)
                {
                    currentState[i] = DateTime.Now;
                }
            }
            return true;
        }
        return false;
    }
    public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
    {
        IAuditable auditableObject = entity as IAuditable;
        if (auditableObject != null)
        {
            DateTime currentDate = DateTime.Now;
            for (int i = 0; i < propertyNames.Length; i++)
            {
                if (propertyNames[i] == auditableObject.CreatedPropertyName)
                {
                    state[i] = currentDate;
                }
                if (propertyNames[i] == auditableObject.ModifiedPropertyName)
                {
                    state[i] = currentDate;
                }
            }
            System.Diagnostics.Debug.WriteLine("interceptor: created: " + auditableObject.Created);
            System.Diagnostics.Debug.WriteLine("interceptor: modified: " + auditableObject.Modified);
            return true;
        }
        return false;
    }
}
}

审计地图.cs

namespace ZeroBase.Infrastructure.Data
{
public class AuditMap<T>: ClassMap<T> where T : AuditableEntity<T>
{
    public AuditMap()
    {
        Map(p => p.Created);
        Map(p => p.Modified);
    }
}
}

用户地图.cs

namespace ZeroBase.Infrastructure.Data
{
public class UserMap : AuditMap<User>
{
    public UserMap()
    {
        Id(x => x.Id)
            .Column("Id")
            .GeneratedBy.Guid();
        Map(x => x.Username);
        Map(x => x.Password);
        Map(x => x.FirstName);
        Map(x => x.LastName);
        Map(x => x.EmailAddress);
        HasMany(x => x.Comments);
        Table("Users");
    }
}
}

会话助手.cs

_sessionFactory = Fluently.Configure()
            // Set up database connection
            .Database(MsSqlConfiguration.MsSql2005
                .ConnectionString(x => x.Is(_connectionString))
                //.ShowSql()
            )
            // Use class mappings
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<UserMap>())
            .ExposeConfiguration(c => c.SetInterceptor(new AuditInterceptor()))
            .BuildSessionFactory();

当我尝试运行它时,我收到以下运行时错误:以下类型不能用作代理:ZeroBase.Domain.Entities.User:方法get_Created应为"公共/受保护的虚拟"或"受保护的内部虚拟"ZeroBase.Domain.Entities.User:方法get_Modified应该是'公共/受保护的虚拟'或'受保护的内部虚拟'"}

这是想告诉我什么?

这和Fluent NHibernate有什么关系吗?

我有点困惑,希望得到一些帮助!

Fluent NHibernate生成的审计

AuditableEntity<T>类中的CreatedModified属性必须是虚拟的。这是 NHibernate 的要求,如果您使用的是延迟加载(默认情况下为打开)。

另外,在SO上也有一些关于它的问题。