tags:

views:

553

answers:

1

Hi,

I'm trying to map CustomerDTO with my domain entity ICustomer with AutoMapper. Everything works fine for first inheritance level but not for the others.

I'm using Interfaces for my domain model since concrete types are injected by StructureMap from my LinqToSql Database Infrastructure layer.

public interface IBaseEntity<TPk>
{
    TPk Id { get; }
}

public interface ICustomer : IBaseEntity<int>
{
    string Email { get; set; }
}

[DataContract]
public class CustomerDTO
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Email { get; set; }
}

Now the AutoMapper mapping

Mapper.CreateMap<CustomerDTO, ICustomer>();

Mapper.CreateMap<ICustomer, CustomerDTO>();

Mapper.AssertConfigurationIsValid();

Now where i'm using the mapping

    public CreateCustomerServiceResult CreateCustomer(CustomerDTO customer)
    {
        var result = new CreateCustomerServiceResult();
        try
        {
            var originalMapped = Mapper.DynamicMap<CustomerDTO, ICustomer>(customer);

            var newCustomer = _customerService.CreateCustomer(originalMapped);

            var newMapped = Mapper.DynamicMap<ICustomer, CustomerDTO>(newCustomer);

            result.Customer = newMapped;
        }
        catch (Exception ex)
        {

        }
        return result;
    }

I've got a Dictionnary missing Key Exception on the "Id" property ...

+1  A: 

Got It!

The problem was due to the missing setter of "Id" property of IBaseEntity.

After changing it everything works

public interface IBaseEntity<TPk>
{
    TPk Id { get; get; }
}
Yoann. B
One other thing - no need to do "DynamicMap" - that's only reserved for cases where you don't know the type at compile time. Just use Mapper.Map instead.
Jimmy Bogard