AdaskoTheBeAsT.AutoMapper.SimpleInjector 8.0.1

dotnet add package AdaskoTheBeAsT.AutoMapper.SimpleInjector --version 8.0.1
NuGet\Install-Package AdaskoTheBeAsT.AutoMapper.SimpleInjector -Version 8.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AdaskoTheBeAsT.AutoMapper.SimpleInjector" Version="8.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AdaskoTheBeAsT.AutoMapper.SimpleInjector --version 8.0.1
#r "nuget: AdaskoTheBeAsT.AutoMapper.SimpleInjector, 8.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AdaskoTheBeAsT.AutoMapper.SimpleInjector as a Cake Addin
#addin nuget:?package=AdaskoTheBeAsT.AutoMapper.SimpleInjector&version=8.0.1

// Install AdaskoTheBeAsT.AutoMapper.SimpleInjector as a Cake Tool
#tool nuget:?package=AdaskoTheBeAsT.AutoMapper.SimpleInjector&version=8.0.1

AdaskoTheBeAsT.AutoMapper.SimpleInjector

AutoMapper extensions for SimpleInjector

Badges

CodeFactor Build Status Azure DevOps tests Azure DevOps coverage Quality Gate Status Sonar Tests Sonar Test Count Sonar Test Execution Time Sonar Coverage Nuget FOSSA Status

Usage

Scans assemblies and:

  1. adds profiles to mapping configuration
  2. adds implementations of ITypeConverter, IValueConverter, IValueResolver, IMemberValueResolver,IMappingAction instances as transient to the container.

There are few options to use with Container instance:

  1. Marker type from assembly which will be scanned

     container.AddAutoMapper(typeof(MyMapper), type2 /*, ...*/);
    
  2. 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 singleton
  • IMapper - As a singleton
  • ITypeConverter instances as transient
  • IValueConverter instances as transient
  • IValueResolver instances as transient
  • IMemberValueResolver instances as transient
  • IMappingAction 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

FOSSA Status

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
8.0.1 319 1/27/2024
8.0.0 517 11/29/2023
7.0.1 939 1/22/2023
7.0.0 827 11/13/2022
6.0.0 885 10/17/2022
5.1.0 855 7/24/2022
5.0.1 966 2/8/2022
5.0.0 760 1/6/2022
4.2.2 1,154 7/24/2021
4.2.1 786 6/27/2021
4.2.0 902 3/7/2021
4.1.1 972 1/16/2021
4.1.0 1,036 12/16/2020
4.0.1 956 11/29/2020
4.0.0 956 11/11/2020
3.2.0 1,121 10/25/2020
3.1.0 952 10/16/2020
3.0.2 1,024 7/25/2020
3.0.1 952 7/14/2020
3.0.0 917 7/2/2020
2.0.1 966 6/16/2020
2.0.0 1,533 6/14/2020
1.2.6 1,284 5/20/2020
1.2.5 982 4/28/2020
1.2.4 984 4/23/2020
1.2.3 944 4/21/2020
1.2.2 1,001 3/7/2020
1.2.1 1,122 2/27/2020
1.2.0 973 2/25/2020
1.1.0 986 1/9/2020
1.0.0 1,150 1/5/2020

- .NET 8 release