Feather: Integrate Ninject, WebApi, and Feather
Overview
One of the most common patterns in programming is the Inversion of Control (IoC) design pattern. Feather provides a smooth integration of Ninject Dependency Injection (DI) framework and gives you the ability to use the IoC pattern when working with ASP.NET Web API.
The advantages of using the IoC design pattern are the following:
- Reduces class coupling
- Increases code reusability
- Improves code maintainability
- Improves application testability
The following article demonstrates how to use Dependency Injection for the constructor of a Web API controller. To achieve this, you create a custom Web API controller and define its constructor parameters via the Ninject API.
Ninject is a lightweight dependency injection framework for .NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner. By using Ninject to support your software’s architecture, your code becomes easier to write, reuse, test, and modify.
PREREQUISITES: You must have installed Feather 1.4.410.0 or above.
Procedure
- Install the required NuGet packages.
- Open your SitefinityWebApp and install NuGet package
Ninject.Web.WebApi.WebHost
- Update the
Ninject.Web.WebApi
NuGet package to version 3.2.4.0 or higher.
- Create the interface that is used as a contract in the constructor parameter of the Web API controller in the following way:
public interface IAnimalService
{
//define your service methods here
}
- Create a class that will be registered as the actual implementation of the contract interface via the Ninject API in the following way:
public class AnimalService: IAnimalService
{
//implement your service methods here
}
- Create a Web API controller that uses Constructor Dependency Injection pattern.
Perform the following:
- Create a new class, name it PetController, and place it inside folder
~/Mvc/Controllers
in the root of your project.
- Implement the
PetController
class in the following way:
public class PetController : ApiController
{
public PetController(SitefinityWebApp.Mvc.Services.IAnimalService animalService)
{
}
public string Get()
{
return "Rabbit";
}
}
- Register your bindings via the Ninject API.
Perform the following:
- In Visual Studio, open
NinjectWebCommon
class that is located in App_Start/NinjectWebCommon.cs
.
- Add the following binding inside the
RegisterServices
method:
kernel.Bind<
IAnimalService
>().To<
AnimalService
>();
- Register route to the Web API controller.
Perform the following:
- Open the
Global.asax
file
If you do not have a Global.asax
, you must create one.
- Register route to the Web API controller in the following way:
protected void Application_Start(object sender, EventArgs e)
{
Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
}
void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
{
if (e.CommandName == "Bootstrapped")
{
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
- Build your web application.
RESULT: You are able to access your custom Web API controller by requesting localhost/api/Pet
URL in a browser.