WebApi Extension 3.0.0 Released

MicroLite.Extensions.WebApi 3.0.0 has been released on NuGet.

Configuration and Filters

In WebApi Extension 2.1 we added the ability for the extension to register the MicroLiteSession attribute globally to save you having to apply it to each controller or action which you wanted an ISession/IReadOnlySession opening for. This was done by passing a boolean value for registerGlobalFilter to the WithWebApi configuration extension.

The MicroLiteSession attribute has been moved into the Filters namespace and 2 more filters have been added, one to verify Api method parameters are not null and another to validate them if they are decorated with DataAnnotation attributes.

The configuration of the extension has also been updated, it is now required to specify a WebApiConfigurationSettings class. The WebApiConfigurationSettings class currently exposes 3 properties:

RegisterGlobalMicroLiteSessionAttribute - defaults to true
RegisterGlobalValidateModelNotNullAttribute - defaults to true
RegisterGlobalValidateModelStateAttribute - defaults to true

If you are happy to use the defaults, you can simply tell MicroLite to use the default settings:

Configure.Extensions().WithWebApi(WebApiConfigurationSettings.Default);

otherwise, simply specify the values you wish to use:

Configure.Extensions().WithWebApi(new WebApiConfigurationSettings
    {
        RegisterGlobalMicroLiteSessionAttribute = false
    });

Registering these filters at a global level means that they are called for every action on every controller and you don’t have to have the following at the start of every method on every controller.

public void Post(Customer customer)
{
     if (customer == null)
     {
          throw new ArgumentNullException("customer");
     }

     if (!this.ModelState.IsValid)
     {
          return this.Request.CreateResponse(HttpStatusCode.BadRequest, this.ModelState);
     }
}

Remember that even though the MicroLiteSession attribute is registered globally, you can still override its default behaviour on a per controller or per action basis by applying the attribute to a class/method. This is probably something you don’t need to do very often and can be left to the infrastructure to open and assign a session for your controller and begin/commit a transaction around each action.

1 thought on “WebApi Extension 3.0.0 Released

  1. Pingback: WebApi 3.0 OData Support | MicroLite ORM

Leave a comment