-
Notifications
You must be signed in to change notification settings - Fork 1
Home
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
.
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(WebApiConfigurationSettings.Default);
// Then create the session factory...
Configure
.Fluently()
...
}
If you don't want to use the defaults, set the properties on WebApiConfigurationSettings explicitly
.WithWebApi(new WebApiConfigurationSettings { ... });
The WebApiConfigurationSettings
class exposes a number of properties which help define the behavior of the extension.
- RegisterGlobalMicroLiteSessionAttribute - indicates whether to register a
MicroLiteSessionAttribute
in theGlobalConfiguration.Configuration.Filters
is one is not already registered (defaults to true). - RegisterGlobalValidateModelNotNullAttribute - indicates whether to register a
ValidateModelNotNullAttribute
in theGlobalConfiguration.Configuration.Filters
is one is not already registered (defaults to true). - RegisterGlobalValidateModelStateAttribute - indicates whether to register a
ValidateModelStateAttribute
in theGlobalConfiguration.Configuration.Filters
is one is not already registered (defaults to true).
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
{
...
}
In version 3 of the extension, an Entity controller was added. This adds CRUD capabilities with minimal boiler plate code. Simply inherit from the new controller and add public methods to opt-in to whichever actions you wish the controller to have.
public class CustomerApiController : MicroLiteApiController<Customer, int>
{
public HttpResponseMessage Delete(int id)
{
return this.DeleteEntityResponse(id);
}
public HttpResponseMessage Get(int id)
{
return this.GetEntityResponse(id);
}
public HttpResponseMessage Post(Customer entity)
{
return this.PostEntityResponse(entity);
}
public HttpResponseMessage Put(int id, Customer entity)
{
return this.PutEntityResponse(id, entity);
}
}
For further info, visit the Blog Post.
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)
{
...
}
In version 3 of the extension, support for OData queries was added. There is a new Controller MicroLiteODataApiController<TEntity, TId>
which adds OData support to the
MicroLiteApiController<TEntity, TId>
we mentioned above.
Again, this is an opt-in. Check the OData Support and OData Filtering blog posts for further information.
Check out the WebApi posts on the MicroLite Blog for further information.