zoukankan      html  css  js  c++  java
  • AutoMapper中用户自定义转换

    Custom Type Converters

    Sometimes, you need to take complete control over the conversion of one type to another. This is typically when one type looks nothing like the other, a conversion function already exists, and you would like to go from a "looser" type to a stronger type, such as a source type of string to a destination type of Int32.

    For example, suppose we have a source type of:

    public class Source
    {
        public string Value1 { get; set; }
        public string Value2 { get; set; }
        public string Value3 { get; set; }
    }

    But you would like to map it to:

    public class Destination
    {
        public int Value1 { get; set; }
        public DateTime Value2 { get; set; }
        public Type Value3 { get; set; }
    }

    If we were to try and map these two types as-is, AutoMapper would throw an exception (at map time and configuration-checking time), as AutoMapper does not know about any mapping from string to int, DateTime or Type. To create maps for these types, we must supply a custom type converter, and we have three ways of doing so:

    void ConvertUsing(Func<TSource, TDestination> mappingFunction);
    void ConvertUsing(ITypeConverter<TSource, TDestination> converter);
    void ConvertUsing<TTypeConverter>() where TTypeConverter : ITypeConverter<TSource, TDestination>;
    The first option is simply any function that takes a source and returns a destination. This works for simple cases, but becomes unwieldy for larger ones. In more difficult cases, we can create a custom ITypeConverter<TSource, TDestination>:

    public interface ITypeConverter<TSource, TDestination>
    {
        TDestination Convert(TSource source);
    }

    And supply AutoMapper with either an instance of a custom type converter, or simply the type, which AutoMapper will instantiate at run time. The mapping configuration for our above source/destination types then becomes:

    [Test]
    public void Example()
    {
        Mapper.CreateMap<string, int>().ConvertUsing(Convert.ToInt32);
        Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
        Mapper.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>();
        Mapper.CreateMap<Source, Destination>();
        Mapper.AssertConfigurationIsValid();
    
        var source = new Source
        {
            Value1 = "5",
            Value2 = "01/01/2000",
            Value3 = "AutoMapperSamples.GlobalTypeConverters.GlobalTypeConverters+Destination"
        };
    
        Destination result = Mapper.Map<Source, Destination>(source);
        result.Value3.ShouldEqual(typeof (Destination));
    }
    
    public class DateTimeTypeConverter : ITypeConverter<string, DateTime>
    {
        public DateTime Convert(string source)
        {
            return System.Convert.ToDateTime(source);
        }
    }
    
    public class TypeTypeConverter : ITypeConverter<string, Type>
    {
        public Type Convert(string source)
        {
            Type type = Assembly.GetExecutingAssembly().GetType(source);
            return type;
        }
    }
  • 相关阅读:
    JWT与Session的比较
    Java面试-TCP连接及其优化
    Java面试-动态规划与组合数
    探索Redis设计与实现12:浅析Redis主从复制
    探索Redis设计与实现11:使用快照和AOF将Redis数据持久化到硬盘中
    探索Redis设计与实现10:Redis的事件驱动模型与命令执行过程
    探索Redis设计与实现9:数据库redisDb与键过期删除策略
    探索Redis设计与实现8:连接底层与表面的数据结构robj
    探索Redis设计与实现6:Redis内部数据结构详解——skiplist
    探索Redis设计与实现7:Redis内部数据结构详解——intset
  • 原文地址:https://www.cnblogs.com/sexintercourse/p/6888577.html
Copyright © 2011-2022 走看看