Listeners are fairly easy to implement. Probably you want to do this by
overloading the DefaultSaveEventListener


class AuditableSaveEventListener : DefaultSaveEventListener
{
 protected override object PerformSaveOrUpdate(SaveOrUpdateEvent @event)
 {
  IAuditable entity = @event.Entity as IAuditable;
  if(entity != null) entity.Audit();
   return base.PerformSaveOrUpdate(@event);
 }
}
Then use as follows:
AuditableSaveEventListener auditableSaveEventListener = new
AuditableSaveEventListener();
configuration.EventListeners.SaveEventListeners = new
ISaveOrUpdateEventListener[] { auditableSaveEventListener };
configuration.EventListeners.SaveOrUpdateEventListeners = new
ISaveOrUpdateEventListener[] { auditableSaveEventListener };

It is important to specify it for both save and saveorupdate, that hung me
up for a bit. :)

You can make modifications to the entity in ths function and they will be
persisted. I think that if you need to see the changes you can get
@event.Entry.LoadedState

 -Will


On Tue, Oct 7, 2008 at 8:15 AM, jonnii <[EMAIL PROTECTED]> wrote:

>
> By looking through the code it seems I could maybe add some listeners
> to
>
> Session.Listeners.PostInsertEventListeners etc...
>
> Does this look reasonable?
>
> On Oct 7, 11:03 am, jonnii <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I'm trying to move from a custom in-house ORM to NHibernate, but have
> > hit one small snag.  We currently have something like this (this is a
> > very simplified example):
> >
> > public class User{
> >
> >    public class Lifecycle : DefaultLifecycle<User> {
> >       public override void OnAfterSave(User u)
> > { MailService.SendWelcomeMail(u); }
> >    }
> >
> > }
> >
> > The way the current system works is that it analyses the model graph
> > and works out the change set, validates the objects, runs the before
> > lifecycle events, saves the objects, then runs the after lifecycle
> > events.
> >
> > I've been experimenting with shoe-horning this feature into nhibernate
> > by using a custom IInterceptor, but that only gets me so far.
> >
> > So what I'm thinking is wrapping up access to the ISession and running
> > the events myself, but to do that I need to be able to be able to work
> > out what is about to be flushed, and because these lifecycle events
> > can modify the object graph I also need to be able to work out if the
> > objects to be flushed has changed, and if so what has changed.
> >
> > Is this possible?
> >
>

--~--~---------~--~----~------------~-------~--~----~
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