AutoMapper:手动设置属性 [英] AutoMapper: manually set property

查看:301
本文介绍了AutoMapper:手动设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AutoMapper从平面DataObject映射到胖BusinessObjects,反之亦然.我注意到从DataObjects到BusinessObjects的映射会花费额外的时间,因为有BusinessObjects的更改通知(带有自定义验证的INotifyPropertyChanged实现等).

I am using AutoMapper to map from flat DataObjects to fat BusinessObjects and vice versa. I noticed that mapping from DataObjects to BusinessObjects takes extra time because of change notification of the BusinessObjects (implements INotifyPropertyChanged with custom validation, etc).

因为通常在映射过程中不需要更改通知,所以我想将其关闭.因此,我添加了一个属性"IsPropertyChangedEnabled".如果将此属性设置为false,则不会引发NotifyPropertyChanged事件,并且不会节省时间.

Because I normally don't need change notification during mapping, I'd like to turn it off. So I added a property "IsPropertyChangedEnabled". If this property is set to false, no NotifyPropertyChanged event is not raised and time is saved.

问题:

我可以告诉AutoMapper在映射过程开始时将此属性设置为false吗?如果可以,怎么办?

Can I tell AutoMapper to set this property to false at the very beginning of the mapping process? If so, how?

谢谢!

推荐答案

在映射过程之前,使用BeforeMap方法设置属性值:

Use BeforeMap method to set property value before mapping process:

Mapper.CreateMap<Source, Destination>()
      .BeforeMap((s, d) => d.IsPropertyChangedEnabled = false );

这篇关于AutoMapper:手动设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆