Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
TrevorPilley edited this page Nov 23, 2012 · 13 revisions

Welcome to the MicroLite.Extensions.WebApi wiki!

Ok, what is it?

MicroLite.Extensions.Mvc is an extension to the MicroLite ORM Framework which allows simple integration of MicroLite within an ASP.NET WebApi application.

Great! how do I use it?

In order to use the WebApi extensions, you first need to install it via NuGet Install-Package MicroLite.Extensions.WebApi.

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

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

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

    // Create the session factory...
    Configure
       .Fluently()
       ...
}

The extension includes a controller MicroLiteApiController which defines an ISession proeprty called Session. Inherit your controllers from MicroLiteController.

public class CustomerController : MicroLiteApiController
{
    ...
}

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.

[HttpPost]
[MicroLiteSession]
public Customer Put(int id, Customer customer)
{
    ...
}

or

[MicroLiteSession]
public class CustomerController : MicroLiteApiController

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 want to opt out of the auto transaction management, simply initialize the attribute and set the property to false.

[MicroLiteSession(AutoManageTransaction = false)]
Clone this wiki locally