I try to answer to my own question: feel free to correct me if there is anything which doesn't sound good to you.
I will use the following words:
layer: in this context a layer is a module of the application in the vertical sense (from the database to the user interface)
module: an area of your application in the horizontal sense, for example "CRM", "Product", Accounting",... each one having it's code in different layers.
If you have an application divided in n layers and n modules, the follwing could be an example for a specific class of a particular module and layer.
public static class ProductMapper
{
static ProductMapper()
{
MapProductBDOToDTO();
MapProductDTOToBDO();
MapProductCategoryBDOToDTO();
MapProductCategoryDTOToBDO();
MapIvaBDOToDTO();
MapIvaDTOToBDO();
MapProductSupplierBDOToDTO();
MapProductSupplierDTOToBDO();
MapProductPictureBDOToDTO();
MapProductPictureDTOToBDO();
MapProductNoteBDOToDTO();
MapProductNoteDTOToBDO();
MapStockProductBDOToDTO();
MapStockProductDTOToBDO();
MapTagBDOToDTO();
MapTagDTOToBDO();
}
public static TTargetType Convert<TToConvert, TTargetType>(TToConvert toConvert)
{
return Mapper.Map<TTargetType>(toConvert);
}
private static void MapProductDTOToBDO()
{
Mapper.CreateMap<ProductDTO, ProductBDO>();
}
private static void MapProductBDOToDTO()
{
Mapper.CreateMap<ProductDTO, ProductBDO>().ReverseMap();
}
private static void MapProductCategoryDTOToBDO()
{
Mapper.CreateMap<ProductCategoryDTO, ProductCategoryBDO>();
}
private static void MapProductCategoryBDOToDTO()
{
Mapper.CreateMap<ProductCategoryBDO, ProductCategoryDTO>();
}
private static void MapIvaDTOToBDO()
{
Mapper.CreateMap<IvaDTO, IvaBDO>();
}
private static void MapIvaBDOToDTO()
{
Mapper.CreateMap<IvaBDO, IvaDTO>();
}
private static void MapProductSupplierDTOToBDO()
{
Mapper.CreateMap<ProductSupplierDTO, ProductSupplierBDO>();
}
private static void MapProductSupplierBDOToDTO()
{
Mapper.CreateMap<ProductSupplierDTO, ProductSupplierBDO>().ReverseMap();
}
private static void MapProductPictureDTOToBDO()
{
Mapper.CreateMap<ProductPictureDTO, ProductPictureBDO>();
}
private static void MapProductPictureBDOToDTO()
{
Mapper.CreateMap<ProductPictureDTO, ProductPictureBDO>().ReverseMap();
}
private static void MapProductNoteDTOToBDO()
{
Mapper.CreateMap<ProductNoteDTO, ProductNoteBDO>();
}
private static void MapProductNoteBDOToDTO()
{
Mapper.CreateMap<ProductNoteDTO, ProductNoteBDO>().ReverseMap();
}
private static void MapStockProductDTOToBDO()
{
Mapper.CreateMap<StockProductDTO, StockProductBDO>();
}
private static void MapStockProductBDOToDTO()
{
Mapper.CreateMap<StockProductDTO, StockProductBDO>().ReverseMap();
}
private static void MapTagDTOToBDO()
{
Mapper.CreateMap<TagDTO, TagBDO>();
}
private static void MapTagBDOToDTO()
{
Mapper.CreateMap<TagDTO, TagBDO>().ReverseMap();
}
As you can see it's a static class with a static constructor, which means that the constructor will be called not more than ONCE during the application life time.
The first time you call Convert method, the constructor is called and all mappings are created.
The second, third, fourth... n time you will cal the Convert method during the same session, the constructor will not be called.
Advantages:
- A particular mapping can not be created more than once
- Mappings are created only when you need them (in this case, when you work with Products). You don't create 1000 mappings at application start for then using only 5 or 6 of them.
Disadvantages:
- A developer can call the generic Convert method with all types he/she wants, types are not checked. By the way using a generic method requires less code, and if mappings have not been created yet an exception will be thrown by the AutoMapper.