示例#1
0
        public IDbConnection CreateConnection()
        {
            IDbConnection connection = InternalDbProvider.CreateConnection();

            connection.ConnectionString = ConnectionString;
            return(connection);
        }
示例#2
0
        public IDbCommand CreateCommand(IDbConnection connection)
        {
            TkDebug.AssertArgumentNull(connection, "connection", this);

            IDbCommand command = InternalDbProvider.CreateCommand();

            command.Connection = connection;
            return(command);
        }
示例#3
0
        //public IUnitOfWork GetUnitOfWork(Type dbContextType, string dbName = null)
        //{
        //    Check.NotNull(dbContextType, nameof(dbContextType));
        //    // 若替换之前的UnitOfWork则有可能造成数据库连接释放不及时
        //    // 若使用锁,可以确保UnitOrWork在当前Scope生命周期的唯一性,但会影响性能
        //    var key = string.Format("{0}${1}$", dbName, dbContextType.FullName);
        //    if (_works.ContainsKey(key))
        //    {
        //        return _works[key];
        //    }
        //    else
        //    {
        //        foreach (var k in _works.Keys)
        //        {
        //            if (k.StartsWith(key))
        //            {
        //                return _works[k];
        //            }
        //        }

        //        IDbContext dbContext;
        //        key += DateTime.Now.Ticks.ToString();
        //        var dbConnectionOptionsMap = _serviceProvider.GetRequiredService<IOptions<DbConnectionMapOptions>>().Value;
        //        if (dbConnectionOptionsMap == null || dbConnectionOptionsMap.Count <= 0)
        //        {
        //            throw new RyeException("无法获取数据库配置");
        //        }

        //        DbConnectionOptions dbConnectionOptions = dbName == null ? dbConnectionOptionsMap.First().Value : dbConnectionOptionsMap[dbName];

        //        var builderOptions = _serviceProvider.GetServices<DbContextOptionsBuilderOptions>()
        //              ?.Where(d => (dbName == null || d.DbName == null || d.DbName == dbName) && (d.DbContextType == null || d.DbContextType == dbContextType))
        //              ?.OrderByDescending(d => d.DbName)
        //              ?.OrderByDescending(d => d.DbContextType);
        //        if (builderOptions == null || !builderOptions.Any())
        //        {
        //            throw new RyeException("无法获取匹配的DbContextOptionsBuilder");
        //        }

        //        var dbUser = _serviceProvider.GetServices<IDbContextOptionsBuilderUser>()?.FirstOrDefault(u => u.Type == dbConnectionOptions.DatabaseType);
        //        if (dbUser == null)
        //        {
        //            throw new RyeException($"无法解析类型为“{dbConnectionOptions.DatabaseType}”的 {typeof(IDbContextOptionsBuilderUser).FullName} 实例");
        //        }


        //        var dbContextOptions = dbUser.Use(builderOptions.First().Builder, dbConnectionOptions.ConnectionString).Options;
        //        if (_expressionFactoryDict.TryGetValue(dbContextType, out Func<IServiceProvider, DbContextOptions, IDbContext> factory))
        //        {
        //            dbContext = factory(_serviceProvider, dbContextOptions);
        //        }
        //        else
        //        {
        //            // 使用Expression创建DbContext
        //            var constructorMethod = dbContextType.GetConstructors()
        //                .Where(c => c.IsPublic && !c.IsAbstract && !c.IsStatic)
        //                .OrderByDescending(c => c.GetParameters().Length)
        //                .FirstOrDefault();
        //            if (constructorMethod == null)
        //            {
        //                throw new RyeException("无法获取有效的上下文构造器");
        //            }

        //            var dbContextOptionsBuilderType = typeof(DbContextOptionsBuilder<>);
        //            var dbContextOptionsType = typeof(DbContextOptions);
        //            var dbContextOptionsGenericType = typeof(DbContextOptions<>);
        //            var serviceProviderType = typeof(IServiceProvider);
        //            var getServiceMethod = serviceProviderType.GetMethod("GetService");
        //            var lambdaParameterExpressions = new ParameterExpression[2];
        //            lambdaParameterExpressions[0] = (Expression.Parameter(serviceProviderType, "serviceProvider"));
        //            lambdaParameterExpressions[1] = (Expression.Parameter(dbContextOptionsType, "dbContextOptions"));
        //            var paramTypes = constructorMethod.GetParameters();
        //            var argumentExpressions = new Expression[paramTypes.Length];
        //            for (int i = 0; i < paramTypes.Length; i++)
        //            {
        //                var pType = paramTypes[i];
        //                if (pType.ParameterType == dbContextOptionsType ||
        //                    (pType.ParameterType.IsGenericType && pType.ParameterType.GetGenericTypeDefinition() == dbContextOptionsGenericType))
        //                {
        //                    argumentExpressions[i] = Expression.Convert(lambdaParameterExpressions[1], pType.ParameterType);
        //                }
        //                else if (pType.ParameterType == serviceProviderType)
        //                {
        //                    argumentExpressions[i] = lambdaParameterExpressions[0];
        //                }
        //                else
        //                {
        //                    argumentExpressions[i] = Expression.Call(lambdaParameterExpressions[0], getServiceMethod);
        //                }
        //            }

        //            factory = Expression
        //                .Lambda<Func<IServiceProvider, DbContextOptions, IDbContext>>(
        //                    Expression.Convert(Expression.New(constructorMethod, argumentExpressions), typeof(IDbContext)), lambdaParameterExpressions.AsEnumerable())
        //                .Compile();
        //            _expressionFactoryDict.TryAdd(dbContextType, factory);

        //            dbContext = factory(_serviceProvider, dbContextOptions);
        //        }

        //        var unitOfWorkFactory = _serviceProvider.GetRequiredService<IUnitOfWorkFactory>();
        //        var unitOfWork = unitOfWorkFactory.GetUnitOfWork(_serviceProvider, dbContext);
        //        _works.Add(key, unitOfWork);
        //        return unitOfWork;
        //    }
        //}

        public IUnitOfWork GetUnitOfWork <TDbContext>(string dbName = null)
            where TDbContext : DbContext, IDbContext
        {
            var dbContextType = typeof(TDbContext);
            var key           = string.Format("{0}${1}$", dbName, dbContextType.FullName);

            foreach (var k in _works.Keys)
            {
                if (k.StartsWith(key))
                {
                    return(_works[k]);
                }
            }

            var unitOfWork = InternalDbProvider <TDbContext> .GetUnitOfWork(_serviceProvider, dbName);

            _works.Add($"{key}{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}{_random.Next(1000)}", unitOfWork);
            return(unitOfWork);
        }
示例#4
0
 public IDbDataAdapter CreateDataAdapter()
 {
     return(InternalDbProvider.CreateDataAdapter());
 }
示例#5
0
 public string GetParamName(string fieldName, bool isOrigin)
 {
     return(InternalDbProvider.GetParamName(fieldName, isOrigin));
 }
示例#6
0
 public IDbDataParameter CreateParameter(TkDataType dataType)
 {
     return(InternalDbProvider.CreateParameter(dataType));
 }
示例#7
0
 public IDbCommand CreateCommand()
 {
     return(InternalDbProvider.CreateCommand());
 }