设为首页 加入收藏

TOP

EF Core 实现读写分离的最佳方案(四)
2019-10-09 19:59:10 】 浏览:161
Tags:Core 实现 读写 分离 最佳 方案
der, dbConnectionOptions.ConnectionString).Options; if (_expressionFactoryDict.TryGetValue(dbContextType, out Func<IServiceProvider, DbContextOptions, DbContext> 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 Exception("无法获取有效的上下文构造器"); } 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, DbContext>>( Expression.Convert(Expression.New(constructorMethod, argumentExpressions), typeof(DbContext)), lambdaParameterExpressions.AsEnumerable()) .Compile(); _expressionFactoryDict.TryAdd(dbContextType, factory); dbContext = factory(_serviceProvider, dbContextOptions); } var unitOfWorkFactory = _serviceProvider.GetRequiredService<IUnitOfWorkFactory>(); unitOfWork = unitOfWorkFactory.GetUnitOfWork(_serviceProvider, dbContext); _works.TryAdd(key, unitOfWork); return unitOfWork; } } public void Dispose() { if (_works != null && _works.Count > 0) { foreach (var unitOfWork in _works.Values) unitOfWork.Dispose(); _works.Clear(); } } } public static class DbProviderExtensions { public static IUnitOfWork GetUnitOfWork<TDbContext>(this IDbProvider provider, string dbName = null) { if (provider == null) return null; return provider.GetUnitOfWork(typeof(TDbContext), dbName); } }
    /// <summary>
    /// 业务系统配置选项
    /// </summary>
    public class FxOptions
    {
        public FxOptions()
        {
        }

        /// <summary>
        /// 默认数据库类型
        /// </summary>
        public DatabaseType DefaultDatabaseType { get; set; } = DatabaseType.SqlServer;

        /// <summary>
        /// 数据库连接配置
        /// </summary>
        public IDictionary<string, DbC
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇矩阵连乘求解优化 下一篇在 ASP.NET Core 项目中使用 Auto..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目