问题
How to configure AutoMapper mapping when I want to use behaviour from UseDestinationValue method, but only when destination property is NOT null.
Something like that:
Mapper.CreateMap<Item, ItemViewModel>()
.ForMember(x => x.Details, _ => _.UseDestinationValue(dontUseWhenNullDestination: true))
EDIT
class ItemDetails {
public string Info { get; set; }
public string ImportantData { get; set; } // only in Domain, not in ViewModel
}
class Item {
public ItemDetails Details { get; set; }
}
class ItemDetailsViewModel {
public string Info { get; set; }
}
class ItemViewModel {
public ItemDetailsViewModel Details { get; set; }
}
Now example of usage. I have a ItemViewModel class and I want to map it to the Item class.
Mapping configuration:
Mapper.CreateMap<Item, ItemViewModel>()
.ForMember(x => x.Details, _ => _.UseDestinationValue())
First case - destination property
Item.Detailsproperty is NOT NULL. Now I want AutoMapper to use this destination instance ofDetailsproperty, because it's not null.And the logic looks like this:
var item = new Item { Details = new Details { Info = "Old text", ImportantData = "Data" } }; var itemViewModel = new ItemViewModel { Details = new DetailsViewModel { Info = "New text" } }; Mapper.Map(itemViewModel, item);AutoMapper, because of presence of
UseDestinationValue, will leave theitem.Detailsinstance and set onlyitem.Details.Infoproperty.Second case - destination property
Item.Detailsproperty is NULL. Now I want AutoMapper not to use this null instance, but create new one. The question is how to configure the mapping to take into account this case?The logic looks like this:
var item = new Item { Details = null }; var itemViewModel = new ItemViewModel { Details = new DetailsViewModel { Info = "New text" } }; Mapper.Map(itemViewModel, item);PROBLEM
Here I have a problem, because after mapping, the
item.Detailsproperty will be null (because of usage ofUseDestinationValuewhich isnullin this case).
REASON
NHibernate, after getting the entity from the database, puts it into a proxy. So the Details property of a loaded object is not of a type: ItemDetails, but ItemDetailsNHibernateProxy - so I have to use this type, when I want to save this existing object to the database later. But if this property is null, then I can't use a null destination value, so Automapper should create a new instance.
Thanks, Chris
回答1:
Had this same problem, but with EF. Cryss' comment about using BeforeMap pointed me in the right direction.
I ended up with code similar to:
In the Configure() method:
Mapper.CreateMap<ItemViewModel, Item>()
.AfterMap((s, d) => { MapDetailsAction(s, d); })
.ForMember(dest => dest.Details, opt => opt.UseDestinationValue());
Then the Action:
Action<ItemViewModel, Item> MapDetailsAction = (source, destination) =>
{
if (destination.Details == null)
{
destination.Details = new Details();
destination.Details =
Mapper.Map<ItemViewModel, Item>(
source.Details, destination.Details);
}
};
回答2:
I think the NullSubstitute option would work for you. See: http://weblogs.asp.net/psteele/archive/2011/03/18/automapper-handling-null-members.aspx
EDIT
Looks like you might need to add a little conditional logic to your mapping for Details (and skip the UseDestinationValue option):
.ForMember(d => d.Details,
o => o.MapFrom(s => s.Details == null ? new ItemDetails() : Mapper.Map<ItemDetailsViewModel, ItemDetails>(s.Details))
回答3:
I had this same problem dealing with NHibernate entities and I found quite simple solution to it.
You should initialize the Details property in the ItemViewModel constructor. This way the destination value is not null ever. Of course this does not work in more complex cases (like abstract classes).
来源:https://stackoverflow.com/questions/12262940/usedestinationvalue-only-when-destination-property-is-not-null