/// <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);
            _service.LazyServiceProvider = Substitute.For <ILazyServiceProvider>();
            var allAssemblyFinder       = new AppDomainAllAssemblyFinder();
            var mapperProfileTypeFinder = new MapperProfileTypeFinder(allAssemblyFinder);
            var instances = mapperProfileTypeFinder
                            .FindAll()
                            .Select(type => Bing.Reflection.Reflections.CreateInstance <IObjectMapperProfile>(type))
                            .ToList();
            var configuration = new MapperConfiguration(cfg =>
            {
                foreach (var instance in instances)
                {
                    Debug.WriteLine($"初始化AutoMapper配置:{instance.GetType().FullName}");
                    instance.CreateMap();
                    // ReSharper disable once SuspiciousTypeConversion.Global
                    cfg.AddProfile(instance as Profile);
                }
            });
            var mapper = new AutoMapperObjectMapper(configuration, instances);

            ObjectMapperExtensions.SetMapper(mapper);
        }
Beispiel #2
0
        /// <summary>
        /// 注册AutoMapper对象映射操作
        /// </summary>
        /// <param name="services">服务集合</param>
        public static void AddAutoMapper(this IServiceCollection services)
        {
            var mapperProfileTypeFinder = services.GetOrAddTypeFinder <IMapperProfileTypeFinder>(assemblyFinder => new MapperProfileTypeFinder(assemblyFinder));
            var instances = mapperProfileTypeFinder
                            .FindAll()
                            .Select(type => Reflections.CreateInstance <IObjectMapperProfile>(type))
                            .ToList();
            var configuration = new MapperConfiguration(cfg =>
            {
                foreach (var instance in instances)
                {
                    Debug.WriteLine($"初始化AutoMapper配置:{instance.GetType().FullName}");
                    instance.CreateMap();
                    // ReSharper disable once SuspiciousTypeConversion.Global
                    cfg.AddProfile(instance as Profile);
                }
            });
            var mapper = new AutoMapperObjectMapper(configuration);

            ObjectMapperExtensions.SetMapper(mapper);
            services.TryAddSingleton <IObjectMapper>(mapper);
        }
Beispiel #3
0
        public MapTest()
        {
            var allAssemblyFinder       = new AppDomainAllAssemblyFinder();
            var mapperProfileTypeFinder = new MapperProfileTypeFinder(allAssemblyFinder);
            var instances = mapperProfileTypeFinder
                            .FindAll()
                            .Select(type => Reflections.CreateInstance <IObjectMapperProfile>(type))
                            .ToList();
            var configuration = new MapperConfiguration(cfg =>
            {
                foreach (var instance in instances)
                {
                    Debug.WriteLine($"初始化AutoMapper配置:{instance.GetType().FullName}");
                    instance.CreateMap();
                    // ReSharper disable once SuspiciousTypeConversion.Global
                    cfg.AddProfile(instance as Profile);
                }
            });
            var mapper = new AutoMapperObjectMapper(configuration, instances);

            ObjectMapperExtensions.SetMapper(mapper);
        }