This is an implmentation I applied to a Web API 2 project, in NET Framework 4.7.2, but despite how old it may look, you can use it too.
Initialization
Notice second line. That's were you'll initialize the Autofact static class.
// Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
AutofacConfig.RegisterComponents(); // IoC, AUTOFAC
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Autofact Configuration
- This is the whole configuration file for Autofac. It is a static class.
- Notice the
#region MAPPING
// AutofacConfig.cs
public static class AutofacConfig
{
public static void RegisterComponents()
{
var builder = new ContainerBuilder();
// Get your HttpConfiguration.
var config = GlobalConfiguration.Configuration;
// Register your Web API controllers.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// OPTIONAL: Register the Autofac filter provider.
builder.RegisterWebApiFilterProvider(config);
// OPTIONAL: Register the Autofac model binder provider.
builder.RegisterWebApiModelBinderProvider();
#region MAPPING
// 1 - Mapper configuration is done here
// Notice the AutoMapperProfile instantiation
var mapperConfiguration = new MapperConfiguration(cfg => { cfg.AddProfile(new AutoMapperProfile()); });
// 2 - After configuring the mapper, you create it
var mapper = mapperConfiguration.CreateMapper();
// 3 - Register that instance of the mapper, which implements IMapper interface
builder.RegisterInstance<IMapper>(mapper);
// 4 - Now you register the TestMapper that you'll inject somewehre in your code, let's say, in a controller
// Register as many mappers as you need
builder.Register(c => new TestMapper(c.Resolve<IMapper>())).As<ITestMapper>();
#endregion
// Set the dependency resolver to be Autofac.
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
}
Automapper Profile
- As you can see, this class extends
Profile
from AutoMapper
namespace.
- Here you will create all the mapping you may need. This is the actual configuration for
AutoMapperProfile
we did above.
- This is the file you call from
AutofacConfig.cs
// AutoMapperProfile.cs
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<Test, TestDto>()
.ForMember(destination => destination.Floating, options => options.MapFrom(source => source.flotante))
.ForMember(dto => dto.Integer, opt => opt.MapFrom(src => src.entero))
;
// Test is a model/entity from the DB
// TestDto is a class I use as a Data Transfer Object
}
}
TestMapper
- Here's where you inject the mapper into
TestMapper
.
ITestMapper
is your custom definition.
// TestMapper.cs
public class TestMapper : ITestMapper
{
IMapper _mapper;
public TestMapper(IMapper mapper)
{
_mapper = mapper;
}
public TestDto GetTestItem(Test test)
{
return _mapper.Map<TestDto>(test);
}
// You may add additional implementation here.
}
Use Example
- Now you can inject
TestMapper
as ITestMapper
wherever you may need it.
- Notice this is an
ApiController
inheritor.
// TestController.cs
[RoutePrefix("api/test")]
public class TestController : ApiController
{
private ITestMapper _testMapper;
public TestController(, ITestMapper testMapper)
{
_testMapper = testMapper;
}
[HttpGet]
[Route("GetTestItem/{id:int}")]
// GET api/<controller>/GetTestItem/5
public Prueba GetTestItem(int id)
{
var itemDb = _testRepository.GetTestItem(id); // it could be a service
var itemDto = _testMapper.GetTestItem(itemDb)
// Return the mapped object
return itemDto;
}
}