我需要一些帮助来设置我的 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);
}
}
可审计实体.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 有什么关系吗?
我有点困惑,希望得到一些帮助!