Best Practice in Creating Web API Services

25. April 2017 Web API 0

This is the second part of Web API Series. In the first part, we’ve learnt how to create a RESTful API with Web API. In this tutorial, we gonna learn how we can enhance the code and provides more flexibility and simplicity to the code.


The following code snippet is the implementation of our Web API service:

As you can see there all the methods are repeating the same thing. They all call a business method and return the result along with the status code. They also check for any exception and wrap the exception in the response object. The repeated task can be delegated to a base class which accepts an action, performs it and returns the result.

Download the EmployeeManagement project if you don’t have it already.

Create a Service Base Class

Let’s create the base class. In the EmployeeManagement.Service, right click on the Controller folder, Add, and then select Class. Name it ServiceBase.cs.

This class will be all services’ base class so it needs to inherit from ApiContoller.

public class ServiceBase : ApiController

We have two types of actions: those which return a result (like Get(), List() or etc. methods), and those which just perform an action (like Update() or Remove() methods). For this purpose, we need to create two methods in our base class. One to get a Func<> as parameter to service those methods returning a result and another one to get an Action as parameter which calls void methods.

Below is the implementation of these two methods:

The first method take Func<TResult> as parameter. In the method’s body, we call the action and hold the result in a variable. We then wrap the result in response object with the optional statusCode value (set to OK by default). The second method gets an Action rather than the Func<>. It does the same thing, except, the action yields no results. So we are just calling the action and return the status code as a response. Both methods wrap the execution in try-catch block and returns InternalServerError along with the exception.

Modify the Web API Methods

Next, we need to modify our Web API methods to use the base class. Remove the inheritance from ApiController and instead inherit from ServiceBase class.

public class EmployeeController : ServiceBase

Modify the service operations as below:

You can see how our service implementation became much simpler, in fact, just one line.

We only pass the second parameter, i.e. status code, when the status code we want to return is something else than OK (200). For example, HttpStatusCode.Accepted (202) for Update and Delete operations, and HttpStatusCode.Created (201) for Create.


Download the sample code

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.