One way to map these objects is:
A couple things of note here. You can only do .ReverseMap() if both objects have the same values. If the objects do not have the same values will need to map them manually.
You will notice that the AGE value is not listed. The names match up so AutoMapper(AM) does this for us automatically. At this point your probably screaming "WAIT!!! You didn't map the lists of strings correctly!!!." Incorrect my dear friend. That is AM magic. You see behind the scenes AM is actually creating those objects and mapping the objects in the same way as I did in the second example.
Okay so a couple side notes. I am not a VB developer. I don't know how to do generics in vb so the code doesn't match. Also the converter I use to convert C# code to VB.NET wouldn't / couldn't convert my code. If it is wrong syntactically. Let me know I will fix it.
Back on point. As you can see behind the scenes AM does the same thing as above it just does it behind the scenes. As long as you write a Mapper for two objects you can map the two as Collections.
So I have mentioned before that I try very hard to follow the Single Responsibility Prinicple.(SRP) What does this have to do with Automapper you ask? Well if in your AutoMapperConfigurator.Configure() method you have several different types of object mappings, then your not following SRP. So I am gonna go over the way I handle that. I will not say this is the best way. I will just say I find it the easiest.
AM's Configure method actually exists on the Profile Object. That being said you inherit from this object and override the Configure method.
Back on point. As you can see behind the scenes AM does the same thing as above it just does it behind the scenes. As long as you write a Mapper for two objects you can map the two as Collections.
So I have mentioned before that I try very hard to follow the Single Responsibility Prinicple.(SRP) What does this have to do with Automapper you ask? Well if in your AutoMapperConfigurator.Configure() method you have several different types of object mappings, then your not following SRP. So I am gonna go over the way I handle that. I will not say this is the best way. I will just say I find it the easiest.
AM's Configure method actually exists on the Profile Object. That being said you inherit from this object and override the Configure method.
Before LinqKit released that extension method I created one for my own use and now you have it if for some reason you cannot use LinqKit's built in method. So in the code above. The Configure() method now gets all of the objects that inherit from Profile and Create an instance of them. Essentially your initializing all of your mappers just like you would if you were to put them all in the Original Configure method.
In conclusion: AM is an excellent tool especially if your mapping many objects from one type to another. With very little setup you can map any object to another. Your code will be cleaner and easier to read in some places.
Robert T. Frey
In conclusion: AM is an excellent tool especially if your mapping many objects from one type to another. With very little setup you can map any object to another. Your code will be cleaner and easier to read in some places.
Robert T. Frey
No comments:
Post a Comment