Beispiel #1
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);
        }
Beispiel #2
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);
        }