zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    http://www.oschina.net/translate/elasticsearch-getting-started?cmp
    http://www.mxchip.com/talk/news/jishuwenzhang/2014-09-11/67.html
    深入理解JVM—性能监控工具
    Windows7查看本地Java安装是否成功和路径的方法
    Eclipse 编译错误 Access restriction:The type *** is not accessible due to restriction on... 解决方案
    PSYoungGen /PSOldGen/PSPermGen区别
    深入浅出Java并发包—锁机制(三)
    深入浅出Java并发包—锁机制(二)
    【转】Spring 注解学习手札(超好的springmvc注解教程)
    解决java.lang.NoClassDefFoundError: org.jdom.Content
  • 原文地址:https://www.cnblogs.com/shiningrise/p/7610665.html
Copyright © 2011-2022 走看看