AutoMapperExtension

Posted 吴晓阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();

 

以上是关于AutoMapperExtension的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数