How can i automatically update a field before saveOrUpdate with Sharp Architecture
Lets say i have this entity
public class Address : Entity
{
public Address()
{
ModifiedDate = DateTime.Now;
}
[NotNull]
public virtual Province Province { get; set; }
[NotNullNotEmpty]
[Length(Max = 80)]
public virtual string Lin开发者_JAVA技巧e1 { get; set; }
[Length(Max = 80)]
public virtual string Line2 { get; set; }
[NotNullNotEmpty]
[Length(Max=50)]
public virtual string City { get; set; }
[NotNullNotEmpty]
[Length(Max = 15)]
public virtual string PostalCode { get; set; }
[NotNull]
public virtual DateTime ModifiedDate { get; set; }
}
i want the ModifiedDate to be updated before every SaveOrUpdate call? How can i do that ?
Is there a way to hook up something in the repository?
Assuming you're using NHibernate with SharpArchitecture (since that's the happy path), the nicest option is probably to add a custom NHibernate listener. An example is explained here: http://jakescott.tumblr.com/post/132853681/nhibernate-listeners-easy-last-modified-dates
If it were me, I would probably adjust that implementation to use DateTime.UtcNow instead of DateTime.Now, but that depends on your needs, so just choose your preference.
Since you're probably also using FluentNHibernate, see http://ferventcoder.com/archive/2009/11/18/nhibernate-event-listener-registration-with-fluent-nhibernate.aspx to find a Fluent configuration solution.
As an alternative, you can certainly write your repository implementation to do this for you.
精彩评论