Reputation: 3
I'm facing a issue with mapping my objects to the view and back to the original object with automapper.
I have following structure.
public class TModel
{
public int TID{get;set;}
public string Name{get;set;}
public string Location{get;set;}
public string address{get;set;}
}
public class UiModel
{
public string Name{get;set;}
public string Location{get;set;}
}
in the class AutoMapperConfiguration i have a initialize of my automapper like following:
cfg.CreateMap < TModel, UiModel >();
cfg.CreateMap < UiModel,TModel > ();
my adapter recieves data from a webservice, this data i wil store in a list of TModel items The view only show items of UiModel, i will do the following mapping en return the data to the view
var list = listOfT.Select(tmodel=> Mapper.Map<UiModel>(tmodel)).ToList();
when all of the changes are made i give the list back to a different adapter to save the data into the database
in this 'sqlAdapter' i map the items back of type TModel
, because i also want the address property in my database
var list = listOfUi.Select(uimodel=> Mapper.Map<TModel>(uimodel)).ToList();
but now i have lost the data of the address property. must i also pass throug the original list of TModel's?...
Upvotes: 0
Views: 4282
Reputation: 133
There's a couple different ways you could solve this. The simplest answer would be to modify your UiModel class so that it has an address property. So something like:
public class UiModel
{
public string Name{get;set;}
public string Location{get;set;}
public string address {get;set;}
}
Automapper works by first trying to match property names. If it doesn't find a match then you have to either modify your class so that it finds a match or write a class that implements the IValueResolver interface. More on that can be found at https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-resolvers
Upvotes: 1