zoukankan      html  css  js  c++  java
  • AutoMapper: Mapper.Initialize() 只能调用一次,Why?

    最开始的代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Reflection;
     7 
     8 using AutoMapper;
     9 
    10 using Happy.ExtentionMethods;
    11 using Happy.Bootstrap;
    12 
    13 namespace Happy.Bootstrap.AutoMapper
    14 {
    15     /// <summary>
    16     /// 自动添加配置。
    17     /// </summary>
    18     public class AutoAddProfilePlugin : IBootstrapPlugin
    19     {
    20         /// <inheritdoc />
    21         public void Start(IBootstrapService bootstrapService, Assembly assembly)
    22         {
    23             bootstrapService.MustNotNull("bootstrapService");
    24             assembly.MustNotNull("assembly");
    25 
    26             foreach (var type in assembly.GetTypes())
    27             {
    28                 if (type.IsAbstract || type.IsInterface)
    29                 {
    30                     continue;
    31                 }
    32 
    33                 if (typeof(Profile).IsAssignableFrom(type))
    34                 {
    35                     Mapper.Initialize(x => {
    36                         x.AddProfile(Activator.CreateInstance(type) as Profile);
    37                     });
    38                 }
    39             }
    40         }
    41     }
    42 }

    问题

    我的项目中,每个 dll 都是自描述的,系统启动的时候,对每个 dll 对执行上面的插件,结果, Mapper.Initialize() 只有最后一次配置才有效,前面的配置会丢失。

    最后的代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Reflection;
     7 
     8 using AutoMapper;
     9 
    10 using Happy.ExtentionMethods;
    11 using Happy.Bootstrap;
    12 
    13 namespace Happy.Bootstrap.AutoMapper
    14 {
    15     /// <summary>
    16     /// 自动添加配置。
    17     /// </summary>
    18     public class AutoAddProfilePlugin : IBootstrapPlugin
    19     {
    20         /// <inheritdoc />
    21         public void Start(IBootstrapService bootstrapService, Assembly assembly)
    22         {
    23             bootstrapService.MustNotNull("bootstrapService");
    24             assembly.MustNotNull("assembly");
    25 
    26             foreach (var type in assembly.GetTypes())
    27             {
    28                 if (type.IsAbstract || type.IsInterface)
    29                 {
    30                     continue;
    31                 }
    32 
    33                 if (typeof(Profile).IsAssignableFrom(type))
    34                 {
    35                     Mapper.AddProfile(Activator.CreateInstance(type) as Profile);
    36                 }
    37             }
    38         }
    39     }
    40 }
  • 相关阅读:
    记录一下自己写PHP程序时走过的一些坑
    自己写了一个TCP攻击测压平台
    Centos 7x 安装 Telegram MTproxy代理【完美可用】
    "@阅后即焚"上线了!
    小白的机器学习坑3:粗大的安装CUDA
    小白的机器学习坑2:nvidia驱动的安装
    小白的机器学习坑1:ubuntu 18.04的安装
    小白的linux学习笔记9:安装nodejs和gitbook
    小白的linux笔记11:放弃gitbook,转战Sphinx
    小白的linux学习笔记10:安装nginx和第一个网页
  • 原文地址:https://www.cnblogs.com/happyframework/p/3516862.html
Copyright © 2011-2022 走看看