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; }
[Data开发者_运维技巧Member]
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 ...
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; set; }
}
精彩评论