zoukankan      html  css  js  c++  java
  • How to use AutoMapper

    http://docs.automapper.org/en/stable/Getting-started.html

      IMappingExpression<TSource, TDestination> CreateMap<TSource, TDestination>(MemberList memberList);

    Mapper.Initialize(cfg => cfg.CreateMap<Order, OrderDto>());
    //or
    var config = new MapperConfiguration(cfg => cfg.CreateMap<Order, OrderDto>());


       IMappingExpression<TSource, TDestination> ForMember<TMember>(Expression<Func<TDestination, TMember>> destinationMember, Action<IMemberConfigurationExpression<TSource, TDestination, TMember>> memberOptions);


    var mapper = config.CreateMapper();
    // or
    var mapper = new Mapper(config);
    OrderDto dto = mapper.Map<OrderDto>(order);
    // or
    OrderDto dto = Mapper.Map<OrderDto>(order);
     

    C# : Converting Base Class to Child Class

    https://stackoverflow.com/a/25653977/3782855

    'm surprised AutoMapper hasn't come up as an answer.

    As is clear from all the previous answers, you cannot do the typecast. However, using AutoMapper, in a few lines of code you can have a new SkyfilterClient instantiated based on an existing NetworkClient.

    In essence, you would put the following where you are currently doing your typecasting:

    using AutoMapper;
    ...
    // somewhere, your network client was declared
    var existingNetworkClient = new NetworkClient();
    ...
    // now we want to type-cast, but we can't, so we instantiate using AutoMapper
    AutoMapper.Mapper.CreateMap<NetworkClient, SkyfilterClient>();
    var skyfilterObject = AutoMapper.Mapper.Map<SkyfilterClient>(existingNetworkClient);

    Here's a full-blown example:

     public class Vehicle
      {
        public int NumWheels { get; set; }
        public bool HasMotor { get; set; }
      }
    
      public class Car: Vehicle
      {
        public string Color { get; set; }
        public string SteeringColumnStyle { get; set; }
      }
    
      public class CarMaker
      {
        // I am given vehicles that I want to turn into cars...
        public List<Car> Convert(List<Vehicle> vehicles)
        {
          var cars = new List<Car>();
          AutoMapper.Mapper.CreateMap<Vehicle, Car>(); // Declare that we want some automagic to happen
          foreach (var vehicle in vehicles)
          {
            var car = AutoMapper.Mapper.Map<Car>(vehicle);
            // At this point, the car-specific properties (Color and SteeringColumnStyle) are null, because there are no properties in the Vehicle object to map from.
            // However, car's NumWheels and HasMotor properties which exist due to inheritance, are populated by AutoMapper.
            cars.Add(car);
          }
          return cars;
        }
      }
     
  • 相关阅读:
    CCF 小明放学 201812-2(简单模拟)
    CSP 小明上学 201812-1 (水题)
    201903-4 消息传递接口(队列模拟)
    Betsy's Tour 漫游小镇(dfs)
    Checker Challenge跳棋的挑战(n皇后问题)
    Money Systems 货币系统(母函数)
    中国剩余定理(模板+详解)
    Biorhythms(中国剩余定理(模板题))
    输入输出外挂(纯数字型)
    欧几里德(转)
  • 原文地址:https://www.cnblogs.com/chucklu/p/8978932.html
Copyright © 2011-2022 走看看