Esempio n. 1
0
        /// <summary>
        /// 应用模块服务
        /// </summary>
        /// <param name="provider">服务提供者</param>
        public override void UsePack(IServiceProvider provider)
        {
            ILogger logger = provider.GetLogger <AutoMapperPack>();
            MapperConfigurationExpression cfg = provider.GetService <MapperConfigurationExpression>();

            //获取已注册到IoC的所有Profile
            IMapTuple[] tuples = provider.GetServices <IMapTuple>().ToArray();
            foreach (IMapTuple mapTuple in tuples)
            {
                mapTuple.CreateMap();
                cfg.AddProfile(mapTuple as Profile);
                logger.LogInformation($"初始化对象映射配对:{mapTuple.GetType()}");
            }

            //各个模块DTO的 IAutoMapperConfiguration 映射实现类
            IAutoMapperConfiguration[] configs = provider.GetServices <IAutoMapperConfiguration>().ToArray();
            foreach (IAutoMapperConfiguration config in configs)
            {
                config.CreateMaps(cfg);
                logger.LogInformation($"初始化对象映射配对:{config.GetType()}");
            }

            MapperConfiguration configuration = new MapperConfiguration(cfg);
            IMapper             mapper        = new AutoMapperMapper(configuration);

            MapperExtensions.SetMapper(mapper);
            logger.LogInformation($"初始化对象映射对象到 MapperExtensions:{mapper.GetType()},共包含 {configuration.GetMappers().Count()} 个映射配对");

            IsEnabled = true;
        }
Esempio n. 2
0
        /// <summary>
        /// 应用模块服务
        /// </summary>
        /// <param name="provider">服务提供者</param>
        public override void UsePack(IServiceProvider provider)
        {
            MapperConfigurationExpression cfg = provider.GetService <MapperConfigurationExpression>();

            //获取已注册到IoC的所有Profile
            IMapTuple[] tuples = provider.GetServices <IMapTuple>().ToArray();
            foreach (IMapTuple mapTuple in tuples)
            {
                mapTuple.CreateMap();
                cfg.AddProfile(mapTuple as Profile);
            }

            //各个模块DTO的 IAutoMapperConfiguration 映射实现类
            IAutoMapperConfiguration[] configs = provider.GetServices <IAutoMapperConfiguration>().ToArray();
            foreach (IAutoMapperConfiguration config in configs)
            {
                config.CreateMaps(cfg);
            }

            MapperConfiguration configuration = new MapperConfiguration(cfg);

            IMapper mapper = new AutoMapperMapper(configuration);

            MapperExtensions.SetMapper(mapper);

            IsEnabled = true;
        }