I have two models for my form, a ViewModel going to it, and a ControlModel coming from it. The ControlModel has all the same field names and hierarchy, but all of the fields are a string data type.
How would you code AutoMapper to convert a string field to integer? I tried Int32.Parse(myString) but Int32 is not available within the expression (gives an error).
Mapper.CreateMap<SourceClass, DestinationClass>()
.ForMember(dest => dest.myInteger,
opt => opt.MapFrom(src => src.myString));
The types in the class and their corresponding conversion types:
string to int, int?, double, double?, DateTime, and bool
Additionally, is there any way to generalize mappings in a way that all integers in the target are parsed with that function? In other words, is there a way to create mappings for data types?
EDIT:
This looks promising:
AutoMapper.Mapper.CreateMap<string, int>()
.ConvertUsing(src => Convert.ToInt32(src));
EDIT: This post is really helpful
I ended up doing something like this:
And the classes it references (first draft):
You could create properties in your source class that cast fields to the types as they exist in the destination. Then use AutoMapper in a plain vanilla manner.