流畅的NHibernate生成审计

时间:2012-06-08 18:59:42

标签: c# nhibernate fluent-nhibernate

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

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

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

User.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; }
}
}

AuditInterceptor.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;
    }
}
}

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

UserMap.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");
    }
}
}

SessionHelper.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应该是'public / protected virtual'或'protected internal virtual' ZeroBase.Domain.Entities.User:方法get_Modified应为'public / protected virtual'或'protected internal virtual'“}

这是什么试图告诉我的?

这与Fluent NHibernate有什么关系吗?

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

1 个答案:

答案 0 :(得分:5)

Created课程中的

ModifiedAuditableEntity<T>属性需要是虚拟的。如果您使用延迟加载(默认情况下为on),这是NHibernate的requirement

此外,SO上有some questions about