-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Welcome to the MicroLite.Extensions.WebApi wiki!
MicroLite.Extensions.Mvc is an extension to the MicroLite ORM Framework which allows simple integration of MicroLite within an ASP.NET WebApi application.
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 MicroLiteApiController
.
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)]