/// <summary> /// 添加特性sql服务 /// </summary> /// <param name="services"></param> /// <param name="dbConnString">数据库连接字符串</param> public static IServiceCollection AddAttributeSqlService(this IServiceCollection services, Action <DbOption> dbOption, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) { //添加缓存机制 services.AddSingleton <IMemoryCache>(factory => new MemoryCache(new MemoryCacheOptions())); //模型转换成实体需要AutuMapper做映射 services.AddAutoMapper(typeof(AttrSqlServiceExtend)); //仓储层注入 var option = new DbOption(); dbOption(option); services.AddDbConnection(option); services.AddTransient <AttrBaseRepository>(); //对外服务层注入 switch (serviceLifetime) { case ServiceLifetime.Singleton: services.AddSingleton <IAttrSqlClient, AttrSqlClient>(); break; case ServiceLifetime.Scoped: services.AddScoped <IAttrSqlClient, AttrSqlClient>(); break; case ServiceLifetime.Transient: services.AddTransient <IAttrSqlClient, AttrSqlClient>(); break; default: break; } return(services); }
public static DbOption UseSqlServer(this DbOption option, string dbConnString) { option.dbType = DbType.DbEnum.SqlServer; option.dbConnection = new SqlConnection(dbConnString); option.dbConnStr = dbConnString; return(option); }
public static DbOption UseOracle(this DbOption option, string dbConnString) { option.dbType = DbType.DbEnum.Oracle; option.dbConnection = new OracleConnection(dbConnString); option.dbConnStr = dbConnString; return(option); }
public static DbOption UseMysql(this DbOption option, string dbConnString) { option.dbType = DbType.DbEnum.Mysql; option.dbConnection = new MySqlConnection(dbConnString); option.dbConnStr = dbConnString; return(option); }
public static IServiceCollection AddDbConnection([NotNull] this IServiceCollection service, [NotNull] DbOption dbOption) { service.AddTransient(c => dbOption.dbConnection as DbConnection); if (dbOption.dbType == DbType.DbEnum.Mysql) { service.AddTransient <IDbExtend, MySqlDbExtend>(); } else if (dbOption.dbType == DbType.DbEnum.SqlServer) { service.AddTransient <IDbExtend, SqlServerDbExtend>(); //sqlserve 在第二次获取上下文对象时,连接字符串为空(原因未知),需要重新为对象赋值 DbContextModel dbContext = new DbContextModel() { connStr = dbOption.dbConnStr }; service.AddTransient(c => dbContext); } else if (dbOption.dbType == DbType.DbEnum.Oracle) { service.AddTransient <IDbExtend, OracleDbExtend>(); } else { throw new AttrSqlException("无法识别的数据库类型."); } return(service); }