Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper Enum to byte with implemention IMapperConfigurator

Enum definition is

public enum RowStatusEnum
{
    Modified = 1,
    Removed = 2,
    Added = 3
}

public class RowStatusEnumConvertor : IMapperConfigurator
{
    public void Cofigure()
    {
        Mapper.CreateMap<RowStatusEnum, byte>();
        Mapper.CreateMap<byte, RowStatusEnum >();
    }
}

I config autoMapper with Implemention IMapperConfigurator in RowStatusEnumConvertor class, but not work this code and not map this type, i think my config not correct or not enough, please help me

thanks

like image 452
Mohammad Akbari Avatar asked Jan 01 '13 05:01

Mohammad Akbari


People also ask

Can AutoMapper map enums?

Alternatively, AutoMapper supports convention based mapping of enum values in a separate package AutoMapper.

How do I test AutoMapper mappings?

To test our configuration, we simply create a unit test that sets up the configuration and executes the AssertConfigurationIsValid method: var configuration = new MapperConfiguration(cfg => cfg. CreateMap<Source, Destination>()); configuration. AssertConfigurationIsValid();


2 Answers

Will something like this work for you?

Classes.cs

namespace StackOverflow.RowStatus
{
    public enum RowStatusEnum
    {
        Modified = 1,
        Removed = 2,
        Added = 3
    }
}

AutoMapperConfigurator.cs

namespace StackOverflow.RowStatus
{
    using System;
    using System.Linq;

    using AutoMapper;

    public class MyProfile : Profile
    {
        protected override void Configure()
        {
            Mapper.CreateMap<byte, RowStatusEnum>().ConvertUsing(
                x => Enum.GetValues(typeof(RowStatusEnum))
                         .Cast<RowStatusEnum>().First(y => (byte)y == x));
            Mapper.CreateMap<RowStatusEnum, byte>().ConvertUsing(
                x => (byte)x);
        }
    }
}

MappingTests.cs

namespace StackOverflow.RowStatus
{
    using AutoMapper;

    using NUnit.Framework;

    [TestFixture]
    public class MappingTests
    {
        [Test]
        public void AutoMapper_Configuration_IsValid()
        {
            Mapper.Initialize(m => m.AddProfile<MyProfile>());
            Mapper.AssertConfigurationIsValid();
        }

        [TestCase(1, Result = RowStatusEnum.Modified)]
        [TestCase(2, Result = RowStatusEnum.Removed)]
        [TestCase(3, Result = RowStatusEnum.Added)]
        public RowStatusEnum AutoMapper_ConvertFromByte_IsValid(
                                                   byte rowStatusEnum)
        {
            Mapper.Initialize(m => m.AddProfile<MyProfile>());
            Mapper.AssertConfigurationIsValid();
            return Mapper.Map<byte, RowStatusEnum>(rowStatusEnum);
        }

        [TestCase(RowStatusEnum.Modified, Result = 1)]
        [TestCase(RowStatusEnum.Removed, Result = 2)]
        [TestCase(RowStatusEnum.Added, Result = 3)]
        public byte AutoMapper_ConvertFromEnum_IsValid(
                                                   RowStatusEnum rowStatusEnum)
        {
            Mapper.Initialize(m => m.AddProfile<MyProfile>());
            Mapper.AssertConfigurationIsValid();
            return Mapper.Map<RowStatusEnum, byte>(rowStatusEnum);
        }
    }
}
like image 58
Mightymuke Avatar answered Sep 28 '22 08:09

Mightymuke


Starting from version 9 of Automapper there were changes in initialization which caused to be out of date above examples. I updated above example and left original values to have also reference to older version (NUnit 3.12.0):

RowStatusEnum

namespace StackOverflow.RowStatus
{
    public enum RowStatusEnum
    {
        Modified = 1,
        Removed = 2,
        Added = 3
    }
}

AutoMapperConfigurator.cs

namespace StackOverflow.RowStatus
{
    using System;
    using System.Linq;

    using AutoMapper;

    public class MyProfile : Profile
    {
        public MyProfile()
        {
            CreateMap<byte, RowStatusEnum>().ConvertUsing(
                x => Enum.GetValues(typeof(RowStatusEnum))
                    .Cast<RowStatusEnum>().First(y => (byte)y == x));
            CreateMap<RowStatusEnum, byte>().ConvertUsing(
                x => (byte)x);
        }
    }
}

MappingTests

namespace StackOverflow.RowStatus
{
    using AutoMapper;

    using NUnit.Framework;

    [TestFixture]
    public class MappingTests
    {
        [Test]
        public void AutoMapper_Configuration_IsValid()
        {
            var config = new MapperConfiguration(cfg => cfg.AddProfile<MyProfile>());
            config.AssertConfigurationIsValid();
        }

        [TestCase(1, ExpectedResult = RowStatusEnum.Modified)]
        [TestCase(2, ExpectedResult = RowStatusEnum.Removed)]
        [TestCase(3, ExpectedResult = RowStatusEnum.Added)]
        public RowStatusEnum AutoMapper_ConvertFromByte_IsValid(
            byte rowStatusEnum)
        {
            var config = new MapperConfiguration(cfg => cfg.AddProfile<MyProfile>());
            var mapper = config.CreateMapper();
            return mapper.Map<byte, RowStatusEnum>(rowStatusEnum);
        }

        [TestCase(RowStatusEnum.Modified, ExpectedResult = 1)]
        [TestCase(RowStatusEnum.Removed, ExpectedResult = 2)]
        [TestCase(RowStatusEnum.Added, ExpectedResult = 3)]
        public byte AutoMapper_ConvertFromEnum_IsValid(
            RowStatusEnum rowStatusEnum)
        {
            var config = new MapperConfiguration(cfg => cfg.AddProfile<MyProfile>());
            var mapper = config.CreateMapper();
            return mapper.Map<RowStatusEnum, byte>(rowStatusEnum);
        }
    }
}
like image 23
Krzysztof Madej Avatar answered Sep 28 '22 09:09

Krzysztof Madej