Extending MicroLite with custom Listeners

MicroLite provides a number of methods to allow custom code to be called during the internal pipeline, one of these is the IListener interface. The IListener interface is actually used by MicroLite to manage object identifiers (e.g. MsSql Identity).

The IListener interface defines a number of methods which you can implement:

  • void AfterInsert(object instance, object executeScalarResult);
  • void BeforeDelete(object instance);
  • void BeforeDelete(object instance, SqlQuery sqlQuery);
  • void BeforeInsert(object instance);
  • void BeforeInsert(object instance, SqlQuery sqlQuery);
  • void BeforeUpdate(object instance);
  • void BeforeUpdate(object instance, SqlQuery sqlQuery);

To save having empty method implementations in your class, you can inherit from the abstract Listener class which implements the IListener methods as virtual methods so you can just override the ones you need.

Lets say for example, that we have a requirement that some entities should have a created date and a created by property. In order to avoid having to set these properties throughout our application before we call ISession.Insert(), we can create a listener which MicroLite will invoke prior to inserting the record into the database.

Firstly, lets define an interface for our created date and created by:

public interface ICreated
    DateTime Created { get; set; }
    string CreatedBy { get; set; }

We can then create a Listener which can deal with this interface:

public class AuditCreatedListener : MicroLite.Listeners.Listener
    protected override BeforeInsert(object instance)
        // cast the instance safely in case not all entities implement ICreated.
        var created = instance as ICreated;

        if (created != null)
            created.Created = DateTime.Now;
            created.CreatedBy = Environment.UserName;

We can the register the listener, so in your application startup (where you call Configure):

using MicroLite.Listeners;


This will tell MicroLite to invoke your listener.

This article was written for version 2.0.0 of the MicroLite framework.


7 thoughts on “Extending MicroLite with custom Listeners

  1. dcsteele

    Like it !

    Couple of queries…

    Why is there an ‘AfterInsert’ but no ‘AfterDelete’ or ‘AfterUpdate’ ?
    What can realistically be done ‘AfterInsert’ ?

    Could you give an example of the use of SqlQuery, please ?

  2. trevorpilley Post author

    The interface was initially designed so that I could deal with object identifiers so there is an IListener for dealing with the Identity strategy (https://github.com/TrevorPilley/MicroLite/blob/master/MicroLite/Listeners/IdentityListener.cs), that appends the SQL to select the identity value upon insert and then in the AfterInsert method, updates the property value on the inserted object.

    AfterDelete and AfterUpdate would be useful for supporting concurrency checks (e.g. if the row count is 0 then the update/delete failed) however I don’t support any built in concurrency models yet, there will be some in the future but I’m still in the planning phase of that.

  3. Pingback: Listener Performance Tweak | MicroLite ORM

  4. Pingback: Using Logging in Custom Listeners | MicroLite ORM

  5. Pingback: MicroLite 4.0 Released | MicroLite ORM

  6. Pingback: MicroLite Comparison Feedback | MicroLite ORM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s