Using MicroLite with ASP.NET MVC

It is very easy to use MicroLite within ASP.NET MVC out of the box, however an extension package has been created to make it even easier.

To use it, firstly install the NuGet package Install-Package MicroLite.Extensions.Mvc

Then, in your application startup simply register the extension before creating the session factories.

protected void Application_Start()
    ... // common mvc startup, register routes etc.

    // Load the extension for mvc
       .Extensions() // If you are also using a logging extension, that should be loaded first.

    // Create the session factory...

The extension includes a controller MicroLiteController which defines an ISession proeprty called Session, since the base controller class already defines a Session property, this has been redirected to a property called HttpSession. Inherit your controllers from MicroLiteController.

public class CustomerController : MicroLiteController

The extension also includes an ActionFilterAttribute called MicroLiteSessionAttribute. This can be applied to specific methods on your controller if only certain actions require an ISession or to the controller class if all methods require an ISession.

public ActionResult Edit(int id, FormCollection collection)


public class CustomerController : MicroLiteController

The attribute will ensure that a new ISession is created and assigned to the Session property of the controller before a method is called. It will also begin a transaction. Once the method has completed, it will either commit the transaction or if an exception was thrown within the method, it will roll the transaction back.

If you are using the automatic transaction management, it is important not to swallow exceptions inside your method, otherwise the transaction will not be rolled back. You should use error handler attributes to deal with exceptions which will allow the Mvc extension to properly manage the transaction.

If you want to opt out of the auto transaction management, simply initialize the attribute and set the property to false.

[MicroLiteSession(AutoManageTransaction = false)]

You should then begin and commit/rollback a transaction within the method yourself.

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


6 thoughts on “Using MicroLite with ASP.NET MVC

  1. Pingback: Targeting Multiple Databases | MicroLite ORM

  2. Pingback: Using MicroLite with ASP.NET WebApi | MicroLite ORM

  3. Pingback: Read Only Session | MicroLite ORM

  4. Pingback: MVC and WebApi extensions | MicroLite ORM

  5. Pingback: New Infrastructure Interfaces | MicroLite ORM

  6. Pingback: Unit Testing MVC Controllers | MicroLite ORM

Leave a Reply

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

You are commenting using your 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