Example #1
0
        /// <summary>
        /// 模块包注入
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddEnginePack(this IServiceCollection services)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("DL框架引擎初始化...\r\n");
                Stopwatch watch = new Stopwatch();
                watch.Start();
                IDLEnginePack service = new DLEnginePack();
                services.AddService();
                //模块注入
                service.AddEnginePack(services);
                services.AddScoped(typeof(IRepository <>), typeof(Repository <>));
                //服务构建
                IServiceProvider provider = services.BuildServiceProvider();
                //服务集合器设置
                ServiceLocator.Instance.SetServiceCollection(services);
                //服务构建器设置
                ServiceLocator.Instance.SetProvider(provider);
                ServiceLocator.Instance.GetProvider = provider;
                var autoConfig = utility.Configer.ConfigerManager.Instance.Configuration.GetDLDbSetting();
                sb.Append($"准备检查是否开启自动迁移.【{autoConfig.AutoEFMigrationEnable}】\r\n");
                if (autoConfig.AutoEFMigrationEnable)
                {
                    var contexts = DbContextManager.GetMeomryDbContxt();
                    sb.Append($"统计数据库上下文数量:{contexts.Count}\r\n");
                    if (contexts.Any())
                    {
                        contexts.ForEach(context =>
                        {
                            var name = context.GetType().Name;
                            sb.Append($"准备迁移数据库上下文[{name}]的数据实体");
                            var result = AutoMigration(context);
                            sb.Append($"上下文:[{name}]" + result + "\r\n");
                        });
                    }
                }

                watch.Stop();
                sb.Append($"DL框架引擎初始化完成\r\n");
                sb.Append($"总共花费:{watch.ElapsedMilliseconds}毫秒");
                logger.Info(sb.ToString());
                return(services);
            }
            catch (Exception ex)
            {
                logger.Error($"初始化DL框架发生异常[AddPack<TDbContext>],异常信息:{ex.Message}");
                throw;
            }
        }