AutoMapper系列2-聚合、解耦

对于某个聚合对象,可能存在嵌套,思路基本是一致的:

 #region demo5
    public class OuterSource
    {
        public int Value { get; set; }
        public InnerSource Inner { get; set; }
    }
    public class InnerSource
    {
        public int OtherValue { get; set; }
    }

    public class OuterDest
    {
        public int Value { get; set; }
        public InnerDest Inner { get; set; }
    }
    public class InnerDest
    {
        public int OtherValue { get; set; }
    }
    #endregion
#region Nesting
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap();
                //cfg.CreateMap();
            });

            var source = new OuterSource
            {
                Value = 5,
                Inner = new InnerSource { OtherValue = 15 }
            };

            //验证类型映射是否正确
            //Mapper.AssertConfigurationIsValid();

            //执行映射
            var dest = Mapper.Map(source);

            Console.WriteLine("dest.Value:" + dest.Value);
            Console.WriteLine("dest.Inner is null:" + (dest.Inner == null ? "true" : "false"));
            Console.WriteLine("dest.Inner.OtherValue:" + dest.Inner.OtherValue);
            #endregion

关于解耦处理(使得映射的方式更加灵活,可以通过外部配置映射方式):

#region demo6
    public class Source
    {
        public string Value1 { get; set; }
        public string Value2 { get; set; }
        public string Value3 { get; set; }
    }
    public class Destination
    {
        public int Value1 { get; set; }
        public DateTime Value2 { get; set; }
        public Type Value3 { get; set; }
    }

    public class CustomResolver : IValueResolver
    {
        int IValueResolver.Resolve(Source2 source, Destination2 destMember, int destination, ResolutionContext context)
        {
            return source.Value1 + source.Value2;
        }
    }
    public class DateTimeTypeConverter : ITypeConverter
    {
        DateTime ITypeConverter.Convert(string source, DateTime destination, ResolutionContext context)
        {
            try
            {
                return System.Convert.ToDateTime(source);
            }
            catch
            {
                return DateTime.MinValue;
            }
        }
    }
    public class TypeTypeConverter : ITypeConverter
    {
        Type ITypeConverter.Convert(string source, Type destination, ResolutionContext context)
        {
            TypeConverter dd = TypeDescriptor.GetConverter(source.GetType());
            dd.CanConvertTo(typeof(Type));
            Type type = Assembly.GetExecutingAssembly().GetType(source);
            return type;
        }
    }
    #endregion
            #region
            var source = new Source
            {
                Value1 = "5",
                Value2 = "01/18/2018jj",
                Value3 = "LibStudy.Program"
            };

            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap().ConvertUsing(x => Convert.ToInt32(x));
                cfg.CreateMap().ConvertUsing(new DateTimeTypeConverter());
                cfg.CreateMap().ConvertUsing();
                cfg.CreateMap();
            });

            //验证类型映射是否正确
            Mapper.AssertConfigurationIsValid();

            Destination result = Mapper.Map(source);
            Console.WriteLine("result.Value1:" + result.Value1.ToString());
            Console.WriteLine("result.Value2:" + result.Value2.ToString());
            Console.WriteLine("result.Value3:" + result.Value3.ToString());
            #endregion

AutoMapper系列2-聚合、解耦_第1张图片

            #region 解耦映射2
            var source2 = new Source2
            {
                Value1 = 5,
                Value2 = 7
            };
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap()
                   //.ForMember(dest => dest.Total, opt => opt.MapFrom(src => src.Value1 + src.Value2));
                   .ForMember(dest => dest.Total, opt => opt.ResolveUsing());
            });

            var result2 = Mapper.Map(source2);
            Console.WriteLine("result.Total:" + result2.Total);
            #endregion

AutoMapper系列2-聚合、解耦_第2张图片

你可能感兴趣的:(C#)