⚠ Deprecated: Legacy
The functionality of this package is now included directly in AutoMapper.
Suggested alternative: AutoMapper
AutoMapper extensions for ASP.NET Core
$ dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjectionScans assemblies and:
To use, with an IServiceCollection instance and one or more assemblies:
services.AddAutoMapper(assembly1, assembly2 /*, ...*/);
or marker types:
services.AddAutoMapper(type1, type2 /*, ...*/);
This registers AutoMapper:
MapperConfigurationIMapperITypeConverter instances as transientIValueConverter instances as transientIValueResolver instances as transientIMemberValueResolver instances as transientIMappingAction instances as transientMapping configuration is static as it is the root object that can create an IMapper.
Mapper instances are registered as transient. You can configure this with the serviceLifetime parameter. Be careful changing this, as Mapper takes a dependency on a factory method to instantiate the other extensions.
To map at runtime, add a dependency on IMapper:
public class EmployeesController {
private readonly IMapper _mapper;
public EmployeesController(IMapper mapper)
=> _mapper = mapper;
// use _mapper.Map to map
}
Starting with 8.0 you can use IMapper.ProjectTo. The old ProjectTo is an extension method and does not have dependency injection available. Pass an IConfigurationProvider instance directly:
var orders = await dbContext.Orders
.ProjectTo<OrderDto>(_configurationProvider)
.ToListAsync();
Or you can use an IMapper instance:
var orders = await dbContext.Orders
.ProjectTo<OrderDto>(_mapper.ConfigurationProvider)
.ToListAsync();