views:

21

answers:

1

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 Line1 { 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?

A: 

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.

JasonTrue
Thanks Nhibernate listener did the trick very easily.
mateo