logo

PostFlush listeners of NHibernate events

In this article, we will show you how to create postFlush event listeners from PostFlushEventListener extension point. We will create listeners of 3 events:

  1. Document creation. We will record the creation author and creation date in the Description attribute;
  2. Document editing. We will record the user, who edited the document and the date of editing in the Description attribute;
  3. Editing of Phone If a new phone number contains invalid symbols, we will remove them from the phone string.

PostFlush event occurs when a transaction (UnitOfWork) is committed in ELMA. E.g. when you click Complete Task button, the transaction is committed. Before committing a transaction, ELMA calls Session.Flush() method and sends data to the database. Thus, when you work with postFlush events, you cannot cancel the event – this is the main difference of PostFlush events compared to Pre events from IEntityEventsListeners extension point. You should also note that PostFlush events occur later than events from IEntityEventsListener

Attention! 
We recommend creating a backup copy of the database before implementing the extension point.

Members of PostFlushEventListener extension point

/// <summary>
/// After creating objects
/// </summary>
/// <param name="event">Event parameters</param>
public virtual void OnPostInsert(PostInsertEvent @event)
 
/// <summary>
/// After editing objects
/// </summary>
/// <param name="event">Event parameters</param>
public virtual void OnPostUpdate(PostUpdateEvent @event)
 
/// <summary>
/// After deleting objects
/// </summary>
/// <param name="event">Event parameters</param>
public virtual void OnPostDelete(PostDeleteEvent @event)
 
/// <summary>
/// After editing collection
/// </summary>
/// <param name="event">Event parameters</param>
public virtual void OnPostUpdateCollection(PostCollectionUpdateEvent @event)

Example of implementation

In our case, the implementation of PostFlushEventListener extension point is:

[Component]
    class PostFlushEvents : PostFlushEventListener
    {
        // 'Document creation' event
        public override void OnPostInsert (PostInsertEvent @event)
        {
            // Check document type
            var doc = @event.Entity as IFax;
            if (doc != null)    //   If a document has 'Fax' type
            {
                // Fill in the 'Description' attribute
                doc.Description = string.Format("Creation author: {0}\r\n", doc.CreationAuthor.FullName);
            }
        }

        // 'Document editing' event
        public override void OnPostUpdate (PostUpdateEvent @event)
        {
            // Check document type
            var doc = @event.Entity as IFax;
            if (doc != null)    //   If a document has 'Fax' type
            {
                doc.EditedBy = string.Format("{0} at {1}", doc.ChangeAuthor, DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\r\n");
            }
        }

        // 'Collection editing' event
        public override void OnPostUpdateCollection (PostCollectionUpdateEvent @event)
        {
            var collection = @event.Collection;
            var collectionEntry = @event.Session.PersistenceContext.GetCollectionEntry(@event.Collection);
            var collectionEntries = collection.Entries(collectionEntry.LoadedPersister);

            var listChars = new List<string> { " ", "-", "(", ")" }; // List of symbols that we want to remvoe from phone strings
            foreach (var entry in collectionEntries)
            {
                if (!(entry is IPhone))
                    continue; // If collection items have IPhone type
                var phone = entry as IPhone;
                var lastNumber = phone.PhoneString;
                foreach (var str in listChars)
                {
                    lastNumber = lastNumber.Replace(str, string.Empty); // Remove invalid symbols 
                }
                (entry as IPhone).PhoneString = lastNumber; // Save the formatted phone string
            }
        }
    }

As a result, we will see the following:

Links to API elements

PostFlushEventListener

Attachments