I want to be able to do something like:
FluentMapper m = new FluentMapper();
//some test data
ProjectDTO dto = new ProjectDTO() {...A List of Properties...};
Project entity = new Project();
List<ProjectDTO> listOfDTOs = new List<ProjectDTO>(){dto};
List<Project> listOfEntities = new List<Project>(){entity};
//Mapping from DTO to entity
m.Map<ProjectDTO>(dto).To<Project>(entity);
m.Map<ProjectDTO>(listOfDTOs).To<Project>(listOfEntities);
//and vice versa
m.Map<Project>(entity).To<ProjectDTO>(dto);
m.Map<Project>(listOfEntities).To<ProjectDTO>(listOfDTOs);
i.e. basically using the same Method Map(item or a list of items).To(item or list of items) I can map between Entity or a list of Entities to a DTO or a list of DTOs. And if there are no mapping defined between ProjectDTO and Project it will throw an exception. If doing a list is way more complex, I'm cool with just looping them through manually.
I'm not sure how I could use Ninject to give me the correct Mapper base on the the From type and To type. I was thinking of using a factory to build mapper base on the two types, but not sure how that can be done either.
Currently I'm using a mapper per entity/DTO pair, but it becomes clunky when I need to use more than one mapper in a class, where I've to inject multiple mappers into. Our Mapper class is like this (which I'm comfortable changing to whatever that will work).
public class ProjectMapper<E, D> : IEntityDTOMapper<Project, ProjectDTO>
where E : Project
where D : ProjectDTO
{
public ProjectDTO Map(Project entity)
{
return new ProjectDTO()
{
... Mapping properties...
};
}
public List<ProjectDTO> Map(List<Project> entities)
{
List<ProjectDTO> dtos = new List<ProjectDTO>();
foreach (Project entity in entities)
{
dtos.Add(Map(entity));
}
return dtos;
}
public Project Map(ProjectDTO dto)
{
return new Project()
{
... Mapping properties...
};
}
public List<Project> Map(List<ProjectDTO> dtos)
{
List<Project> entities = new List<Project>();
foreach (ProjectDTO dto in dtos)
{
entities.Add(Map(dto));
}
return entities;
}
I've thought about using AutoMapper, but it seems like an overkill and I also want to learn some advanced Ninject techniques.
Thank You.