I am a hater of AutoMapper. I don't think it is really beneficial to delegate mapping to it, because over time, it becomes a mapping hell where you are trying to identify which properties haven't been mapped and where and why. You would never be in such a situation if you just manually map one type to another. Yes, it is boring. But it is stable. After all, you can speed it up with Copilot or any other tool.
But if you got trapped in AutoMapper frustration, I'm here to cover your back.
There is a very fast and easy way to check if any type maps are missing. Add this to your startup script, and it will tell you during startup if there are any unmapped properties. This will throw an exception if there are any issues, preventing the application from running.
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var mapper = scope.ServiceProvider.GetRequiredService<IMapper>();
mapper.ConfigurationProvider.AssertConfigurationIsValid();
}
Example output
Unhandled 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
=============================================
CustomerDto -> CreateCustomerCommand (Destination member list)
SampleApp.Application.Dtos.CustomerDto ->
SampleApp.Application.Commands.Customers.CreateCustomerCommand (Destination member list)
Unmapped properties:
OrganizationId
=============================================
CustomerDto -> UpdateCustomerCommand (Destination member list)
SampleApp.Application.Dtos.CustomerDto ->
SampleApp.Application.Commands.Customers.UpdateCustomerCommand (Destination member list)
Unmapped properties:
OrganizationId
CustomerId
=============================================
🗺️ Enjoy your coding!
Top comments (0)