0

I've got a custom adapter pattern like so

public interface IFoo
{
     byte[] Content{get; set;}
}

public class FooDTO : IFoo
{
    byte[] Content {get; set;}
}

public FooAdapter : IFoo
{
    Picture _picture;
    public FooAdapter(Picture picture)
    {
        this._picture = picture;
    }
   
    public byte[] Content => this._picture.GetContent();
}

Then I Have an Extension method to convert my entities to a dto like so

public static FooDTO ToDTO(this IFoo getter)
{
    return AutoMapperExtensions.Map<IFoo, FooDTO >(getter);            
}

This adapter adapters between the same interface IFoo, Occasionally I Get AutoMapperMappingExceptions

Mapping types: FooAdapter -> Byte[] MyNamespace.FooAdapter -> System.Byte[]

Destination path: FooDTO.Content

Source value: MyNamespace.FooAdapter

All that information is nice but how can I get the property name of the property that has thrown the error?

Community
  • 1
  • 1
johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • Just a quick question, why are you trying to map the values of a property that already has a getter (i.e. that is returning a value)? – DOMZE Apr 13 '17 at 19:48
  • @DOMZE This adapter pattern has multiple benefits. 1. by doing so you don't have to have the same names of properties between your DTO and your Entity. 2. This provides an additional layer between mapping so you can add logic, for instance you may not want to map you're Id because they are set from the server and it could cause a bug if someone modified you're dto's Id etc – johnny 5 Apr 13 '17 at 19:53
  • My bad, I misread that the destination is not your entity ;-) – DOMZE Apr 13 '17 at 19:57
  • Sorry yeah that was misleading I've edited the question to make more sense – johnny 5 Apr 13 '17 at 20:21

1 Answers1

1

Incase anyone was wondering you can access the Member name through the error context.

catch(AutoMapperMappingException mappingException)
{
    BuildNewError(mappingException.Context.MemberName);
}
johnny 5
  • 19,893
  • 50
  • 121
  • 195