Ejemplo 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;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 测试 - 初始化
        /// </summary>
        public CrudServiceTest()
        {
            _id     = Guid.NewGuid();
            _id2    = Guid.NewGuid();
            _entity = new EntitySample(_id)
            {
                Name = "A"
            };
            _entity2 = new EntitySample(_id2)
            {
                Name = "B"
            };
            _dto = new DtoSample {
                Id = _id.ToString(), Name = "A"
            };
            _dto2 = new DtoSample {
                Id = _id2.ToString(), Name = "B"
            };
            _unitOfWork = Substitute.For <IUnitOfWork>();
            _repository = Substitute.For <IRepositorySample>();
            _service    = new CrudServiceSample(_unitOfWork, _repository);
            var mapper = new AutoMapperMapper();

            MapperExtensions.SetMapper(mapper);
        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 注册AutoMapper对象映射操作
        /// </summary>
        /// <param name="service">服务集合</param>
        public static void AddAutoMapper(this IServiceCollection service)
        {
            var mapper = new AutoMapperMapper();

            service.TryAddSingleton <IMapper>(mapper);
            MapperExtensions.SetMapper(mapper);
        }
        /// <summary>
        /// 启用AutoMapper
        /// </summary>
        public static IApplicationBuilder UseAutoMapper(this IApplicationBuilder app,
                                                        Action <IMapperConfigurationExpression> additionalInitAction = null)
        {
            MapperConfigurationExpression cfg = new MapperConfigurationExpression();

            if (additionalInitAction != null)
            {
                additionalInitAction(cfg);
            }

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

            Mapper.Initialize(cfg);

            IMapper mapper = app.ApplicationServices.GetService <IMapper>();

            MapperExtensions.SetMapper(mapper);

            return(app);
        }
        public static IServiceCollection RegisterAutoMapper(this IServiceCollection services)
        {
            var mapper = ServiceStartup.MapperInitialize();

            MapperExtensions.SetMapper(mapper);
            services.AddSingleton(mapper);
            return(services);
        }
Ejemplo n.º 7
0
        public MapTest()
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <TestMapperConfiguration>();
            });

            AutoMapperConfiguration.Init(config);
            var mapper = new AutoMapperMapper();

            MapperExtensions.SetMapper(mapper);
        }
Ejemplo n.º 8
0
        private void SetMapper()
        {
            MapperConfigurationExpression cfg = new MapperConfigurationExpression();

            var config = new Kira.LaconicInvoicing.Warehouse.Dtos.AutoMapperConfiguration();

            config.CreateMaps(cfg);

            Mapper.Initialize(cfg);
            var mapper = new AutoMapperMapper();

            MapperExtensions.SetMapper(mapper);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 测试 - 初始化
        /// </summary>
        public QueryServiceTest()
        {
            _id     = Guid.NewGuid();
            _id2    = Guid.NewGuid();
            _entity = new EntitySample(_id)
            {
                Name = "A"
            };
            _entity2 = new EntitySample(_id2)
            {
                Name = "B"
            };
            _repository = Substitute.For <IRepositorySample>();
            _service    = new QueryServiceSample(_repository);
            var mapper = new AutoMapperMapper();

            MapperExtensions.SetMapper(mapper);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 使用模块服务
        /// </summary>
        /// <param name="provider"></param>
        public override void UseModule(IServiceProvider provider)
        {
            MapperConfigurationExpression cfg = provider.GetService <MapperConfigurationExpression>() ?? new MapperConfigurationExpression();

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

            Mapper.Initialize(cfg);

            IMapper mapper = provider.GetService <IMapper>();

            MapperExtensions.SetMapper(mapper);

            IsEnabled = true;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 注册AutoMapper对象映射操作
        /// </summary>
        /// <param name="services">服务集合</param>
        public static void AddAutoMapper(this IServiceCollection services)
        {
            var typeFinder           = services.GetOrAddTypeFinder <ITypeFinder>(assemblyFinder => new TypeFinder(assemblyFinder));
            var mapperConfigurations = typeFinder.Find <IOrderedMapperProfile>();
            var instances            = mapperConfigurations.Select(mapperConfiguration =>
                                                                   (IOrderedMapperProfile)Activator.CreateInstance(mapperConfiguration))
                                       .OrderBy(mapperConfiguration => mapperConfiguration.Order);
            var config = new MapperConfiguration(cfg =>
            {
                foreach (var instance in instances)
                {
                    Debug.WriteLine($"初始化AutoMapper配置:{instance.GetType().FullName}");
                    cfg.AddProfile(instance.GetType());
                }
            });

            AutoMapperConfiguration.Init(config);
            var mapper = new AutoMapperMapper();

            services.TryAddSingleton <IMapper>(mapper);
            MapperExtensions.SetMapper(mapper);
        }
 /// <summary>
 /// 初始化一个<see cref="GithubAuthorizationProviderTest"/>类型的实例
 /// </summary>
 public GithubAuthorizationProviderTest(ITestOutputHelper output)
 {
     _output   = output;
     _provider = new GithubAuthorizationProvider(new TestGithubAuthorizationConfigProvider());
     MapperExtensions.SetMapper(new AutoMapperMapper());
 }
Ejemplo n.º 13
0
 /// <summary>
 /// 初始化一个<see cref="MicrosoftAuthorizationProviderTest"/>类型的实例
 /// </summary>
 public MicrosoftAuthorizationProviderTest(ITestOutputHelper output)
 {
     _output   = output;
     _provider = new MicrosoftAuthorizationProvider(new TestMicrosoftAuthorizationConfigProvider());
     MapperExtensions.SetMapper(new AutoMapperMapper());
 }
Ejemplo n.º 14
0
        public MapTest()
        {
            var mapper = new AutoMapperMapper();

            MapperExtensions.SetMapper(mapper);
        }
Ejemplo n.º 15
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="provider"></param>
        public static void UsePack(IServiceProvider provider)
        {
            IMapper mapper = provider.GetService <IMapper>();

            MapperExtensions.SetMapper(mapper);
        }