I have a domain object
public class ProductModel
{
public long Id {get;set;}
public string Name {get;set;}
public string SerialNumber {get;set;}
}
Single Dto class:
public class ProductDto
{
public long Id {get;set;}
public string Name {get;set;}
public string SerialNumber {get;set;}
}
Single Dto class that is a list of Dto object:
public class ProductListDto : List<ProductDto>
{
public List<ProductDto> Products;
public ProductListDto()
{
Products = new List<ProductDto>();
}
}
And I'd like to map a list of domain objects to list of Dto objects such that the "Products" property of ProductListDto object AUTOMATICALLY is mapped with a list of ProductModel objects:
ProductListDto dto = new ProductListDto();
Mapper.CreateMap<ProductModel, ProductDto>();
/* dto = (ProductListDto) Mapper.Map<List<ProductModel>, List<ProductDto>>((List<ProductModel>)model); this code line causes error. It is commented out. */
dto.Products = Mapper.Map<List<ProductModel>, List<ProductDto>>((List<ProductModel>)model); // (*) works OK but need to specify "Products" property
The code line (*) works OK, but I'd like to know if there is another way to AUTOMATICALLY (implicitly) map that "Products" property of dto object other than the code line (*)?
That means I do not have to write code like the left hand side of code line (*).
You will need to create a mapping for it. Something like this should work:
Then in your code you can do:
Here are a couple of unit tests to show how it works: