3

I'm trying to map an entity to a enum. As I was searching for a source I found this:

using Should;

public enum OrderStatus : short
{
    InProgress = 0,
Complete = 1
}

public enum OrderStatusDto
{
    InProgress = 0,
Complete = 1
}

[Test]
public void Example()
{
    Mapper.Map<OrderStatus, OrderStatusDto>(OrderStatus.InProgress)
                .ShouldEqual(OrderStatusDto.InProgress);
    Mapper.Map<OrderStatus, short>(OrderStatus.Complete).ShouldEqual((short)1);
    Mapper.Map<OrderStatus, string>(OrderStatus.Complete).ShouldEqual("Complete");
    Mapper.Map<short, OrderStatus>(1).ShouldEqual(OrderStatus.Complete);
    Mapper.Map<string, OrderStatus>("Complete").ShouldEqual(OrderStatus.Complete);
}

but I think this works for only enum-to-enum mapping. because when I try to use .ShouldEqual, intellisense can't find it. In that codeblock, there is a reference that's called Should but I couldn't find its reference anywhere.

Any ideas about how to use automapper to map between enum and entity/class? Any ideas about using Should?

@I updated the question because without seeing the actual code, it's harder to consider a solution. Here is the code snippet that might be needed:

public class ParameterEnum
{
/// <summary>
/// Enum Sayisi: 2650, Son Guncelleme Tarihi: 21.2.2013 09:40:37
/// </summary>
public enum Parameters : int 
{
...
IsEmriTuruIsTalebi = 138, 
<summary>
Adi: Kalite Öneri; ID: 2218; Seviyesi: 3; Aciklamasi: ; Aktif Mi: True
</summary>
...}}

and this is where normal mapping done:

                    isEmriEntity.IsEmriTuruId = (int)ParameterEnum.Parameters.IsEmriTuruIsTalebi;
2
  • ShouldEqual is part of the test that demonstrates this feature. It is not part of AutoMapper and you don't need it. Commented Jul 10, 2013 at 10:10
  • then .ForMember method should work for this one as well? Commented Jul 10, 2013 at 10:25

1 Answer 1

2

You should look into ITypeConverter. Something like this should do the job:

Mapper.CreateMap<OrderStatus, OrderStatusDto>().ConvertUsing(new OrderStatusConverter());

and your converter would look like so:

public class OrderStatusConverter: ITypeConverter<OrderStatus, OrderStatusDto>
{
    public OrderStatusDto Convert(OrderStatus source)
    {
        return (OrderStatusDto)source;
    }
}

That should be enough to apply the same approach to any other cross-type mappings in your DTOs.

EDIT:

On your enum conversion error, using this as an example for clarity (an enum is not a DTO):

    public enum ExampleEnum : short
    {
        SomeValue,
        SomeOtherValue,
        BigValue = 100,
    }

    public enum AnotherEnum
    {
        Foo,
        Bar,
    }

This should make the enum conversion clearer (don't cast to int at all).

    private void Test()
    {
        // Casting to int only works when the value is 0

        // This works (SomeValue = 0)
        AnotherEnum example = (int) ExampleEnum.SomeValue;

        // This won't even compile (SomeOtherValue = 1)
        AnotherEnum example2 = (int) ExampleEnum.SomeOtherValue;

        // Casting to another enum works fine
        AnotherEnum example2 = (AnotherEnum) ExampleEnum.SomeOtherValue;

        // Just be careful of values that don't exist in the target enum
        // This will compile even though it won't work at run-time (BigValue = 100)
        AnotherEnum example2 = (AnotherEnum) ExampleEnum.BigValue;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. The int cast was wrong. I have VisualStudio handy now so was able to check instead of just 'coding' in the browser. Hope the answer's clearer now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.