On Dec 18, 4:30 pm, "Will Shaver" <[email protected]> wrote:
> If I understand your question, you want to know why you can't change your
> entity directly inside of the OnFlushDirty or OnSave overrides inside your
> Interceptor.
> If you review the code that calls the Interceptor you will understand this
> well enough, but I will explain briefly. Before the Interceptor is called,
> all of the data members from your entity are copied out into the
> []currentState. The state of the entity as it was loaded from the database
> is in the []previousState. At the end of the Interceptor calls, the
> []currentState is copied out into the database.
>
> If you wish to make changes to your entity directly I recommend you use
> event listeners instead of the Interceptor. For example:
>
> var aud = new AuditableSaveEventListener();
> Configuration cfg = ...
> cfg.EventListeners.SaveEventListeners = new ISaveOrUpdateEventListener[] {
> aud };
> cfg.EventListeners.SaveOrUpdateEventListeners = new
> ISaveOrUpdateEventListener[] { aud };
>
> class AuditableSaveEventListener : DefaultSaveEventListener
>     {
>         protected override object PerformSaveOrUpdate(SaveOrUpdateEvent
> @event)
>         {
>             IAuditable entity = @event.Entity as IAuditable;
>             if(entity != null) entity.Audit();
>             return base.PerformSaveOrUpdate(@event);
>         }
>    }
>
>  public interface IAuditable
>     {
>         void Audit();
>     }
>
> On Thu, Dec 18, 2008 at 4:18 AM, Stefan Sedich <[email protected]>wrote:
>
>
>
> > I did think of this option but like to keep this all in my application
> > the solution works nicely and I think I will stick with it for now.
>
> > Cheers
> > Stefan
>
> > On Thu, Dec 18, 2008 at 9:17 PM, Fabio Maulo <[email protected]> wrote:
> > > In this case I prefer a <generated> value by a trigger.
>
> > > 2008/12/18 Stefan Sedich <[email protected]>
>
> > >> Hello,
>
> > >> I am setting date last modified audit on an entity, I implement an
> > >> IAuditableEntity interface and use a helper to set the date modified:
>
> > >>  public override bool OnFlushDirty(object entity, object id, object[]
> > >> currentState, object[] previousState, string[] propertyNames,
> > >> global::NHibernate.Type.IType[] types) {
>
> > >>            if
> > >> (typeof(IAuditableEntity).IsAssignableFrom(entity.GetType())) {
>
> > >>                // If entity is auditable as this is only an update
> > >> set the last modified date.
> > >>                SetValue(propertyNames, currentState, item =>
> > >> item.LastModified, DateTime.Now);
>
> > >>            }
>
> > >>            return false;
> > >>        }
>
> > >> Type safe helper:
>
> > >>  public void SetValue<T>(string[] propertyNames, object[] state,
> > >> Expression<System.Func<IAuditableEntity, T>> propertyExpression, T
> > >> value) {
>
> > >>            var memberExpression = propertyExpression.Body as
> > >> MemberExpression;
> > >>            if (memberExpression == null)
> > >>                throw new ArgumentException("The member expression was
> > >> not a valid member expression.");
>
> > >>            string name = memberExpression.Member.Name;
> > >>            int index = propertyNames.ToList().IndexOf(name);
>
> > >>            if(index == -1)
> > >>                throw new
> > >> InvalidOperationException(string.Format("Property {0} does no exist on
> > >> entity.", name));
>
> > >>            state[index] = value;
>
> > >>        }
>
> > >> Now the question is, I saw that you must set the current state and not
> > >> the entity directly why would
>
> > >> var ent = entity as IAuditableEntity;
> > >> ent.LastModified = DateTime.Now;
>
> > >> Not work properly or will it be fine to do this?
>
> > >> Thanks
>
> > >> --
> > >> Stefan Sedich
> > >> Software Developer
> > >>http://weblogs.asp.net/stefansedich
>
> > > --
> > > Fabio Maulo
>
> > --
> > Stefan Sedich
> > Software Developer
> >http://weblogs.asp.net/stefansedich

I think I should use NH Events instead but right now I am using an
Interceptor via inheriting from EmptyInterceptor.
I am overriding OnFlushDirty to hook up on entity Update and OnSave
for an entity Save.
I have an integration test that IIRC loads an entity (IAuditable) and
then I create a copy of the record being saved via a different
ISession and I noticed that for an Update operation OnSave gets called
and then OnFlushDirty.
Am I right it should be one or the other and not both ?
Or shall I instead use NH Events to know for sure whether it's a Save
or Update ?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to