【AutoMapperExtension】智慧并不产生于学历,而是来自对于知识的终生不懈的追求。这篇文章主要讲述AutoMapperExtension相关的知识,希望能为你提供帮助。
using System; using System.Collections.Generic; using System.Linq; using AutoMapper; using System.Collections; namespace DanaZhangCms.Domain.AutoMapper { public static class AutoMapperExtension {/// < summary> /// 集合对集合 /// < /summary> /// < typeparam name="TResult"> < /typeparam> /// < param name="self"> < /param> /// < returns> < /returns> public static List< TResult> MapTo< TResult> (this IEnumerable self) { if (self == null) throw new ArgumentNullException(); return (List< TResult> )Mapper.Map(self, self.GetType(), typeof(List< TResult> )); }/// < summary> /// 对象对对象 /// < /summary> /// < typeparam name="TResult"> < /typeparam> /// < param name="self"> < /param> /// < returns> < /returns> public static TResult MapTo< TResult> (this object self) { if (self == null) throw new ArgumentNullException(); return (TResult)Mapper.Map(self, self.GetType(), typeof(TResult)); }///// < summary> ///// 自动Map ///// < para> 此方式极易覆盖预期的【已Map】的设置,调用前请确定映射从未被创建< /para> ///// < /summary> ///// < typeparam name="TResult"> < /typeparam> ///// < param name="self"> < /param> ///// < returns> < /returns> //[Obsolete("此方式极易覆盖预期的【已Map】的设置,调用前请确定映射从未被创建", false)] //public static TResult AutoMapTo< TResult> (this object self) //{ //if (self == null) throw new ArgumentNullException(); //Mapper.CreateMap(self.GetType(), typeof(TResult)); //return (TResult)Mapper.Map(self, self.GetType(), typeof(TResult)); //}/// < summary> /// /// < /summary> /// < typeparam name="TSource"> < /typeparam> /// < typeparam name="TResult"> < /typeparam> /// < param name="result"> < /param> /// < param name="source"> < /param> /// < returns> < /returns> public static void MapFrom< TSource, TResult> (this TResult result, TSource source) { result = Mapper.Map< TSource, TResult> (source, result); }} }
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AutoMapper; using DanaZhangCms.Domain.ViewModel; using DanaZhangCms.Domain.DbModels; using DanaZhangCms.DbModels; namespace DanaZhangCms.Domain.AutoMapper { public class Configuration { public static void RegisterConfigure() { #region Mapper.Initialize( cfg => { cfg.CreateMap< ChannelViewModel, Channel> (); cfg.CreateMap< Channel, ChannelViewModel> (); cfg.CreateMap< ArticleCategoryViewModel, ArticleCategory> (); cfg.CreateMap< ArticleCategory, ArticleCategoryViewModel> (); cfg.CreateMap< ArticleViewModel, Article> (); cfg.CreateMap< Article, ArticleViewModel> (); cfg.CreateMap< Banner, BannerViewModel> (); cfg.CreateMap< BannerViewModel, Banner> (); } ); #endregion } } }
DanaZhangCms.Domain.AutoMapper.Configuration.RegisterConfigure();
推荐阅读
- android防反编译技术初体验——混淆
- Android编程入门--底部Dialog弹窗
- Flask AppBuilder Model Views
- Android编程入门--android.support.v7.widget.Toolbar
- Android学习笔记----开启Android系统adb服务(默认关闭)
- 摆动数字时钟的示例
- Java CardLayout
- Java BoxLayout
- Java中的带源代码的计算器