In my Application layer, I have the following classes:
public class AutoMapperConfig
{
public static MapperConfiguration RegisterMappings()
{
return new MapperConfiguration(cfg =>
{
cfg.AddProfile(new DomainToViewModelMappingProfile());
cfg.AddProfile(new ViewModelToDomainMappingProfile());
});
}
}
public class DomainToViewModelMappingProfile : Profile
{
public DomainToViewModelMappingProfile()
{
CreateMap<Customer, CustomerViewModel>();
}
}
public class ViewModelToDomainMappingProfile : Profile
{
public ViewModelToDomainMappingProfile()
{
CreateMap<CustomerViewModel, RegisterNewCustomerCommand>()
.ConstructUsing(c => new RegisterNewCustomerCommand(c.Name, c.Email, c.BirthDate));
CreateMap<CustomerViewModel, UpdateCustomerCommand>()
.ConstructUsing(c => new UpdateCustomerCommand(c.Id, c.Name, c.Email, c.BirthDate));
}
}
In my Asp.Net MVC layer, I have the Startup.cs class which through the services.AddAutoMapper () method; initializes my Automapper configuration classes, except that it calls the method and does not pass through the classes mentioned above. Consequently, when I have to save some registry in my bank, an error occurs in my controller related to Automapper:
Error: "+ $ exception {AutoMapper.AutoMapperConfigurationException: Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source / destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
CustomerViewModel - > RegisterNewCustomerCommand (Destination member list) SystemComercial.Application.ViewModels.CustomerViewModel - > SystemComercial.Domain.Commands.Customer.RegisterNewCustomerCommand (Destination member list)
Unmapped properties: Timestamp ValidationResult MessageType AggregateId
at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid (IEnumerable'1 typeMaps) at lambda_method (Closure, CustomerViewModel, RegisterNewCustomerCommand, ResolutionContext) at lambda_method (Closure, Object, Object, ResolutionContext) at AutoMapper.Mapper.AutoMapper.IMapper.Map [TDestination] (Object source) at SystemComercial.Application.Services.CustomerAppService.Register (CustomerViewModel customerViewModel) in C: \ Users \ jalbe \ Desktop \ New_Project \ CommerceSystem \ src \ CommerceSystem.Application \ Services \ CustomerAppService.cs: line 46 at SystemComercial.Posentation.Web.MVC.Controllers.CustomerController.Create (CustomerViewModel customerViewModel) in C: \ Users \ jalbe \ Desktop \ New_Project \ CommerceSystem \ src \ CommerceSystem.Presentation.Web.MVC \ Controllers \ CustomerController.cs: line 62 at lambda_method (Closure, Object, Object []) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute (Object target, Object [] parameters) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext ()} AutoMapper.AutoMapperConfigurationException "
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAutoMapper(); //Jalber
// Adding MediatR for Domain Events and Notifications
services.AddMediatR(typeof(Startup)); //Jalber
// .NET Native DI Abstraction
RegisterServices(services); //Jalber
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
private static void RegisterServices(IServiceCollection services)
{
// Adding dependencies from another layers (isolated from Presentation)
NativeInjectorBootStrapper.RegisterServices(services);
}
}
How do I resolve this?