Reputation: 636
Suppose I have the following structure:
public class EntityDto
{
int EntityId { get; set; }
}
public class Entity
{
EntityId<int> EntityId { get; }
}
public class EntityId<T>
{
T Id { get; set; }
}
What is the appropriate way for defining the mapping profile in this case? Both directions, of course. Any help will be greatly appreciated.
Upvotes: 1
Views: 196
Reputation: 37770
Assuming, that your classes look like this:
class EntityDto
{
public int EntityId { get; set; }
}
class EntityId<T>
{
public T Id { get; set; }
}
class Entity
{
public EntityId<int> EntityId { get; set; }
}
mapping will look like this:
static void CreateMapForEntityId<T>()
{
Mapper.CreateMap<T, EntityId<T>>()
.ForMember(_ => _.Id, options => options.MapFrom(_ => _));
}
static void TestMapping(int id)
{
CreateMapForEntityId<int>();
Mapper
.CreateMap<Entity, EntityDto>()
.ForMember(_ => _.EntityId, options => options.MapFrom(_ => _.EntityId.Id))
.ReverseMap();
Mapper.AssertConfigurationIsValid();
var entity = new Entity { EntityId = new EntityId<int> { Id = id } };
var entityDto = Mapper.Map<EntityDto>(entity);
Debug.Assert(entityDto.EntityId == id);
var entityClone = Mapper.Map<Entity>(entityDto);
Debug.Assert(entityClone.EntityId.Id == id);
}
Basically, there won't be any "auto"-mapping.
Upvotes: 1