Posts

Showing posts from March, 2023

Validate Automapper configurations with AssertConfigurationIsValid

Automapper permit to map a class on another in order to transform source object into a destination object. We can do this transformation declaring mappings into a file for example MyProfile1. We could have complex transformation for each property of the object or we can use an automapper behaviour that permit to copy the contents of property between the objects if thay have the same name property. Just with this possibility that allows us to save lines of codes, we could find a problem. If we rename one of the property the copy will be lost, and we'll find this problem at runtime. To overcome this problem, we can use the verification feature of AutoMapper, AssertConfigurationIsValid . We put this line of code in program.cs, at the start of the application to check mappings. var mapper = app.Services.GetService (); mapper.ConfigurationProvider.AssertConfigurationIsValid(); For example if we have this two classes public class Source { public string Name { get; set;