zoukankan      html  css  js  c++  java
  • AutoMapperHelper

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using AutoMapper;
    
    namespace YinLong.Framework.Mapper
    {
        /// <summary>
        /// AutoMapper(4.1.1.0)扩展帮助类(Mapper.Initialize(x => x.CreateMap<UserA, UserB>());)
        /// </summary>
        public static class AutoMapperHelper
        {
            /// <summary>
            ///  类型映射
            /// </summary>
            public static T MapTo<T>(this object obj)
            {
                if (obj == null) return default(T);
                AutoMapper.Mapper.CreateMap(obj.GetType(), typeof(T));
                return AutoMapper.Mapper.Map<T>(obj);
            }
            /// <summary>
            /// 集合列表类型映射
            /// </summary>
            public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
            {
                foreach (var first in source)
                {
                    var type = first.GetType();
                    AutoMapper.Mapper.CreateMap(type, typeof(TDestination));
                    break;
                }
                return AutoMapper.Mapper.Map<List<TDestination>>(source);
            }
            /// <summary>
            /// 集合列表类型映射
            /// </summary>
            public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
            {
                //IEnumerable<T> 类型需要创建元素的映射
                AutoMapper.Mapper.CreateMap<TSource, TDestination>();
                return AutoMapper.Mapper.Map<List<TDestination>>(source);
            }
            /// <summary>
            /// 类型映射
            /// </summary>
            public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
                where TSource : class
                where TDestination : class
            {
                if (source == null) return destination;
                AutoMapper.Mapper.CreateMap<TSource, TDestination>();
                return AutoMapper.Mapper.Map(source, destination);
            }
            /// <summary>
            /// DataReader映射
            /// </summary>
            public static IEnumerable<T> DataReaderMapTo<T>(this IDataReader reader)
            {
                AutoMapper.Mapper.Reset();
                AutoMapper.Mapper.CreateMap<IDataReader, IEnumerable<T>>();
                return AutoMapper.Mapper.Map<IDataReader, IEnumerable<T>>(reader);
            }
        }
    }
  • 相关阅读:
    【洛谷P1005】矩阵取数游戏
    【洛谷P1966】火柴排队
    【题解】洛谷P1731 [NOI1999] 生日蛋糕(搜索+剪枝)
    【题解】洛谷P3627 [APIO2009]抢掠计划(缩点+SPFA)
    【题解】洛谷P1262 间谍网络 (强连通分量缩点)
    【题解】洛谷P3200 [HNOI2009] 有趣的数列(卡特兰数+质因数分解)
    点双连通分量 [HNOI2012]矿场搭建
    分块 公主的朋友
    [置顶] 数学的坑,一点点来填
    大暴搜 [NOIP2009]靶形数独
  • 原文地址:https://www.cnblogs.com/wangyinlon/p/12460249.html
Copyright © 2011-2022 走看看