WebApi 3.0 MicroLiteApiController update

MicroLiteApiController<TEntity, TId>

It now contains a new controller MicroLiteApiController<TEntity, TId>.

There are 2 generic args, one is to specify the entity type, the other the type used by the identifier property (usally int but MicroLite supports other identifier types).

The new controller supports Get, Post, Put and Delete on an opt-in basis. The methods are all declared protected, so in order to use them you need to create a public method which calls the protected one. The reasons for this are to make the functionality opt-in not opt-out which is safer and it works nicely with the WebApi help extension.

In order to use the new controller, simply create a custom controller which inherits from it and provides the type which the controller will deal with.

For example, if we have a customer class we can create a CustomerApiController:

public class CustomerApiController : MicroLiteApiController<Customer, int>
{
     ...
}

The methods all return HttpResponseMessage, the base controller is responsible for setting the HttpStatus code (e.g. 200, 404) depending on the request type and whether the request is successful.

To enable a HTTP action, simply implement the public version of the method and call the protected version.

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);
}

The MicroLiteApiController&lt;TEntity, TId> class can be used to reduce the amount of boiler plate repetitive code you need to write in order to create WebApis.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s