blog

Setup Autofac and AutoMapper in .NET 5 Web project

Save time and enhance code efficiency with Autofac and AutoMapper in your .NET web projects.

Posted: 1/12/2021

Autofac is a powerful and very popular Inversion of Control container for .NET platform. AutoMapper, on the other hand, is a library that solves the problem of highly repetitive and junk code mapping one object to another. One of the first things to do after creating a new .NET web project is to add both of these libraries to it. In this tutorial I will show you how to setup both of these tools in .NET 5 web project which hopefully save your time during next project creation.

Autofac

At first, you need to install Autofac packages for your project. To do that, RMB on your project in Solution Explorer view, choose Manage NuGet packages… and search for Autofac and Autofac.Extensions.DependencyInjection

Autofac packages in NuGet package manager

Then it is required to add AutofacServiceProvider to .NET Core hosting mechanism in Program.cs:

public static IHostBuilder CreateHostBuilder(string\[\] args) =>
    Host.CreateDefaultBuilder(args)
        .UseServiceProviderFactory(new AutofacServiceProviderFactory())
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Now it is time for ContainerModule class to be created. It will be used for registering all types and modules. It should inherit from Autofac.Module and override Load method.

public class ContainerModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        // Register types & modules
    }
}

For example, to register all types implementing IService interface you can use below code snippet:

var assembly = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(assembly)
       .AssignableTo<IService>()
       .AsImplementedInterfaces()
       .InstancePerLifetimeScope();

Last but not least - create new ConfigureContainer method in Startup.cs. This method registers previously created module and will be implicitly invoked by Autofac. Thanks to this you can access the Autofac ContainerBuilder and register things directly with it (like in the module above).

public void ConfigureContainer(ContainerBuilder builder)
{
    builder.RegisterModule(new ContainerModule());
}

AutoMapper

Same as Autofac, AutoMapper requires installation of two NuGet packages - base (AutoMapper) and dependency injection extensions (AutoMapper.Extensions.Microsoft.DependencyInjection).

AutoMapper packages in NuGet package manager

The next step is to build the mapping profile. This is a special class where all mappings between models and DTOs should be defined. I recommend using ReverseMap() method, which automatically creates also a reverse mapping to the defined one.

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<TestModel, TestModelDto>().ReverseMap();
    }
}

Now it’s time to add class which will create mapper based on the mapping profile built in the previous step. This method will be used later for registering IMapper in IoC container, so I recommend creating static method as below.

public class AutoMapperConfig
{
    public static IMapper Initialize()
    {
        var mapperConfig = new MapperConfiguration(mc =>
        {
            mc.AddProfile(new MappingProfile());
        });
        return mapperConfig.CreateMapper();
    }
}

In this example I’m adding mapping profile to MapperConfiguration, but there’s no problem with creating the mapping here explicitly (instead using special MappingProfile class).

Last thing to do is to register instance of just built AutoMapperConfig in the Autofac IoC container. Just add the code snippet in the ContainerModule.cs file.

builder.RegisterInstance(AutoMapperConfig.Initialize()).SingleInstance();

And that’s it! Both Autofac and AutoMapper are ready to work.

If you already use Dependency Injection in your project (e.g. built-in Microsoft.Extensions.DependencyInjection) but want to replace it with Autofac, you can use this commit as real-life example how to do it - Add Autoface IoC · kubawajs/WeddingPlanner@c7fd52d (github.com)

Sources


Hero photo by Annie Spratt on Unsplash