AutoMapper.Extensions.EnumMapping 4.1.0

AutoMapper.Extensions.EnumMapping

CI NuGet NuGet MyGet (dev)

Summary

The AutoMapper.Extensions.EnumMapping library gives you control about your enum values mappings. It is possible to create a custom type converter for every enum.

This library supports mapping enums values like properties.

Dependencies

Installing AutoMapper.Extensions.EnumMapping

You should install AutoMapper.Extensions.EnumMapping with NuGet:

Install-Package AutoMapper.Extensions.EnumMapping

Or via the .NET Core command line interface:

dotnet add package AutoMapper.Extensions.EnumMapping

Either commands, from Package Manager Console or .NET Core CLI, will download and install AutoMapper.Extensions.EnumMapping. AutoMapper.Extensions.EnumMapping has no dependencies.

Usage

Install via NuGet first: Install-Package AutoMapper.Extensions.EnumMapping

To use it:

For method CreateMap this library provide a ConvertUsingEnumMapping method. This method add all default mappings from source to destination enum values.

If you want to change some mappings, then you can use MapValue method. This is a chainable method.

Default the enum values are mapped by value (MapByValue()), but it is possible to map by name calling MapByName(). For enums which does not have same values and names, you can use MapByCustom(). Then you have to add a MapValue for every source enum value.

using AutoMapper.Extensions.EnumMapping;

public enum Source
{
    Default = 0,
    First = 1,
    Second = 2
}

public enum Destination
{
    Default = 0,
    Second = 2
}

internal class YourProfile : Profile
{
    public YourProfile()
    {
        CreateMap<Source, Destination>()
            .ConvertUsingEnumMapping(opt => opt
		// optional: .MapByValue() or MapByName(), without configuration MapByValue is used
		.MapValue(Source.First, Destination.Default))
            .ReverseMap(); // to support Destination to Source mapping, including custom mappings of ConvertUsingEnumMapping
    }
}
    ...

Testing

AutoMapper provides a nice tooling for validating typemaps. This library adds an extra EnumMapperConfigurationExpressionExtensions.EnableEnumMappingValidation extension method to extend the existing AssertConfigurationIsValid() method to validate also the enum mappings.

To enable testing the enum mapping configuration:


public class MappingConfigurationsTests
{
    [Fact]
    public void WhenProfilesAreConfigured_ItShouldNotThrowException()
    {
        // Arrange
        var config = new MapperConfiguration(configuration =>
        {
            configuration.EnableEnumMappingValidation();

            configuration.AddMaps(typeof(AssemblyInfo).GetTypeInfo().Assembly);
        });
		
        // Assert
        config.AssertConfigurationIsValid();
    }
}

No packages depend on AutoMapper.Extensions.EnumMapping.

.NET 8.0

Version Downloads Last updated
4.1.0 1 03/09/2025
4.0.0 7 02/23/2025
3.2.0 29 03/12/2024
3.1.0 28 05/17/2023
3.0.1 48 10/12/2022
3.0.0 38 10/12/2022
2.0.1 1,961 06/27/2022
1.1.0 2,136 06/27/2022
1.0.0 32 08/12/2022