AutoMapper

扩展方法

/// <summary>
        ///  类型映射,默认字段名字一一对应
        /// </summary>
        /// <typeparam name="TDestination">转化之后的model,可以理解为viewmodel</typeparam>
        /// <typeparam name="TSource">要被转化的实体,Entity</typeparam>
        /// <param name="source">可以使用这个扩展方法的类型,任何引用类型</param>
        /// <returns>转化之后的实体</returns>
        public static TDestination MapTo<TDestination, TSource>(this TSource source)
            where TDestination : class
            where TSource : class
        {
            if (source == null) return default(TDestination);
            var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
            var mapper = config.CreateMapper();
            return mapper.Map<TDestination>(source);
        }

使用方式

var source1 = new Source1
                {
                    Name = "source",
                    Sub = new SubSource1 { Age = "25" },
                    c = "c",
                    GetA = "A",
                    SetB = "B"
                };

                var destViewModel = source1.MapTo<Source1,Dest1>();

高级用法:

var config2 = new MapperConfiguration(
                    cfg => cfg.CreateMap<SourceUser, DestUser2>()
                        .ForMember(d => d.DestName, opt => opt.MapFrom(s => s.Name))    //指定字段一一对应
                        .ForMember(d => d.Birthday, opt => opt.MapFrom(src => src.Birthday.ToString("yy-MM-dd HH:mm")))//指定字段,并转化指定的格式
                        .ForMember(d => d.Age, opt => opt.Condition(src => src.Age > 5))//条件赋值
                        .ForMember(d => d.A1, opt => opt.Ignore())//忽略该字段,不给该字段赋值
                        .ForMember(d => d.A1, opt => opt.NullSubstitute("Default Value"))//如果源字段值为空,则赋值为 Default Value
                        .ForMember(d => d.A1, opt => opt.MapFrom(src => src.Name + src.Age * 3 + src.Birthday.ToString("d"))));//可以自己随意组合赋值
                var mapper2 = config2.CreateMapper();

list转化

/// <summary>
        /// 集合列表类型映射,默认字段名字一一对应
        /// </summary>
        /// <typeparam name="TDestination">转化之后的model,可以理解为viewmodel</typeparam>
        /// <typeparam name="TSource">要被转化的实体,Entity</typeparam>
        /// <param name="source">可以使用这个扩展方法的类型,任何引用类型</param>
        /// <returns>转化之后的实体列表</returns>
        public static IEnumerable<TDestination> MapToList<TSource,TDestination>(this IEnumerable<TSource> source)
            where TDestination : class
            where TSource : class
        {
            if (source == null) return new List<TDestination>();
            var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
            var mapper = config.CreateMapper();
            return mapper.Map<List<TDestination>>(source);
        }

使用方式

var source1 = new Source1
                {
                    Name = "source",
                    Sub = new SubSource1 { Age = "25" },
                    c = "c",
                    GetA = "A",
                    SetB = "B"
                };
          var source3 = new Source1
                {
                    Name = "source3",
                    Sub = new SubSource1 { Age = "253" },
                    c = "c3",
                    GetA = "A3",
                    SetB = "B3"
                };
                var sourceList = new List<Source1> { source1, source3 };
                var destViewModelList = sourceList.MapToList<Source1,Dest1>();

 

上一篇:CodeGo.net> Automapper-如何从源子对象映射到目标


下一篇:ABP中的AutoMapper