Skip to content

Repository files navigation

MagicMapper.Extensions.EnumMapping

CINuGetNuGetMyGet (dev)

Summary

The MagicMapper.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 MagicMapper.Extensions.EnumMapping

You should install MagicMapper.Extensions.EnumMapping with NuGet:

Install-Package MagicMapper.Extensions.EnumMapping

Or via the .NET Core command line interface:

dotnet add package MagicMapper.Extensions.EnumMapping

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

Usage

Install via NuGet first: Install-Package MagicMapper.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.

usingAutoMapper.Extensions.EnumMapping;publicenumSource{Default=0,First=1,Second=2}publicenumDestination{Default=0,Second=2}internalclassYourProfile:Profile{publicYourProfile(){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

MagicMapper 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:

publicclassMappingConfigurationsTests{[Fact]publicvoidWhenProfilesAreConfigured_ItShouldNotThrowException(){// Arrangevarconfig=newMapperConfiguration(configuration =>{configuration.EnableEnumMappingValidation();configuration.AddMaps(typeof(AssemblyInfo).GetTypeInfo().Assembly);});// Assertconfig.AssertConfigurationIsValid();}}

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages