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 20, 2014 · 13 revisions

Getting Started

In order to use the WebApi extension for the MicroLite ORM framework, you need to reference it in your solution. The easiest way to do this is install it via NuGet (if you are unfamiliar with NuGet, it's a package manager for Visual Studio - visit nuget.org for more information).

Install-Package MicroLite.Extensions.WebApi

Configuring the Extension

In the WebApiConfig class in the App_Start folder of your WebApi project, register the action filter(s) (only add the ones you want based upon the behaviour they introduce):

public static void Register(HttpConfiguration config)
{
    ...

    // Add the MicroLite filters in the following order (for the ones you choose to use)
    config.Filters.Add(new ValidateModelNotNullAttribute());
    config.Filters.Add(new ValidateModelStateAttribute());
    config.Filters.Add(new MicroLiteSessionAttribute("ConnectionName"));
    config.Filters.Add(new AutoManageTransactionAttribute());
}

See the following pages for further details about the attributes:

Create a MicroLiteConfig class in your App_Start folder using the following template:

public static class MicroLiteConfig
{
    public static void ConfigureConnection()
    {
        // Load any MicroLite extensions
        Configure
            .Extensions() // if you are also using a logging extension, register it first
            .WithWebApi();
    }

    public static void ConfigureExtensions()
    {
        // Create session factories for any connections
        Configure
            .Fluently()
            .For...Connection("ConnectionName")
            .CreateSessionFactory();
    }
}

Call the MicroLiteConfig from the application start method in your Global.asax:

protected void Application_Start()
{
    // Register routes, bundles etc, then:
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    ...

    // Always configure extensions before connections.
    MicroLiteConfig.ConfigureExtensions();
    MicroLiteConfig.ConfigureConnection();
}

Configure your Controllers

The last thing to do is configure your controllers so that the extension can provide a session and manage the transaction for you.

See the Controllers page for further details.

OData Support

The WebApi extension also provides support for OData queries, see the OData page for further details.

Clone this wiki locally