Perhaps use an interceptor, this works for sure
using System;
using System.Threading;
using NHibernate;
using NHibernate.Type;
using Objective.Core.Model;
/// <summary>
/// The nhibernate interceptor for audit fields.
/// </summary>
public class AuditInterceptor : EmptyInterceptor
{
#region Public Methods (2)
/// <summary>
/// Called when [flush dirty].
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="id">The id.</param>
/// <param name="currentState">State of the current.</param>
/// <param name="previousState">State of the previous.</param>
/// <param name="propertyNames">The property names.</param>
/// <param name="types">The types.</param>
/// <returns></returns>
public override bool OnFlushDirty(object entity, object id,
object[] currentState, object[] previousState, string[] propertyNames,
IType[] types)
{
if (entity is IModificationAuditable)
{
bool changed = false;
for (int i = 0; i < propertyNames.Length; i++)
{
if ("LastChangedBy".Equals(propertyNames[i]))
{
currentState[i] =
Thread.CurrentPrincipal.Identity.Name;
changed = true;
}
if ("LastChangedOn".Equals(propertyNames[i]))
{
currentState[i] = DateTime.Now;
changed = true;
}
}
if (changed) return true;
}
return base.OnFlushDirty(entity, id, currentState,
previousState, propertyNames, types);
}
/// <summary>
/// Called when [save].
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="id">The id.</param>
/// <param name="state">The state.</param>
/// <param name="propertyNames">The property names.</param>
/// <param name="types">The types.</param>
/// <returns></returns>
public override bool OnSave(object entity, object id, object[]
state, string[] propertyNames, IType[] types)
{
if (entity is ICreationAuditable || entity is
IModificationAuditable)
{
bool changed = false;
string actor = Thread.CurrentPrincipal.Identity.Name;
DateTime actedOn = DateTime.Now;
for (int i = 0; i < propertyNames.Length; i++)
{
if ("CreatedBy".Equals(propertyNames[i]))
{
state[i] = actor;
changed = true;
continue;
}
if ("CreatedOn".Equals(propertyNames[i]))
{
state[i] = actedOn;
changed = true;
continue;
}
if ("LastChangedBy".Equals(propertyNames[i]))
{
state[i] = actor;
changed = true;
continue;
}
if ("LastChangedOn".Equals(propertyNames[i]))
{
state[i] = actedOn;
changed = true;
continue;
}
}
if (changed) return true;
}
return base.OnSave(entity, id, state, propertyNames,
types);
}
#endregion Public Methods
}
On Jan 14, 2:55 pm, epitka <[email protected]> wrote:
> I think I had simmilar issue with listener but I took a route of not
> inheriting but doing something like this
>
> public class SaveOrUpdateEventListener : ISaveOrUpdateEventListener
> {
>
> public void OnSaveOrUpdate(SaveOrUpdateEvent @event)
> {
> IDomainObject o = @event.Entity as IDomainObject;
>
> if (o != null)
> {
> o.OnSaveOrUpdate();
> }
>
> DefaultSaveOrUpdateEventListener el = new
> DefaultSaveOrUpdateEventListener();
>
> el.OnSaveOrUpdate(@event);
> }
> }
>
> On Jan 12, 8:46 pm, "Karron Qiu" <[email protected]> wrote:
>
>
>
> > Is there a way to detect that is an update or save operation in
> > DefaultSaveOrUpdateEventListener?
>
> > On Tue, Jan 13, 2009 at 10:30 AM, Stefan Sedich <[email protected]>
> > wrote:
>
> > > This works:
>
> > > public class AuditingEventListener : DefaultSaveEventListener {
>
> > > protected override object PerformSave(object entity, object
> > > id, global::NHibernate.Persister.Entity.IEntityPersister persister,
> > > bool useIdentityColumn, object anything, IEventSource source, bool
> > > requiresImmediateIdAccess) {
> > > var ent = entity as IAuditableEntity;
>
> > > if (ent != null) {
> > > ent.LastModified = DateTime.Now;
> > > ent.CreatedDate = DateTime.Now;
> > > }
>
> > > return base.PerformSave(entity, id, persister,
> > > useIdentityColumn, anything, source, requiresImmediateIdAccess);
> > > }
>
> > > protected override object
> > > PerformSaveOrUpdate(SaveOrUpdateEvent @event) {
> > > var ent = @event.Entity as IAuditableEntity;
>
> > > if (ent != null) {
> > > ent.LastModified = DateTime.Now;
> > > }
>
> > > return base.PerformSaveOrUpdate(@event);
> > > }
>
> > > }
>
> > > Hacky but I don't have the time to work this out for now :\.
>
> > > Cheers
>
> > > On Tue, Jan 13, 2009 at 11:28 AM, Stefan Sedich <[email protected]>
> > > wrote:
> > >> Thanks will give it a look. Is there any reason why using default will
> > >> not work?
>
> > >> Cheers
> > >> Stefan
>
> > >> On Tue, Jan 13, 2009 at 11:17 AM, Dario Quintana
> > >> <[email protected]> wrote:
> > >>> Hi
>
> > >>> Maybe you are looking for IPreInsertEventListener and
> > >>> IPreInsertEventListener.
>
> > >>> Best regards
>
> > >>> On Tue, Jan 13, 2009 at 12:11 AM, Stefan Sedich
> > >>> <[email protected]>
> > >>> wrote:
>
> > >>>> Hi,
>
> > >>>> I created a custom event listener like so:
>
> > >>>> public class AuditingEventListener : DefaultSaveEventListener {
>
> > >>>> protected override object PerformSave(object entity, object
> > >>>> id, global::NHibernate.Persister.Entity.IEntityPersister persister,
> > >>>> bool useIdentityColumn, object anything, IEventSource source, bool
> > >>>> requiresImmediateIdAccess) {
> > >>>> var ent = entity as IAuditableEntity;
>
> > >>>> if (ent != null) {
> > >>>> ent.LastModified = DateTime.Now;
> > >>>> ent.CreatedDate = DateTime.Now;
> > >>>> }
>
> > >>>> return base.PerformSave(entity, id, persister,
> > >>>> useIdentityColumn, anything, source, requiresImmediateIdAccess);
> > >>>> }
>
> > >>>> protected override void PerformUpdate(SaveOrUpdateEvent
> > >>>> @event, object entity,
> > >>>> global::NHibernate.Persister.Entity.IEntityPersister persister) {
> > >>>> var ent = @event.Entity as IAuditableEntity;
>
> > >>>> if (ent != null) {
> > >>>> ent.LastModified = DateTime.Now;
> > >>>> }
>
> > >>>> base.PerformUpdate(@event, entity, persister);
> > >>>> }
>
> > >>>> }
>
> > >>>> And config like so:
>
> > >>>> <listener class="App.AuditingEventListener, App" type="save" />
> > >>>> <listener class="App.AuditingEventListener, App" type="update" />
>
> > >>>> Now the save is getting called on insert, but update is not getting
> > >>>> called, if I add
>
> > >>>> protected override object PerformSaveOrUpdate(SaveOrUpdateEvent
> > >>>> @event) {
> > >>>> var ent = @event.Entity as IAuditableEntity;
>
> > >>>> if (ent != null) {
> > >>>> ent.LastModified = DateTime.Now;
>
> > >>>> }
>
> > >>>> return base.PerformSaveOrUpdate(@event);
> > >>>> }
>
> > >>>> this is getting called on save and updates, how can I get the update
> > >>>> only listener to be called? Am I missing something simple here.
>
> > >>>> Thanks
>
> > >>>> --
> > >>>> Stefan Sedich
> > >>>> Software Developer
> > >>>>http://weblogs.asp.net/stefansedich
>
> > >>> --
> > >>> Dario Quintana
> > >>>http://darioquintana.com.ar
>
> > >> --
> > >> Stefan Sedich
> > >> Software Developer
> > >>http://weblogs.asp.net/stefansedich
>
> > > --
> > > Stefan Sedich
> > > Software Developer
> > >http://weblogs.asp.net/stefansedich
>
> > --
> > Regards,
> > Karron- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---