Repost: Noisy code does not equal clean code
When using AutoMapper to map objects from one type to another in your code you need to implement something like below:
var someMappedDto = Mapper.Map<SomeModel, SomeDto>(someModelInstance);If you look at the code there is a bit of noise here in my opinion. So what do I mean by noise? I consider anything in my code that gets in my way or does not provide me direct feedback as noise.
In the line above I consider the following noise:
- Mapper.Map – I do not like this because having Mapper.Map sprinkled all over my code base just means that my code has too much knowledge of the actually mapping library we use, this is noise. Hey, it was for this basic concept that the Common Service Locator was created to help prevent
- Having to provide both the source type AND the destination type – The source type should be able to be derived from the source object instance (in cases where you do not want to use the mapper for some child type of the source type). I concede that you do need to provide the destination type other wise we would have no clue what you want in the end
- Having to provide the source type instance in the constructor.
In order to reduce the noise, and make for cleaner code I would like to see something like
var someMappedDto = someModelInstance.Map<SomeDto>();
via devlicio.us
This feels nit-picky but it is something to keep in mind. Especially when using a bunch of generics. C# Extensions are your friend.