zoukankan      html  css  js  c++  java
  • 静态和实例初始化映射

    In 4.2.1 version of AutoMapper and later, AutoMapper provides two APIs: a static and an instance API. The static API:

    Mapper.Initialize(cfg => {
        cfg.AddProfile<AppProfile>();
        cfg.CreateMap<Source, Dest>();
    });
    
    var dest = Mapper.Map<Source, Dest>(new Source());
    

    And the instance API:

    var config = new MapperConfiguration(cfg => {
        cfg.AddProfile<AppProfile>();
        cfg.CreateMap<Source, Dest>();
    });
    
    var mapper = config.CreateMapper();
    // or
    IMapper mapper = new Mapper(config);
    var dest = mapper.Map<Source, Dest>(new Source());
    

    Gathering configuration before initialization

    AutoMapper also lets you gather configuration before initialization:

    var cfg = new MapperConfigurationExpression();
    cfg.CreateMap<Source, Dest>();
    cfg.AddProfile<MyProfile>();
    MyBootstrapper.InitAutoMapper(cfg);
    
    Mapper.Initialize(cfg);
    // or
    var mapperConfig = new MapperConfiguration(cfg);
    IMapper mapper = new Mapper(mapperConfig);
    
    

    LINQ projections

    For the instance method of using AutoMapper, LINQ now requires us to pass in the MapperConfiguration instance:

    public class ProductsController : Controller {
        public ProductsController(MapperConfiguration config) {
            this.config = config;
        }
        private MapperConfiguration config;
    
        public ActionResult Index(int id) {
            var dto = dbContext.Products
                                   .Where(p => p.Id == id)
                                   .ProjectTo<ProductDto>(config)
                                   .SingleOrDefault();
    
            return View(dto);
        }    
    }
    

    Unsupported operations

    One "feature" of AutoMapper allowed you to modify configuration at runtime. That caused many problems, so the new static API does not allow you to do this. You'll need to move all your Mapper.CreateMap calls into a profile, and into a Mapper.Initialize.

    For dynamic mapping, such as Mapper.DynamicMap, you can configure AutoMapper to create missing maps as needed:

    Mapper.Initialize(cfg => cfg.CreateMissingTypeMaps = true);
    

    Internally this uses conventions to create maps as necessary.

  • 相关阅读:
    linq in 查询
    sql数据分组取第一条
    获取mac地址
    计算机网络体系结构分层 (OSI TCP/IP)
    2048游戏代码
    go——变量、类型、常量、函数
    关于装饰器 开放封闭
    linux杂碎知识
    crawl——scrapy(配置文件,持久化,请求传递参数,提高爬虫效率,爬虫中间件,集成selenium,去重规则)
    crawl——xpath使用
  • 原文地址:https://www.cnblogs.com/Leman/p/5774383.html
Copyright © 2011-2022 走看看