private IFreeSql CreateDb(FreeSql.DataType dbType, DbInfo currentDbOption) { var master = currentDbOption.ConnectionStrings?.FirstOrDefault(e => e.UseType == DbUseType.Master); if (master == null) { throw new ArgumentNullException($"请设置租户 {Tenant.Code} 的主库连接字符串"); } var slaveConnectionStrings = currentDbOption.ConnectionStrings?.Where(e => e.UseType == DbUseType.Slave).Select(e => e.ConnectionString).ToArray(); var freeSqlBuilder = new FreeSql.FreeSqlBuilder() .UseConnectionString(dbType, master.ConnectionString); if (slaveConnectionStrings?.Length > 0) { freeSqlBuilder = freeSqlBuilder.UseSlave(slaveConnectionStrings); } if (_env.IsDevelopment()) { freeSqlBuilder = freeSqlBuilder.UseAutoSyncStructure(true); //自动同步实体结构【开发环境必备】 } var fsql = freeSqlBuilder.Build(); fsql.Aop.ConfigEntityProperty += ConfigEntityProperty; fsql.Aop.CurdBefore += CurdBefore; fsql.Aop.AuditValue += AuditValue; //fsql.Aop.SyncStructureAfter += SyncStructureAfter; DataFilterAsync(fsql); return(fsql); }
public static void Builder() { var builder = new ContainerBuilder(); var cfg = CreateCfg(); builder.RegisterInstance(cfg).SingleInstance(); #region 注册freesql var build = new FreeSql.FreeSqlBuilder(); if (cfg.DbType == ConstCfg.DbType_MSsql) { build = build.UseConnectionString(FreeSql.DataType.SqlServer, cfg.AccurateConn); } else if (cfg.DbType == ConstCfg.DbType_Sqlite) { build = build.UseConnectionString(FreeSql.DataType.Sqlite, cfg.AccurateConn); } else if (cfg.DbType == ConstCfg.DbType_Mysql) { build = build.UseConnectionString(FreeSql.DataType.MySql, cfg.AccurateConn); } IFreeSql freeSql = build.UseAutoSyncStructure(false) .Build(); builder.RegisterInstance(freeSql).SingleInstance(); BAccurateContext.Fluent(freeSql); builder.RegisterType <BAccurateContext>().InstancePerLifetimeScope(); #endregion #region 注册服务 var allAssemblies = BuildManager.GetReferencedAssemblies().Cast <Assembly>(); var assembiles = allAssemblies.Where(x => x.ManifestModule.Name.Contains("BAccurate")).ToArray(); builder.RegisterAssemblyTypes(assembiles) .Where(type => typeof(ITransient).IsAssignableFrom(type) && !type.IsAbstract) .AsSelf() .AsImplementedInterfaces() .InstancePerDependency(); #endregion //注册controller builder.RegisterApiControllers(assembiles); // 注册AutoMapper builder.Register(c => DtoMapper.CreateConfig()).SingleInstance(); builder.RegisterType <Mapper>().As <IMapper>().InstancePerLifetimeScope(); builder.RegisterType <DtoMapper>().As <IFMapper>().InstancePerLifetimeScope(); // 注册核心业务 builder.RegisterType <OnlineUserMgr>().As <IOnlineUserMgr>().SingleInstance(); IOCContainer = builder.Build(); // webApi部分注册 HttpConfiguration config = GlobalConfiguration.Configuration; config.DependencyResolver = new AutofacWebApiDependencyResolver(IOCContainer); }