Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
Trevor Pilley edited this page May 22, 2013 · 13 revisions

MicroLite.Extensions.WebApi is an extension to the MicroLite ORM Framework which allows simple integration of MicroLite within an ASP.NET WebApi application. It can be installed via NuGet Install-Package MicroLite.Extensions.WebApi.

You can then load the extension in your application start-up:

protected void Application_Start()
{
    ... // common start-up, register routes etc.

    // Load extensions first
    Configure
       .Extensions() // If you are also using a logging extension, that should be loaded first.
       .WithWebApi();

    // Then create the session factory...
    Configure
       .Fluently()
       ...
}

When .WithWebApi() is called to load the extension, a MicroLiteSessionAttribute will be created with all default values and added to GlobalConfiguration.Configuration.Filters. This saves you having to apply it to each controller or action where you want an ISession/IReadOnlySession to be configured. If you do not want this to happen, use the overload .WithWebApi(registerGlobalFilter: false).

The extension includes 2 controllers, MicroLiteApiController and MicroLiteReadOnlyApiController which define an ISession or IReadOnlySession property called Session.

public class CustomerApiController : MicroLiteApiController // This will provide an ISession
{
    ...
}

public class OrderApiController : MicroLiteReadOnlyApiController // This will provide an IReadOnlySession
{
    ...
}

If you don't want to or can't for some reason inherit from the MicroLite controllers, you can implement MicroLite.Infrastructure.IHaveSession or MicroLite.Infrastructure.IHaveReadOnlySession directly.

The MicroLiteSessionAttribute is an ActionFilterAttribute which specifies the connection name and transaction behavior. It will ensure that a new ISession/IReadOnlySession 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 have a MicroLiteSessionAttribute registered in GlobalConfiguration.Configuration.Filters (either by .WithWebApi() or manually added), you can still override its behavior for a specific controller or action by applying the attribute directly to it. For example, if you want to opt out of the auto transaction management for an action, simply initialize the attribute and set the property to false.

[MicroLiteSession(AutoManageTransaction = false)]
public HttpResponseMessage Post(Order order)
{
    ...
}

Check out the WebApi posts on the MicroLite Blog for further information.

Clone this wiki locally