1
0
Форкнуть 0
Перейти к файлу
Henk Kin 6a5c265047
Updated to Automapper 13.0 (#25)
* Updated to Automapper 13.0
Changed CI/CD pipeline to net8.0 for tests
Changed target framework of library to net6.0
Updated other nuget packages to latest stable version
Updated documentation

* Removed explicit .net version from ci/cd pipelines

---------

Co-authored-by: Henk Kin <HenkK@4dotnet.nl>
2024-02-06 10:37:27 +01:00
.github Updated to Automapper 13.0 (#25) 2024-02-06 10:37:27 +01:00
src Updated to Automapper 13.0 (#25) 2024-02-06 10:37:27 +01:00
.editorconfig Create .editorconfig 2019-12-17 08:56:59 +02:00
.gitattributes Added build and release pipeline via appveyor and nuget/myget 2019-12-19 12:55:51 +01:00
.gitignore Added build and release pipeline via appveyor and nuget/myget 2019-12-19 12:55:51 +01:00
AutoMapper.Extensions.EnumMapping.sln Updated to Automapper 13.0 (#25) 2024-02-06 10:37:27 +01:00
AutoMapper.snk Add files via upload 2019-12-17 11:08:49 +02:00
Build.ps1 not needed 2023-01-15 16:57:19 +02:00
Directory.Build.props Converting to github actions 2020-07-09 10:56:04 -05:00
ISSUE_TEMPLATE.md Create ISSUE_TEMPLATE.md 2019-12-17 11:05:14 +02:00
LICENSE Added logic of EnumMapping and associated tests and documentation 2019-12-18 09:50:15 +01:00
Push.ps1 Converting to github actions 2020-07-09 10:56:04 -05:00
README.md Updated to Automapper 13.0 (#25) 2024-02-06 10:37:27 +01:00
icon.png Converting to github actions 2020-07-09 10:56:04 -05:00
nuget.config Added logic of EnumMapping and associated tests and documentation 2019-12-18 09:50:15 +01:00

README.md

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, but it is possible to map by name calling MapByName() or MapByValue().

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();
    }
}