Click here to Skip to main content
15,887,928 members
Articles / Programming Languages / C#
Tip/Trick

Validate Automapper Configurations with AssertConfigurationIsValid

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Mar 2023CPOL2 min read 7.2K   5  
How to validate Automapper configuration using AssertConfigurationIsValid
This tip gives a solution by using the verification feature of AutoMapper, AssertConfigurationIsValid.

Introduction

Automapper permits 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 transformations for each property of the object or we can use an automapper behaviour that permits to copy the contents of properties between the objects if they 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 properties, the copy will be lost, and we'll find this problem at runtime.

Using the Code

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.

C#
var mapper = app.Services.GetService<IMapper>();
mapper.ConfigurationProvider.AssertConfigurationIsValid();

For example, if we have these two classes:

C#
public class Source
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public string DescriptionRole { get; set; }
}

public class Destination
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Role { get; set; }
}

We renamed into the class Source the property DescriptionRole in Role. If your mapping is:

C#
public class MyProfile1
{
    public MyProfile1()
    {
        CreateMap<Source, Destination>()
            .ForMember(dest => dest.Surname, opt
                => opt.MapFrom(src => src.Surname))
            .ForMember(dest => dest.Name, opt
                => opt.MapFrom(src => src.Name));
    }
}

The error when you start the application will be:

Quote:

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
==========================================================
Source -> Destination (Destination member list)
MyNameSpace.Source -> MyNameSpace.Destination(Destination member list)

Unmapped properties:
DescriptionRole

If you don't want to map this property, you can use Ignore.

C#
CreateMap<Source, Destination>()
    .ForMember(dest => dest.Surname, opt
        => opt.MapFrom(src => src.Surname))
    .ForMember(dest => dest.Name, opt
        => opt.MapFrom(src => src.Name)
    .ForMember(dest => dest.Role, opt =>
        opt.Ignore());

Another problem occurred when you define CreateMap multiple times in the same Profile class or another, the automapper uses the last one loaded, but they could be different from each other.

Quote:

With AssertConfigurationIsValid, if tou define the same trasformation in MyProfile1 and MyProfile2, the error will be
The following type maps were found in multiple profiles:
MyNamespace.Source to MyNamespace.Destination defined in profiles:
MyNamespace.MyProfile1
MyNamespace.MyProfile2

History

  • 13th March, 2023: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Italy Italy
Alessandro is a software developer, graduated at politecnico of Milan with the passion for software programming, TDD and music.

Comments and Discussions

 
-- There are no messages in this forum --