国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

AutoMapper——最佳實踐

2019-11-06 06:34:09
字體:
來源:轉載
供稿:網友

通過配置文件控制要加載那些定義模型map的文件

指定map文件所在的程序集

然后通過Mapper的初始化方法添加

  <autoMapper>    <!--鍵為程序集簡稱,可隨意命名,不重復即可,值為程序集名稱-->    <add key="Search" value="MyPRoject.Search.Mongo" />    <add key="Semantics" value="MyProject.Semantics.SQL" />  </autoMapper>接下來定義一個加載這些程序集下面繼承Profile的派生類的方法

    public static void LoadConfig(string sectionName = "autoMapper")        {            var config = ConfigurationManager.GetSection(sectionName).As<NameValueCollection>();            var assemlies = config.Keys                .Cast<string>()                .Select(e => config[e])                .Where(e => !e.IsNullOrEmpty())                .ToArray();            Mapper.Initialize(cfg => cfg.AddProfiles(assemlies));            Mapper.AssertConfigurationIsValid();        }然后如果是MVC項目,在global.asax中的 application_Start方法中調用上面的方法。

接下來只需要在需要模型map的地方預先創建模型之間的map關系

  /// <summary>AutoMapper映射配置</summary>    public class AutoMapperProfile : Profile    {        /// <summary>構造函數</summary>        public AutoMapperProfile()        {            CreateMap<Definition, DefDTO>()                .ForMember(t => t.Enabled, opt => opt.Ignore())                ;        }    }

在定義模型映射中的MapFrom 和ResolveUsing的區別

參數不同

MapFrom更適用于屬性直接轉化,會檢查 是否為null

ResolveUsing 更適合復雜的屬性轉化,不會檢查 是否為null

如果使用MapFrom在轉化的過程中遇到拋出的null相關的異常,它會自動處理該異常,使之無法知道內部發生錯誤。

這是可以自定義resolver ,并使用ResolveUsing進行映射轉化

下面來自參考:http://blog.travisgosselin.com/automapper-mapfrom-vs-resolveusing/

When specifying the mapping creation between two objects you have two possible approaches that are often used interchangeably by some without understanding the difference:You can use the “ResolveUsing” method:Mapper.CreateMap<SourceType, DestType>().ForMember(d => d.DestPropX, o => o.ResolveUsing(s => s.SourcePropY));You can use the “MapFrom” methodMapper.CreateMap<SourceType, DestType>().ForMember(d => d.DestPropX, o => o.MapFrom(s => s.SourcePropY));In 90% of the time, these are going to and do the exact same thing, and you won’t have to worry. However, if you are doing a more complex flattening exercise for a property that won’t flatten by convention the “MapFrom” includes NULL checks all the way through the object graph. For example:ForMember(d => d.DestPropX, o => o.MapFrom(s => s.Person.Address.State));This scenario will not blow up your application if Person or Address is NULL, but instead NULL will be mapped over to the DestPropX safely. AutoMapper automatically catches the thrown NULL reference exception and hides it from you. That sounds AWESOME. But you need to be aware of the overhead price you pay in performance for using Exceptions during your normal workflow of your application.Consider the scenario where I have a list of 1,000 objects to convert, all with a NULL Person property, in which cases we are looking at 1,000 Null reference exceptions that must be eatin’ inside your app. In my own experience, under the wrong scenario where this was multiplied many times, I had a single web service call that took about 10 sec. Upon investigating, I found that due to the number of exceptions thrown for NULL checks, my web service transformation was taking an extra 5 sec. I could easily get my 50% performance boost by doing some manual NULL checks with ResolveUsing:ForMember(d => d.DestPropX, o => o.ResolveUsing(s => s.Person != null ? s.Person.Address.State : null ));By manually doing my null checks, I have avoided the onslaught of exceptions, and get the large performance boost.While that is one large workflow difference, often the ResolvingUsing is used for custom logic that occurs during the mapping, while the MapFrom is generally reserved for flattening Operations. If you expect to never have a null, then you may consider using ResolveUsing in order to get an exception that bubbles up when it occurs, and lets you know there is likely a problem. This way the problem is not hidden from you with the large performance hit on the side.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高尔夫| 宿松县| 温泉县| 疏勒县| 乐东| 克东县| 竹山县| 马关县| 平安县| 蒙自县| 仁寿县| 黎川县| 楚雄市| 红桥区| 茂名市| 旅游| 绵竹市| 泗阳县| 宁化县| 荔浦县| 普定县| 山阴县| 富锦市| 陵川县| 龙井市| 项城市| 万山特区| 巨野县| 嘉鱼县| 盱眙县| 揭阳市| 蕉岭县| 竹溪县| 宜兰市| 湄潭县| 海南省| 万荣县| 金平| 桓台县| 东乡县| 环江|