Home > Azure, C#, Mobile Services > Using Dependency Injection in Mobile Services .NET Backend

Using Dependency Injection in Mobile Services .NET Backend

24/03/2014

The .NET Backend of Mobile Services makes use of Dependency Injection through the Autofac library. So, we can configure it by adding our dependencies, just like we would do in any other .NET application.

Suppose we have the following interface and one possible implementation:

public interface ILocationService
{
    Task<string> ReverseGeocodeAsync(double latitude, double longitude);
}

public class BingLocationService : ILocationService
{
    public Task<string> ReverseGeocodeAsync(double latitude, double longitude)
    {
        // ...
        return Task.FromResult("<value>");
    }
}

We want to pass it to our controllers using Inversion of Control. So, let’s open the WebApiConfig.cs file in the App_Start folder. First of all, we need to add the using Autofac; statement at the top of the file, because we need some extension methods that are defined within this namespace.

Then, locate the following line in the Register method:

// Use this class to set WebAPI configuration options
HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));

The ConfigBuilder constructor has an overload accepting an Action that receives the Inversion of Control container as argument. So, we can use it do add our dependency:

HttpConfiguration config = 
    ServiceConfig.Initialize(new ConfigBuilder(options, (container) =>
    {
        container.RegisterType<BingLocationService>().As<ILocationService>();
    }));

Now the dependcy is registered in the container, and so we can use it as usual:

public class LocationController : ApiController
{
    private ILocationService locationService;

    public LocationController(ILocationService locationService)
    {
        this.locationService = locationService;
    }

    public async Task<string> GetAddress(double latitude, double longitude)
    {
        var address = await locationService.ReverseGeocodeAsync(latitude, longitude);
        return address;
    }
}

If we prefer a more structured solution, instead of defining all the configurations within the Register method, we can also create a class that inherits from ConfigBuilder and overrides its ConfigureServiceDependencies method:

public class MyCustomConfigBuilder : ConfigBuilder
{
    public MyCustomConfigBuilder(ConfigOptions options) 
        : base(options)
    { }

    protected override void ConfigureServiceDependencies(HttpConfiguration config, 
        ContainerBuilder containerBuilder)
    {
        containerBuilder.RegisterType<BingLocationService>().As<ILocationService>();

        base.ConfigureServiceDependencies(config, containerBuilder);
    }
}

The last thing to do is to tell the application that we want to use our custom ConfigBuilder:

// Use this class to set WebAPI configuration options
HttpConfiguration config = ServiceConfig.Initialize(new MyCustomConfigBuilder(options));

The Autofac site contains a lot of documentation and example on how to use it, so we can refer to it for more information.

Categories: Azure, C#, Mobile Services
  1. 25/03/2014 at 05:34

    As they say, Autofac is addictive. Easy to configure. Have you explored auto-resolve in Autofac? Recently I had been involved in auto resolving dependencies via Windsor. Found it impressive.

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: