zoukankan      html  css  js  c++  java
  • AutoMapper 5.0-升级指南

    Initialization

    You now must use either Mapper.Initialize or new MapperConfiguration() to initialize AutoMapper. If you prefer to keep the static usage, use Mapper.Initialize.

    If you have a lot of Mapper.CreateMap calls everywhere, move those to a Profile, or into Mapper.Initialize, called once at startup.

    Profiles

    Instead of overriding a Configure method, you configure directly via the constructor:

    public class MappingProfile : Profile {
        public MappingProfile() {
            CreateMap<Foo, Bar>();
            RecognizePrefix("m_");
        }
    }
    

    Resolution Context things

    ResolutionContext used to capture a lot of information, source and destination values, along with a hierarchical parent model. For source/destination values, all of the interfaces (value resolvers and type converters) along with config options now include the source/destination values, and if applicable, source/destination members.

    If you're trying to access some parent object in your model, you will need to add those relationships to your models and access them through those relationships, and not through AutoMapper's hierarchy. The ResolutionContext was pared down for both performance and sanity reasons.

    Value resolvers

    The signature of a value resolver has changed to allow access to the source/destination models. Additionally, the base class is gone in favor of interfaces. For value resolvers that do not have a member redirection, the interface is now:

    public interface IValueResolver<in TSource, in TDestination, TDestMember>
    {
        TDestMember Resolve(TSource source, TDestination destination, TDestMember destMember, ResolutionContext context);
    }
    

    You have access now to the source model, destination model, and destination member this resolver is configured against.

    If you are using a ResolveUsing and passing in the FromMember configuration, this is now a new resolver interface:

    public interface IMemberValueResolver<in TSource, in TDestination, in TSourceMember, TDestMember>
    {
        TDestMember Resolve(TSource source, TDestination destination, TSourceMember sourceMember, TDestMember destMember, ResolutionContext context);
    }
    

    This is now configured directly as ForMember(dest => dest.Foo, opt => opt.ResolveUsing<MyCustomResolver, string>(src => src.Bar)

    Type converters

    The base class for a type converter is now gone in favor of a single interface that accepts the source and destination objects and returns the destination object:

    public interface ITypeConverter<in TSource, TDestination>
    {
        TDestination Convert(TSource source, TDestination destination, ResolutionContext context);
    }
    

    Circular references

    Previously, AutoMapper could handle circular references by keeping track of what was mapped, and on every mapping, check a local hashtable of source/destination objects to see if the item was already mapped. It turns out this tracking is very expensive, and you need to opt-in using PreserveReferences for circular maps to work. Alternatively, you can configure MaxDepth:

    // Self-referential mapping
    cfg.CreateMap<Category, CategoryDto>().MaxDepth(3);
    
    // Circular references between users and groups
    cfg.CreateMap<User, UserDto>().PreserveReferences();
    

    UseDestinationValue

    UseDestinationValue tells AutoMapper not to create a new object for some member, but to use the existing property of the destination object. It used to be true by default. Consider whether this applies to your case. Check recent issues.

    cfg.CreateMap<Source, Destination>()
       .ForMember(d => d.Child, opt => opt.UseDestinationValue());
    
  • 相关阅读:
    1030 完美数列 (25 分)
    1029 旧键盘 (20 分)
    数据库命令失败原因汇总
    代码有中文括号,导致错误
    win10笔记本触控板使用指南
    (已解决)vsC#控制台应用添加System.Windows.Forms引用失败(精通C#)
    ildasm中Ctrl+M闪退的问题(已解决, 精通C# 15.1)
    C#控制台应用(.NET Core)添加System.Windows.Forms失败(已解决)
    知识点_指针_增加对指针的理解
    自己写出的Bug_应是%f却写成%d
  • 原文地址:https://www.cnblogs.com/Leman/p/5774000.html
Copyright © 2011-2022 走看看