这是我的设置......
类/接口:
IEntity
User : IEntity
Vehicle : IEntity
我正在使用Fluent NHibernate进行设置。
我要做的是为每个Entity类保存Created,Creator,Updated和Updater。
我有这些NHibernate听众来完成这个:
public class NHibernateListener : IPreInsertEventListener, IPreUpdateEventListener
{
private static readonly string CreatedPropertyName = GetPropertyName<IEntity>(val => val.Created);
private static readonly string CreatorPropertyName = GetPropertyName<IEntity>(val => val.Creator);
private static readonly string UpdatedPropertyName = GetPropertyName<IEntity>(val => val.Updated);
private static readonly string UpdaterPropertyName = GetPropertyName<IEntity>(val => val.Updater);
public bool OnPreInsert(PreInsertEvent @event)
{
IEntity entity;
if ((entity = @event.Entity as IEntity) != null)
{
var currentDate = DateTime.Now;
entity.Created = entity.Updated = currentDate;
if (User.CurrentUser != null)
{
entity.Creator = entity.Updater = User.CurrentUser;
Set(@event.Persister, @event.State, CreatorPropertyName, currentDate);
Set(@event.Persister, @event.State, UpdaterPropertyName, currentDate);
}
Set(@event.Persister, @event.State, CreatedPropertyName, currentDate);
Set(@event.Persister, @event.State, UpdatedPropertyName, currentDate);
}
return false;
}
public bool OnPreUpdate(PreUpdateEvent @event)
{
IEntity entity;
if ((entity = @event.Entity as IEntity) != null)
{
var currentDate = DateTime.Now;
entity.Updated = currentDate;
if (User.CurrentUser != null)
{
entity.Creator = entity.Updater = User.CurrentUser;
Set(@event.Persister, @event.State, UpdaterPropertyName, currentDate);
}
Set(@event.Persister, @event.State, UpdatedPropertyName, currentDate);
}
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;
}
private static string GetPropertyName<TType>(Expression<Func<TType, object>> expression)
{
return ExpressionProcessor.FindPropertyExpression(expression.Body);
}
}
Created
和Updated
工作正常。创建者和更新者无法正常工作。
类
IEntity
public enum Status
{
Active,
Inactive
}
public interface IEntity
{
Guid Id { get; }
DateTime Created { get; set; }
User Creator { get; set; }
DateTime Updated { get; set; }
User Updater { get; set; }
Status Status { get; set; }
}
用户 - 请注意,存在名为Property
的静态CurrentUser
,其类型为User
。
public class User : IEntity
{
public static User CurrentUser { get; set; }
public virtual string Email { get; protected set; }
public virtual Guid Id { get; protected set; }
public virtual DateTime Created { get; set; }
public virtual User Creator { get; set; }
public virtual DateTime Updated { get; set; }
public virtual User Updater { get; set; }
public virtual Status Status { get; set; }
public virtual string FirstName { get; protected set; }
public virtual string LastName { get; protected set; }
public virtual IEnumerable<Vehicle> Vehicles { get; protected set; }
protected User()
{
}
public User(Guid id, string email, string firstName, string lastName)
{
Id = id;
FirstName = firstName;
LastName = lastName;
Email = email;
}
public virtual void AddVehicle(Vehicle vehicle)
{
((IList<Vehicle>) Vehicles).Add(vehicle);
vehicle.AddUser(this);
}
}
车辆
public class Vehicle : IEntity
{
protected Vehicle()
{
}
public Vehicle(Guid id, string make, string model, string color)
{
Id = id;
Make = make;
Model = model;
Color = color;
Users = new List<User>();
}
public virtual string Make{ get; set; }
public virtual string Model{ get; set; }
public virtual string Color { get; set; }
public virtual IEnumerable<User> Users { get; protected set; }
public virtual Guid Id { get; set; }
public virtual DateTime Created { get; set; }
public virtual User Creator { get; set; }
public virtual DateTime Updated { get; set; }
public virtual User Updater { get; set; }
public virtual Status Status { get; set; }
protected internal virtual void AddUser(User user)
{
((IList<User>) Users).Add(user);
}
}
我正在使用自动化,没有覆盖这两个类。
当我运行时,我在事务尝试提交时收到以下错误:
Message: Error dehydrating property value for MyApp.Domain.Users.User.Creator
Inner Exception: Exception occurred getter of MyApp.Domain.Users.User.Id
Inner Inner Exception: Object does not match target type.
答案 0 :(得分:0)
这两行看起来不太好看:
Set(@event.Persister, @event.State, CreatorPropertyName, currentDate);
Set(@event.Persister, @event.State, UpdaterPropertyName, currentDate);
他们甚至应该在那里吗?