AdaskoTheBeAsT.AutoMapper.SimpleInjector
10.0.0
dotnet add package AdaskoTheBeAsT.AutoMapper.SimpleInjector --version 10.0.0
NuGet\Install-Package AdaskoTheBeAsT.AutoMapper.SimpleInjector -Version 10.0.0
<PackageReference Include="AdaskoTheBeAsT.AutoMapper.SimpleInjector" Version="10.0.0" />
paket add AdaskoTheBeAsT.AutoMapper.SimpleInjector --version 10.0.0
#r "nuget: AdaskoTheBeAsT.AutoMapper.SimpleInjector, 10.0.0"
// Install AdaskoTheBeAsT.AutoMapper.SimpleInjector as a Cake Addin #addin nuget:?package=AdaskoTheBeAsT.AutoMapper.SimpleInjector&version=10.0.0 // Install AdaskoTheBeAsT.AutoMapper.SimpleInjector as a Cake Tool #tool nuget:?package=AdaskoTheBeAsT.AutoMapper.SimpleInjector&version=10.0.0
AdaskoTheBeAsT.AutoMapper.SimpleInjector
AutoMapper extensions for SimpleInjector
Badges
Usage
Scans assemblies and:
- adds profiles to mapping configuration
- adds implementations of
ITypeConverter
,IValueConverter
,IValueResolver
,IMemberValueResolver
,IMappingAction
instances as transient to the container.
There are few options to use with Container
instance:
Marker type from assembly which will be scanned
container.AddAutoMapper(typeof(MyMapper), type2 /*, ...*/);
List of assemblies which will be scanned.
Below is sample for scanning assemblies from some solution.
[ExcludeFromCodeCoverage] public static class AutoMapperConfigurator { private const string NamespacePrefix = "YourNamespace"; public static void Configure(Container container) { var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); var assemblies = new List<Assembly>(); var mainAssembly = typeof(AutoMapperConfigurator).Assembly; var refAssemblies = mainAssembly.GetReferencedAssemblies(); foreach (var assemblyName in refAssemblies .Where(a => a.FullName.StartsWith(NamespacePrefix, StringComparison.OrdinalIgnoreCase))) { var assembly = loadedAssemblies.Find(l => l.FullName == assemblyName.FullName) ?? AppDomain.CurrentDomain.Load(assemblyName); assemblies.Add(assembly); } container.AddAutoMapper(assemblies); } }
This registers AutoMapper:
MapperConfiguration
- As a singletonIMapper
- As a singletonITypeConverter
instances as transientIValueConverter
instances as transientIValueResolver
instances as transientIMemberValueResolver
instances as transientIMappingAction
instances as transient
Mapping configuration is static as it is the root object that can create an IMapper
.
Mapper instance are registered as singleton. You can configure this with the Lifestyle
parameter. Be careful changing this, as Mapper
takes a dependency on a factory method to instantiate the other extensions.
Advanced usage
Setting up custom IMapper
instance and marker type from assembly for unit testing (Moq sample)
var testMapper = new Mock<IMapper>();
container.AddAutoMapper(
cfg =>
{
cfg.Using(() => testMapper.Object);
cfg.WithMapperAssemblyMarkerTypes(typeof(MyMarkerType));
});
Setting up custom IMapper
implementation and marker type from assembly
container.AddAutoMapper(
cfg =>
{
cfg.Using<MyCustomMapper>();
cfg.WithMapperAssemblyMarkerTypes(typeof(MyMarkerType));
});
Setting up custom IMapper
implementation and assemblies to scan
container.AddAutoMapper(
cfg =>
{
cfg.Using<MyCustomMapper>();
cfg.WithAssembliesToScan(assemblies);
});
Setting assemblies to scan and different lifetime for IMapper
implementation
container.AddAutoMapper(
cfg =>
{
cfg.WithAssembliesToScan(assemblies);
cfg.AsScoped();
});
Setting configuration for MapperConfigurationExpression
container.AddAutoMapper(
cfg =>
{
cfg.WithAssembliesToScan(assemblies);
cfg.AsScoped();
cfg.WithMapperConfigurationExpressionAction(
(
container1,
expression) => expression.CreateMap<Foo, Bar>().ReverseMap());
});
Library scans all descendant classes from Profile
so it is better to store mapping in Profile
descendants
public class Profile1 : Profile
{
public Profile1()
{
CreateMap<Source, Dest>();
}
}
Mapper.Map usage
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
}
ProjectTo usage
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();
Thanks to:
- Jimmy Boggard for AutoMapper
- Steven van Deursen for SimpleInjector
Code originates from AutoMapper.Extensions.Microsoft.DependencyInjection and was changed to work with SimpleInjector.
License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. |
-
net6.0
- AutoMapper (>= 13.0.1)
- SimpleInjector (>= 5.5.0)
-
net7.0
- AutoMapper (>= 13.0.1)
- SimpleInjector (>= 5.5.0)
-
net8.0
- AutoMapper (>= 13.0.1)
- SimpleInjector (>= 5.5.0)
-
net9.0
- AutoMapper (>= 13.0.1)
- SimpleInjector (>= 5.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
10.0.0 | 88 | 11/13/2024 |
9.2.0 | 248 | 8/18/2024 |
9.1.0 | 122 | 5/27/2024 |
8.0.1 | 383 | 1/27/2024 |
8.0.0 | 547 | 11/29/2023 |
7.0.1 | 1,054 | 1/22/2023 |
7.0.0 | 853 | 11/13/2022 |
6.0.0 | 916 | 10/17/2022 |
5.1.0 | 877 | 7/24/2022 |
5.0.1 | 1,001 | 2/8/2022 |
5.0.0 | 779 | 1/6/2022 |
4.2.2 | 1,211 | 7/24/2021 |
4.2.1 | 816 | 6/27/2021 |
4.2.0 | 964 | 3/7/2021 |
4.1.1 | 1,006 | 1/16/2021 |
4.1.0 | 1,080 | 12/16/2020 |
4.0.1 | 978 | 11/29/2020 |
4.0.0 | 991 | 11/11/2020 |
3.2.0 | 1,149 | 10/25/2020 |
3.1.0 | 996 | 10/16/2020 |
3.0.2 | 1,049 | 7/25/2020 |
3.0.1 | 981 | 7/14/2020 |
3.0.0 | 940 | 7/2/2020 |
2.0.1 | 988 | 6/16/2020 |
2.0.0 | 1,961 | 6/14/2020 |
1.2.6 | 1,315 | 5/20/2020 |
1.2.5 | 1,020 | 4/28/2020 |
1.2.4 | 1,014 | 4/23/2020 |
1.2.3 | 970 | 4/21/2020 |
1.2.2 | 1,025 | 3/7/2020 |
1.2.1 | 1,142 | 2/27/2020 |
1.2.0 | 998 | 2/25/2020 |
1.1.0 | 1,011 | 1/9/2020 |
1.0.0 | 1,172 | 1/5/2020 |
- .NET 9 release