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();
  • 相关阅读:
    paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之output encoded style with registered outputs(Good style)
    软测(一)
    package.json
    邬江兴:网络安全“再平衡战略”抓手--拟态防御
    什么是DDOS攻击?怎么防御?
    什么是gitlab CI ?CI代表什么?
    结构体字节对齐(转)
    MySQL 及 SQL 注入与防范方法
    HDU 4704 Sum (费马小定理)
    HDU 4704 Sum (费马小定理)
  • 原文地址:https://www.cnblogs.com/shiningrise/p/7610665.html
Copyright © 2011-2022 走看看