AutoMapper AutoMapperMappingException - c#

I've found some very strange behaviour of AutoMapper.
This simple code
internal class Program
{
private static void Main(string[] args)
{
Mapper.Initialize(cfg => { cfg.CreateMap<MyClass1, MyClass2>(); });
Mapper.Initialize(cfg => { cfg.CreateMap<MyClass3, MyClass4>(); });
var dto = new MyClass1();
Mapper.Map<MyClass1, MyClass2>(dto);
}
}
public class MyClass1
{
}
public class MyClass2
{
}
public class MyClass3
{
}
public class MyClass4
{
}
generates exception:
Missing type map configuration or unsupported mapping.
Mapping types: MyClass1 -> MyClass2 ConsoleApplication2.MyClass1 ->
ConsoleApplication2.MyClass2
but if change order of two initialize lines like this
Mapper.Initialize(cfg => { cfg.CreateMap<MyClass3, MyClass4>(); });
Mapper.Initialize(cfg => { cfg.CreateMap<MyClass1, MyClass2>(); });
everything is fine. What's wrong? What's going on?

Long story short, Jimmy temporarily moved away from the static stuff in AutoMapper in favour of instance based mapping. However, as Jimmy commented:
The static mapper is not removed, just the ability to call CreateMap anywhere in the code
The answer to question what is wrong is that you're trying to initialise twice, rather then once.
To answer the next question about how to have different configurations scattered across the code, you use Profile
To answer how you configure all of it, please see below:
For AutoMapper 5.1.1
There is a MapperConfigurationExpression
https://github.com/AutoMapper/AutoMapper/blob/master/src/AutoMapper/Configuration/MapperConfigurationExpression.cs
You can pass to the mapper, or Mapper takes a Action<IMapperConfigurationExpression>.
IMapperConfigurationExpression exposes this:
void AddProfile(Profile profile)
So you can pretty much do the same as below, but register everything against a IMapper interface, which is what it seems like 4.2.1 was heading towards.
For AutoMapper 4.2.1 (Short Intro to Profiles)
Here is an example profile:
using AutoMapper;
using TreasuryRecords.Database.Models;
using TreasuryRecords.Requests.Account.Models;
public class AccountMappings : Profile
{
protected override void Configure()
{
this.CreateMap<RegisterDto, Client>()
.ForMember(x => x.UserName, c => c.MapFrom(x => x.Email));
}
}
Here is an example of how I register my profiles:
using System;
using System.Linq;
using System.Reflection;
using AutoMapper;
using TreasuryRecords.Requests.Authenticate.Login;
public static class AutoMapperConfig
{
public static void Configure()
{
Assembly
.GetExecutingAssembly()
.RegisterConfigurations();
typeof(LoginRequest)
.Assembly
.RegisterConfigurations();
}
public static void RegisterConfigurations(this Assembly assembly)
{
var types = assembly.GetTypes();
var automapperProfiles = types
.Where(x => typeof(Profile).IsAssignableFrom(x))
.Select(Activator.CreateInstance)
.OfType<Profile>()
.ToList();
// so here you can pass in the instance of mapper
// I just use the static for ease
automapperProfiles.ForEach(Mapper.Configuration.AddProfile);
}
}
This is how I add it to DI:
public static void RegisterAutoMapper(this IUnityContainer container)
{
container.RegisterType<IMapper>(new InjectionFactory(_ => Mapper.Instance));
}
I'm using unity here, but it is pretty simple, just register Mapper.Instance against the interface IMapper.
Then I inject IMapper and use it like so:
this.mapper.Map<Client>(message.RegistrationDetails);

Related

How can I pass parameters to an AutoMapper Profile in ABP?

I need to customize the way MyAutoMapper profile maps my objects to DTOs. From one of my ApplicationServices, I use an ObjectMapper for a relatively simple mapping. The catch is that ABP's AutoMapper isn't the normal AutoMapper that everyone knows about.
Below is a snippet of what it would ideally look like; Except opt.MapFrom(m => Localizer[m.Type.ToString()]) and _objectMapper.Map<Preparation, DtoPreparation>(preparation, _localizer) cannot work that way.
public class MyAutoMapperProfile : Profile
{
public MyAutoMapperProfile()
{
CreateMap<Preparation, DtoPreparation>()
.ForMember(m => m.PreparatorType, opt => opt.MapFrom(m => m.Type))
.ForMember(m => m.PreparatorTypeString, opt => opt.MapFrom(m => Localizer[m.Type.ToString()]));
}
}
public class SlipsAppService : TaxesAppService
{
private readonly IObjectMapper<TaxesApplicationModule> _objectMapper;
private readonly ISlipsManager _slipsManager;
private readonly IStringLocalizer<TaxesResource> _localizer;
public SlipsAppService(ISlipsManager iSlipsManager, IObjectMapper<TaxesApplicationModule> objectMapper, IStringLocalizer<TaxesResource> localizer)
{
_objectMapper = objectMapper;
_slipsManager = iSlipsManager;
_localizer = localizer;
}
[Microsoft.AspNetCore.Mvc.HttpPost("/api/slips/get-or-create-preparation")]
public async Task<DtoPreparation> GetOrCreateCurrentPreparation(BaseGetterInput input)
{
var preparation = await _slipsManager.GetOrCreatePreparation(input.Id);
return _objectMapper.Map<Preparation, DtoPreparation>(preparation, _localizer);
}
}
I can't find a way to pass any information from my ApplicationService to the AutoMapper Profile, as IObjectMapper.Map<>() has no parameters for additional options or objects, unlike the normal AutoMapper.
Maybe there is a way to register the Profile in dependency injection, but with my limited knowledge of the framework, I couldn't find a clue...
For now, my problem is only with Localization, but really it can apply to anything. Since my DTOs contain other nested DTOs, managing extra stuff outside of the AutoMapper isn't an option, unless I change the structure of my application just for a workaround.
Since you are using one mapping profile per appservice, here is a good suggestion that works for me:
Create a class the implements the IMappingAction interface.
In the implementation of the Process method, inject your ILocalizer and use it with the source and destination.
In your mapping, instead of passing the localizer, chain call with AfterMap.
Here is an example:
public class MyAutoMapperProfile : Profile
{
public MyAutoMapperProfile()
{
CreateMap<Preparation, DtoPreparation>()
.ForMember(m => m.PreparatorType, opt => opt.MapFrom(m => m.Type))
.AfterMap<PreparationDtoLocalizerAction>;
}
}
public class PreparationDtoLocalizerAction : IMappingAction<Preparation, DtoPreparation>
{
private readonly IStringLocalizer<TaxesResource> _localizer;
public PreparationDtoLocalizerAction(IStringLocalizer<TaxesResource> localizer)
{
_localizer = localizer;
}
public void Process(Preparation source, DtoPreparation destination)
{
destination.PreparatorTypeString = _localizer[source.Type.ToString()]
}
}
public class SlipsAppService : TaxesAppService
{
private readonly IObjectMapper<TaxesApplicationModule> _objectMapper;
private readonly ISlipsManager _slipsManager;
public SlipsAppService(ISlipsManager iSlipsManager, IObjectMapper<TaxesApplicationModule> objectMapper)
{
_objectMapper = objectMapper;
_slipsManager = iSlipsManager;
}
[Microsoft.AspNetCore.Mvc.HttpPost("/api/slips/get-or-create-preparation")]
public async Task<DtoPreparation> GetOrCreateCurrentPreparation(BaseGetterInput input)
{
var preparation = await _slipsManager.GetOrCreatePreparation(input.Id);
return _objectMapper.Map<Preparation, DtoPreparation>(preparation);
}
}

AutoMapper 5.2 how to configure

What is the correct way to configure AutoMapper for global use.
I want to set it once and then used though out the app.
i have a strong feeling this is wrong.
in fact i know this is wrong as this calls an new instance.
I want a global config and then how do you call it.
Can not find a good example!
this is what ive got: but its not what im wanting
public static class AutoMapperConfig
{
public static IMapper GetMapper()
{
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<R_Logo, LogoDto>();
//lots more maps...?
});
IMapper mapper = config.CreateMapper();
return mapper;
}
}
and then usage:
var imapper = AutoMapperConfig.GetMapper();
var dest = imapper.Map<R_Logo, LogoDto>(logo);
UPDATE based on: pinkfloydx33
Call this once and then the config is done.
public static class AutoMapperConfig
{
public static void RegisterMappings()
{
AutoMapper.Mapper.Initialize(cfg => {
cfg.CreateMap<R_Logo, LogoDto>();
/* etc */
});
}
}
Here is the steps to configure the automapper in asp.net core mvc.
1. Create the mapping profile class which extends from Profile
public class ClientMappingProfile : Profile
{
public ClientMappingProfile ()
{
CreateMap<R_Logo, LogoDto>().ReverseMap();
}
}
2. Create the AutoMapper Configuration Class and add your mapping profile class here.
public class AutoMapperConfiguration
{
public MapperConfiguration Configure()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<ClientMappingProfile>();
});
return config;
}
}
3. How we can use it.
var config = new AutoMapperConfiguration().Configure();
var iMapper = config.CreateMapper();
var dest = iMapper.Map<R_Logo, LogoDto>(logo);
Set this in your StartupConfig or StartUp file.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
.....
MappingDTOModelToModel.Configure();
}
}
Configuration of Mappings,
public static class MappingDTOModelToModel
{
private static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<R_Logo, LogoDto>()
.ForMember(x => x.ID,
m => m.MapFrom(a => a.ID))
.ForMember(x => x.FirstName,
m => m.MapFrom(a => a.FirstName)).ReverseMap();
}
}
}
Calling it in a method,
public class MyService
{
public void MyMethod(var model)
{
var myModel = Mapper.Map<LogoDto, R_Logo>(model);
}
}
Hope this helps,
You can use the static mapper api as outlined here.
For example, somewhere in your application, probably during startup you would configure the static (global) mapper using something like:
AutoMapper.Mapper.Initialize(cfg => {
cfg.CreateMap<Type1, Type2>();
/* etc */
});
Then, any time you need to use your "globally" configured mapper, you access it via the static Mapper property (which is an IMapper):
Type1 objectOfType1 = new Type1();
var result = AutoMapper.Mapper.Map<Type2>(objectOfType1);
You then have one mapper that has been configured for all the types/configurations/profiles you provide for the duration of your application without needing to configure individual mapper instances.
In short, you configure it once (perhaps at application startup). The static mapper instance (the IMapper) is then available anywhere throughout your application by accessing it via AutoMapper.Mapper.
Access via this static property is what you refer to as "globally" in your comments. Anywhere you need it just use AutoMapper.Mapper.Map(...) So long as you've called Initialize once first.
Note that if you call Initialize more than once on the static instance, each subsequent call overwrites the existing configuration.
WARNING
In a previous release of AutoMapper, the static mapper was removed. It was later added back in and I don't know if they guarantee that it will remain in future versions. The recommendation is to use your own configured instances of a mapper. You can store it in a static property somewhere if you need it. Otherwise you can look into profiles, etc for easy ways to configure your mapper so that having your own instance isn't necessarily a "hassle".
Our solution to this problem was to first create a selection of attributes that can decorate a class as being "Mappable" (either To, From or Both). Then you can initialize the AutoMapper in a single location, usually post application initialization and use Reflection to dynamically create a map for each instance of the decorated classes.
Here's an example:
var types = _myTypeFinder.Find(type =>
type.IsDefined(typeof(AutoMapperAttribute)) ||
type.IsDefined(typeof(AutoMapperFromAttribute)) ||
type.IsDefined(typeof(AutoMapperToAttribute))
);
Mapper.Initialize(cfg =>
{
foreach (var type in types)
{
AutoMapperHelper.CreateMap(type, cfg);
}
});
I have find best solution for configuration auto mapper in .Net Core.
Multiple Profile.
Just use this:
services.AddSingleton(provider => new MapperConfiguration(cfg =>
{
cfg.AddProfile(new sampleProfileMapper());
}).CreateMapper());

How to set up Automapper in ASP.NET Core

I'm relatively new at .NET, and I decided to tackle .NET Core instead of learning the "old ways". I found a detailed article about setting up AutoMapper for .NET Core here, but is there a more simple walkthrough for a newbie?
I figured it out! Here's the details:
Add the main AutoMapper Package to your solution via NuGet.
Add the AutoMapper Dependency Injection Package to your solution via NuGet.
Create a new class for a mapping profile. (I made a class in the main solution directory called MappingProfile.cs and add the following code.) I'll use a User and UserDto object as an example.
public class MappingProfile : Profile {
public MappingProfile() {
// Add as many of these lines as you need to map your objects
CreateMap<User, UserDto>();
CreateMap<UserDto, User>();
}
}
Then add the AutoMapperConfiguration in the Startup.cs as shown below:
public void ConfigureServices(IServiceCollection services) {
// .... Ignore code before this
// Auto Mapper Configurations
var mapperConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mapperConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddMvc();
}
To invoke the mapped object in code, do something like the following:
public class UserController : Controller {
// Create a field to store the mapper object
private readonly IMapper _mapper;
// Assign the object in the constructor for dependency injection
public UserController(IMapper mapper) {
_mapper = mapper;
}
public async Task<IActionResult> Edit(string id) {
// Instantiate source object
// (Get it from the database or whatever your code calls for)
var user = await _context.Users
.SingleOrDefaultAsync(u => u.Id == id);
// Instantiate the mapped data transfer object
// using the mapper you stored in the private field.
// The type of the source object is the first type argument
// and the type of the destination is the second.
// Pass the source object you just instantiated above
// as the argument to the _mapper.Map<>() method.
var model = _mapper.Map<UserDto>(user);
// .... Do whatever you want after that!
}
}
Step To Use AutoMapper with ASP.NET Core.
Step 1. Installing AutoMapper.Extensions.Microsoft.DependencyInjection from NuGet Package.
Step 2. Create a Folder in Solution to keep Mappings with Name "Mappings".
Step 3. After adding Mapping folder we have added a class with Name "MappingProfile" this name can anything unique and good to understand.
In this class, we are going to Maintain all Mappings.
Step 4. Initializing Mapper in Startup "ConfigureServices"
In Startup Class, we Need to Initialize Profile which we have created and also Register AutoMapper Service.
Mapper.Initialize(cfg => cfg.AddProfile<MappingProfile>());
services.AddAutoMapper();
Code Snippet to show ConfigureServices Method where we need to Initialize and Register AutoMapper.
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
// Start Registering and Initializing AutoMapper
Mapper.Initialize(cfg => cfg.AddProfile<MappingProfile>());
services.AddAutoMapper();
// End Registering and Initializing AutoMapper
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}}
Step 5. Get Output.
To Get Mapped result we need to call AutoMapper.Mapper.Map and pass Proper Destination and Source.
AutoMapper.Mapper.Map<Destination>(source);
CodeSnippet
[HttpPost]
public void Post([FromBody] SchemeMasterViewModel schemeMaster)
{
if (ModelState.IsValid)
{
var mappedresult = AutoMapper.Mapper.Map<SchemeMaster>(schemeMaster);
}
}
I want to extend #theutz's answers - namely this line :
// services.AddAutoMapper(typeof(Startup)); // <-- newer automapper version uses this signature.
There is a bug (probably) in AutoMapper.Extensions.Microsoft.DependencyInjection version 3.2.0. (I'm using .NET Core 2.0)
This is tackled in this GitHub issue. If your classes inheriting AutoMapper's Profile class exist outside of assembly where you Startup class is they will probably not be registered if your AutoMapper injection looks like this:
services.AddAutoMapper();
unless you explicitly specify which assemblies to search AutoMapper profiles for.
It can be done like this in your Startup.ConfigureServices:
services.AddAutoMapper(<assembies> or <type_in_assemblies>);
where "assemblies" and "type_in_assemblies" point to the assembly where Profile classes in your application are specified. E.g:
services.AddAutoMapper(typeof(ProfileInOtherAssembly), typeof(ProfileInYetAnotherAssembly));
I suppose (and I put emphasis on this word) that due to following implementation of parameterless overload (source code from GitHub) :
public static IServiceCollection AddAutoMapper(this IServiceCollection services)
{
return services.AddAutoMapper(null, AppDomain.CurrentDomain.GetAssemblies());
}
we rely on CLR having already JITed assembly containing AutoMapper profiles which might be or might not be true as they are only jitted when needed (more details in this StackOverflow question).
theutz' answer here is very good, I just want to add this:
If you let your mapping profile inherit from MapperConfigurationExpression instead of Profile, you can very simply add a test to verify your mapping setup, which is always handy:
[Fact]
public void MappingProfile_VerifyMappings()
{
var mappingProfile = new MappingProfile();
var config = new MapperConfiguration(mappingProfile);
var mapper = new Mapper(config);
(mapper as IMapper).ConfigurationProvider.AssertConfigurationIsValid();
}
I solved it this way (similar to above but I feel like it's a cleaner solution) Works with .NET Core 3.x
Create MappingProfile.cs class and populate constructor with Maps (I plan on using a single class to hold all my mappings)
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Source, Dest>().ReverseMap();
}
}
In Startup.cs, add below to add to DI (the assembly arg is for the class that holds your mapping configs, in my case, it's the MappingProfile class).
//add automapper DI
services.AddAutoMapper(typeof(MappingProfile));
In Controller, use it like you would any other DI object
[Route("api/[controller]")]
[ApiController]
public class AnyController : ControllerBase
{
private readonly IMapper _mapper;
public AnyController(IMapper mapper)
{
_mapper = mapper;
}
public IActionResult Get(int id)
{
var entity = repository.Get(id);
var dto = _mapper.Map<Dest>(entity);
return Ok(dto);
}
}
I like a lot of answers, particularly #saineshwar 's one. I'm using .net Core 3.0 with AutoMapper 9.0, so I feel it's time to update its answer.
What worked for me was in Startup.ConfigureServices(...) register the service in this way:
services.AddAutoMapper(cfg => cfg.AddProfile<MappingProfile>(),
AppDomain.CurrentDomain.GetAssemblies());
I think that rest of #saineshwar answer keeps perfect. But if anyone is interested my controller code is:
[HttpGet("{id}")]
public async Task<ActionResult> GetIic(int id)
{
// _context is a DB provider
var Iic = await _context.Find(id).ConfigureAwait(false);
if (Iic == null)
{
return NotFound();
}
var map = _mapper.Map<IicVM>(Iic);
return Ok(map);
}
And my mapping class:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Iic, IicVM>()
.ForMember(dest => dest.DepartmentName, o => o.MapFrom(src => src.Department.Name))
.ForMember(dest => dest.PortfolioTypeName, o => o.MapFrom(src => src.PortfolioType.Name));
//.ReverseMap();
}
}
----- EDIT -----
After reading the docs linked in the comments by Lucian Bargaoanu, I think it's better to change this answer a bit.
The parameterless services.AddAutoMapper() (that had the #saineshwar answer) doesn't work anymore (at least for me). But if you use the NuGet assembly AutoMapper.Extensions.Microsoft.DependencyInjection, the framework is able to inspect all the classes that extend AutoMapper.Profile (like mine, MappingProfile).
So, in my case, where the class belong to the same executing assembly, the service registration can be shortened to services.AddAutoMapper(System.Reflection.Assembly.GetExecutingAssembly());
(A more elegant approach could be a parameterless extension with this coding).
Thanks, Lucian!
At the latest versions of asp.net core you should use the following initialization:
services.AddAutoMapper(typeof(YourMappingProfileClass));
In my Startup.cs (Core 2.2, Automapper 8.1.1)
services.AddAutoMapper(new Type[] { typeof(DAL.MapperProfile) });
In my data access project
namespace DAL
{
public class MapperProfile : Profile
{
// place holder for AddAutoMapper (to bring in the DAL assembly)
}
}
In my model definition
namespace DAL.Models
{
public class PositionProfile : Profile
{
public PositionProfile()
{
CreateMap<Position, PositionDto_v1>();
}
}
public class Position
{
...
}
For AutoMapper 9.0.0:
public static IEnumerable<Type> GetAutoMapperProfilesFromAllAssemblies()
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var aType in assembly.GetTypes())
{
if (aType.IsClass && !aType.IsAbstract && aType.IsSubclassOf(typeof(Profile)))
yield return aType;
}
}
}
MapperProfile:
public class OrganizationProfile : Profile
{
public OrganizationProfile()
{
CreateMap<Foo, FooDto>();
// Use CreateMap... Etc.. here (Profile methods are the same as configuration methods)
}
}
In your Startup:
services.AddAutoMapper(GetAutoMapperProfilesFromAllAssemblies()
.ToArray());
In Controller or service:
Inject mapper:
private readonly IMapper _mapper;
Usage:
var obj = _mapper.Map<TDest>(sourceObject);
Need to install a package for setting up the automapper.
dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection
After the AddAutoMapper will be available in services.
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));
}
Create mapper from Employee class to EmployeeDTO.
using AutoMapper;
public class AutomapperProfile: Profile
{
public AutomapperProfile()
{
//Source to destination.
CreateMap<Employee,EmployeeDTO>();
}
}
EmployeeController maps from Employee to EmployeeDTo
using System.Collections.Generic;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
[Route("api/[controller]")]
[ApiController()]
public class EmployeeController : ControllerBase
{
private readonly IMapper _mapper;
public EmployeeController(IMapper mapper)
{
_mapper = mapper;
}
[HttpGet]
public IEnumerable<EmployeeDTO> GetEmployees()
{
/*
Assume it to be a service call/database call
it returns a list of employee, and now we will map it to EmployeeDTO
*/
var employees = Employee.SetupEmployee();
var employeeDTO = _mapper.Map<IEnumerable<EmployeeDTO>>(employees);
return employeeDTO;
}
}
Employee.cs for reference
using System.Collections.Generic;
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int Salary { get; set; }
public static IEnumerable<Employee> SetupEmployee()
{
return new List<Employee>()
{
new Employee(){EmployeeId = 1, EmployeeName ="First", Salary=10000},
new Employee(){EmployeeId = 2, EmployeeName ="Second", Salary=20000},
new Employee(){EmployeeId = 3, EmployeeName ="Third", Salary=30000},
new Employee(){EmployeeId = 4, EmployeeName ="Fourth", Salary=40000},
new Employee(){EmployeeId = 5, EmployeeName ="Fifth", Salary=50000}
};
}
}
EmployeeDTO.cs for reference
public class EmployeeDTO
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
}
I am using AutoMapper 6.1.1 and asp.net Core 1.1.2.
First of all, define Profile classes inherited by Profile Class of Automapper. I Created IProfile interface which is empty, the purpose is only to find the classes of this type.
public class UserProfile : Profile, IProfile
{
public UserProfile()
{
CreateMap<User, UserModel>();
CreateMap<UserModel, User>();
}
}
Now create a separate class e.g Mappings
public class Mappings
{
public static void RegisterMappings()
{
var all =
Assembly
.GetEntryAssembly()
.GetReferencedAssemblies()
.Select(Assembly.Load)
.SelectMany(x => x.DefinedTypes)
.Where(type => typeof(IProfile).GetTypeInfo().IsAssignableFrom(type.AsType()));
foreach (var ti in all)
{
var t = ti.AsType();
if (t.Equals(typeof(IProfile)))
{
Mapper.Initialize(cfg =>
{
cfg.AddProfiles(t); // Initialise each Profile classe
});
}
}
}
}
Now in MVC Core web Project in Startup.cs file, in the constructor, call Mapping class which will initialize all mappings at the time of application
loading.
Mappings.RegisterMappings();
In .NET 6 you'll need to add the following to the Program.cs file:
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
For ASP.NET Core (tested using 2.0+ and 3.0), if you prefer to read the source documentation:
https://github.com/AutoMapper/AutoMapper.Extensions.Microsoft.DependencyInjection/blob/master/README.md
Otherwise following these 4 steps works:
Install AutoMapper.Extensions.Microsoft.DependancyInjection from nuget.
Simply add some profile classes.
Then add below to your startup.cs class.
services.AddAutoMapper(OneOfYourProfileClassNamesHere)
Then simply Inject IMapper in your controllers or wherever you need it:
public class EmployeesController {
private readonly IMapper _mapper;
public EmployeesController(IMapper mapper){
_mapper = mapper;
}
And if you want to use ProjectTo its now simply:
var customers = await dbContext.Customers.ProjectTo<CustomerDto>(_mapper.ConfigurationProvider).ToListAsync()
Let’s have a look at how to add Auto mapper into our .NET Core application.
step: 1
The first step is to install the corresponding NuGet package:
Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection
step: 2
After installing the required package, the next step is to configure the services. Let’s do it in the Startup.cs class:
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));
services.AddControllersWithViews();
}
step: 3
Let’s start usage we have a domain object named User:
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Address { get; set; }
}
In the UI layer, we would have a View Model to display the user information:
public class UserViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
step: 4
A good way to organize our mapping configurations is with Profiles. We need to create classes that inherit from Profile class and put the configuration in the constructor:
public UserProfile()
{
CreateMap<User, UserViewModel>();
}
step: 5
Now, let’s define a Controller and use the Auto-Mapping capabilities that we just added:
public class UserController : Controller
{
private readonly IMapper _mapper;
public UserController(IMapper mapper)
{
_mapper = mapper;
}
public IActionResult Index()
{
// Populate the user details from DB
var user = GetUserDetails();
UserViewModel userViewModel = _mapper.Map<UserViewModel>(user);
return View(userViewModel);
}
}
First, we inject the mapper object into the controller. Then, we call the Map() method, which maps the User object to the UserViewModel object. Furthermore, pay attention to a local method GetUserDetails that we use for the local data storage.
You can find its implementation in our source code.
Asp.Net Core 2.2 with AutoMapper.Extensions.Microsoft.DependencyInjection.
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Domain, DomainDto>();
}
}
In Startup.cs
services.AddAutoMapper(typeof(List.Handler));
services.AddAutoMapper(); didn't work for me. (I am using Asp.Net Core 2.0)
After configuring as below
var config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.CreateMap<ClientCustomer, Models.Customer>();
});
initialize the mapper
IMapper mapper = config.CreateMapper();
and add the mapper object to services as a singleton
services.AddSingleton(mapper);
this way I am able to add a DI to controller
private IMapper autoMapper = null;
public VerifyController(IMapper mapper)
{
autoMapper = mapper;
}
and I have used as below in my action methods
ClientCustomer customerObj = autoMapper.Map<ClientCustomer>(customer);
To add onto what Arve Systad mentioned for testing. If for whatever reason you're like me and want to maintain the inheritance structure provided in theutz solution, you can set up the MapperConfiguration like so:
var mappingProfile = new MappingProfile();
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(mappingProfile);
});
var mapper = new Mapper(config);
I did this in NUnit.
For AutoMapper 11.0.1 using .NET 7 I started getting this exception:
System.ArgumentException: 'GenericArguments[0], 'System.DateTime', on 'T MaxInteger[T](System.Collections.Generic.IEnumerable`1[T])' violates the constraint of type 'T'.'
Inner Exception
VerificationException: Method System.Linq.Enumerable.MaxInteger: type argument 'System.DateTime' violates the constraint of type parameter 'T'.
See this question:
System.DateTime on 'T MaxInteger[T](System.Collections.Generic.IEnumerable`1[T])' violates the constraint of type T for .NET 7 using AutoMapper 11.0.1
This meant that I could no longer use services.AddAutoMapper(typeof(MappingProfile).Assembly); without an exception.
For AutoMapper.Extensions.Microsoft.DependencyInjection I solved it like this:
services.AddAutoMapper(cfg => cfg.Internal().MethodMappingEnabled = false, typeof(MappingProfile).Assembly);
For Blazor WebAssembly client the solution looked like this:
var mapperConfig = new MapperConfiguration(mc =>
{
//Needed for https://github.com/AutoMapper/AutoMapper/issues/3988
mc.Internal().MethodMappingEnabled = false;
mc.AddProfile(new MappingProfile());
});
//mapperConfig.AssertConfigurationIsValid();
IMapper mapper = mapperConfig.CreateMapper();
builder.Services.AddSingleton(mapper);
about theutz answer ,
there is no need to specify the IMapper mapper parrameter at the controllers constructor.
you can use the Mapper as it is a static member at any place of the code.
public class UserController : Controller {
public someMethod()
{
Mapper.Map<User, UserDto>(user);
}
}

How can I add a mapping in AutoMapper after Initialize has been called?

I have a couple of ASP.Net apps that share mapping code, so I've created a generic automapper init class.
However, in one of my apps, I have some specific classes that I want added to the configuration.
I have the following code:
public class AutoMapperMappings
{
public static void Init()
{
AutoMapper.Mapper.Initialize(cfg =>
{
... A whole bunch of mappings here ...
}
}
}
and
// Call into the global mapping class
AutoMapperMappings.Init();
// This erases everything
AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<CustomerModel, CustomerInfoModel>());
How do I add this unique mapping without destroying what is already initialized?
A quick sample that allows you to initialize your AutoMapper 5.x several times...
Ok it's not very nice ;)
public static class MapperInitializer
{
/// <summary>
/// Initialize mapper
/// </summary>
public static void Init()
{
// Static mapper
Mapper.Initialize(Configuration);
// ...Or instance mapper
var mapperConfiguration = new MapperConfiguration(Configuration);
var mapper = mapperConfiguration.CreateMapper();
// ...
}
/// <summary>
/// Mapper configuration
/// </summary>
public static MapperConfigurationExpression Configuration { get; } = new MapperConfigurationExpression();
}
// First config
MapperInitializer.Configuration.CreateMap(...);
MapperInitializer.Init(); // or not
//...
MapperInitializer.Configuration.CreateMap(...);
MapperInitializer.Init();
The idea is to store the MapperConfigurationExpression instead of the MapperConfiguration instance.
This should be possible if you use the instance API that AutoMapper provides instead of the static API. This wiki page details the differences between the two.
Essentially instead of calling AutoMapper.Mapper.Initialize(cfg => ...) again for your additional mapping, which overwrites the entire global mapper configuration with that single mapping, you'll need to create another mapper object with the instance API using:
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<CustomerModel, CustomerInfoModel>()
);
var mapper = config.CreateMapper();
Of course in order to use this new mapper you will have to do something like var mappedModel = mapper.Map<CustomerInfoModel>(new CustomerModel()); specifically when mapping objects using your additional mapping configuration. Whether that's practical in your case, I don't know, but I believe this is the only way to do what you require.
You can't, but rather than initialize the Mappings from your Init method, you could get it to return a function that can be called inside a Mapper.Initialize() call.
So, your Init method looks like this:
public static Action<IMapperConfigurationExpression> Init()
{
return (cfg) => {
... A whole bunch of mappings here ...
};
}
Then from your app where you want extra mappings:
var mappingsFunc = MyClass.Init();
Mapper.Initialize((cfg) => {
mappingsFunc(cfg);
... Extra mappings here ...
});
or you could reduce it a little...
Mapper.Initialize((cfg) => {
MyClass.Init()(cfg);
... Extra mappings here ...
});
Hope this helps.
Automapper 5+
I have an initialiser class in my main assembly
public static class Mapping
{
public static void Initialize()
{
// Or marker types for assemblies:
Mapper.Initialize(cfg =>
cfg.AddProfiles(new[] {
typeof(MapperFromImportedAssemblyA),
typeof(MapperFromImportedAssemblyB),
typeof(MapperFromImportedAssemblyC)
})
);
}
}
Then in each assembly that requires a Mapper
public class MapperFromImportedAssemblyA : Profile
{
public MapperFromImportedAssemblyA()
{
// Use CreateMap here (Profile methods are the same as configuration methods)
}
}
This is what I hacked up for my requirement.
Actual Configurator
public static void Configure(params Action<MapperConfigurationExpression>[] registerCallbacks)
{
MapperConfigurationExpression configuration = new MapperConfigurationExpression();
foreach (Action<MapperConfigurationExpression> regCallBack in registerCallbacks)
{
regCallBack.Invoke(configuration);
}
AutoMapper.Mapper.Initialize(configuration);
}
Mapping Group 1
public class AutoMapperConfigSet1
{
public static void RegisterTypes(MapperConfigurationExpression configuration)
{
configuration.CreateMap<Foo, Bar>();
}
}
Mapping Group 2
public class AutoMapperConfigSet2
{
public static void RegisterTypes(MapperConfigurationExpression configuration)
{
configuration.CreateMap<Foo1, Bar1>();
}
}
When initializing
Configure(AutoMapperConfigSet1.RegisterTypes,AutoMapperConfigSet2.RegisterTypes);

Where to place AutoMapper map registration in referenced dll

This is my first AutoMapper project and may be obvious to some but the tutorials and examples are not clicking with me. I am trying to understand where and to a certain degree how to register(I think I want profiles) my maps for use. There are plenty of MVC examples saying to use the global asax and this makes sense but what is the equivalent in a library project?
In my sandbox I have a winform app and a core library. The winform app calls methods made available by the library and it is one of these library methods that makes use of automapper.
So for some background here is my map:
(and to be clear the mapping is in the SAME core library project)
public class Raw_Full_Map
{
public Raw_Full_Map()
{
Mapper.CreateMap<IEnumerable<RawData>, FullData>()
.ForMember(d => d.Acres, m => m.ResolveUsing(new RawLeadDataNameResolver("Acres")));
//this is clearly just a snip to show it's a basic map
}
}
This is the core library method being called: (note it is a static..which means I won't have a constructor...if this is the problem am I to understand then that AutoMapper can't be utilized by static helper classes...that doesn't make sense....so likely I'm just not doing it right.
public static class RawDataProcessing
{
public static FullData HTMLDataScrape(string htmlScrape)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlScrape);
var list = Recurse(doc.DocumentNode);
//HTML agility stuff that turns my html doc into a List<RawData> object
return Mapper.Map<FullData>(list);
}
My test harness calls it like this:
var _data = RawDataProcessing.HTMLDataScrape(rawHTML);
This of course errors because the map isn't "registered".
If I do this in the test harness:
var x = new RawData_FullData();
var _data = RawDataProcessing.HTMLDataScrape(rawHTML);
Then everything works as my map get's registered albeit I think in a really bogus way...but it does work.
So the question is how do I register my mapping in the core library project...so that ANY method can use it...there isn't really an equivalent global.asax in a dll is there?
Thank you for helping me connect the missing pieces.
Put it in the static constructor of either the source or the target type of the mapping.
public class FullData
{
static FullData()
{
Mapper.CreateMap<IEnumerable<RawData>, FullData>()
.ForMember(d => d.Acres, m => m.ResolveUsing(new RawLeadDataNameResolver("Acres")));
}
}
The static constructor will automatically get called the first time you try to use the type FullData for anything (for example a mapping).
You can use PreApplicationStartMethod for any class and it's method in your class library which will be referenced from your startup project if you want automatically to call this on startup. And then you can register all your mappings in that method. By the way, I suggest to use AddProfile for registering all mappings.
[assembly: PreApplicationStartMethod(typeof(MyClassLibrary.Startup), "Start")]
namespace MyClassLibrary
{
public class Startup
{
// Automatically will work on startup
public static void Start()
{
Mapper.Initialize(cfg =>
{
Assembly.GetExecutingAssembly().FindAllDerivedTypes<Profile>().ForEach(match =>
{
cfg.AddProfile(Activator.CreateInstance(match) as Profile);
});
});
}
}
}
You just need to create new classes which derived from Profile class and then override it's Configure() method:
...
public class FooMapperProfile:Profile
{
protected override void Configure()
{
Mapper.CreateMap<OtherFoo, Foo>()
.ForMember(...
... // so on
}
}
public class AnotherFooMapperProfile:Profile
{
protected override void Configure()
{
Mapper.CreateMap<OtherFoo, AnotherFoo>()
.ForMember(...
... // so on;
}
}
...
// and so on
Additional information:
If you have seen I have initialized all mappings with that code:
Mapper.Initialize(cfg =>
{
Assembly.GetExecutingAssembly().FindAllDerivedTypes<Profile>().ForEach(match =>
{
cfg.AddProfile(Activator.CreateInstance(match) as Profile);
});
});
It will automatically find all types derived from Profile and will add all profiles after createing their new instances.
Update1:
As #Scott Chamberlain commented, PreApplicationStartMethod only works for ASP.NET applications. This would not work with a desktop app. If you are working with Wpf, then you can use Application.OnStartup method. Or just call Start.Startup (); in load event.
Update2:
FindAllDerivedTypes extension method:
public static class AssemblyExtensions
{
public static List<Type> FindAllDerivedTypes<T>(this Assembly assembly)
{
var derivedType = typeof(T);
return assembly.GetTypes()
.Where(t => t != derivedType && derivedType.IsAssignableFrom(t))
.ToList();
}
}

Categories