I'm just getting to grips with AutoMapper and love how it works. However I believe it can map some complex scenarios that I'm currently wiring up manually. Does anyone have any suggestions / tips to remove my manual processes from the below simplified example and accelerate my learning curve?
I have a source object like so:
public class Source
{
public Dictionary<string, object> Attributes;
public ComplexObject ObjectWeWantAsJson;
}
and a destination object like so:
public class Destination
{
public string Property1; // Matches with Source.Attributes key
public string Property2; // Matches with Source.Attributes key
// etc.
public string Json;
}
My configuration for AutoMapper is minimal:
var config = new MapperConfiguration(cfg => {});
var mapper = config.CreateMapper();
And my conversion code is like so:
var destinationList = new List<Destination>();
foreach (var source in sourceList)
{
var dest = mapper.Map<Dictionary<string, object>, Destination(source.Attributes);
// I'm pretty sure I should be able to combine this with the mapping
// done in line above
dest.Json = JsonConvert.SerializeObject(source.ObjectWeWantAsJson);
// I also get the impression I should be able to map a whole collection
// rather than iterating through each item myself
destinationList.Add(dest);
}
Any pointers or suggestions are most appreciated. Thanks in advance!