Option

 1)注册仓储  AbpCommonDbContextRegistrationOptions(基类)

为了创建Options,使用了建造者模式IAbpCommonDbContextRegistrationOptionsBuilder,在依赖注入注册,IServiceCollection的扩展方法,使用匿名委托

Action<IAbpCommonDbContextRegistrationOptionsBuilder>提供给用户进行配置

用法:可以给每一个entity设置仓储,仓储泛型参数分两个,一个key,一个无key,为了简单可以给整个 DbContext设置仓储,此默认仓储的提供在 Options下属性DefaultRepositoryDbContextType,为了简单还设置一个RepositoryRegistrarBase的基类来实现注册,如果用户指定默认仓储实现类(SetDefaultRepositoryClasses),则按此SpecifiedDefaultRepositoryTypes, 则使用其基类的实现类GetRepositoryType方法来实现,

规格:

AddDefaultRepositories:用于给DbContext的默认仓储

AddRepository:给每个entity设定仓储

SetDefaultRepositoryClasses:默认仓储提供的Class

ReplaceDbContext 替换DbContext

其RepositoryRegistrarBase基类的实现有有EfCoreRepositoryRegistrar,MemoryDbRepositoryRegistrar,MongoDbRepositoryRegistrar

 2)AbpDbConnectionOptions

字典,Dictionary<string, string>;默认的是DefaultConnectionStringName是Default,提供Get与Set方法

用法,DefaultConnectionStringResolver,根据提供的connectionStringName,从AbpDbConnectionOptions里的Dictionary解析出字符串

MultiTenantConnectionStringResolver:是覆盖方法,

public override string Resolve(string connectionStringName = null)
        {
            //No current tenant, fallback to default logic
            if (_currentTenant.Id == null)
            {
                return base.Resolve(connectionStringName);
            }
            using (var serviceScope = _serviceProvider.CreateScope())
            {
                var tenantStore = serviceScope
                    .ServiceProvider
                    .GetRequiredService<ITenantStore>();
                var tenant = tenantStore.Find(_currentTenant.Id.Value);
                if (tenant?.ConnectionStrings == null)
                {
                    return base.Resolve(connectionStringName);
                }
                //Requesting default connection string
                if (connectionStringName == null)
                {
                    return tenant.ConnectionStrings.Default ??
                           Options.ConnectionStrings.Default;
                }
                //Requesting specific connection string
                var connString = tenant.ConnectionStrings.GetOrDefault(connectionStringName);
                if (connString != null)
                {
                    return connString;
                }
                /* Requested a specific connection string, but it's not specified for the tenant.
                 * - If it's specified in options, use it.
                 * - If not, use tenant's default conn string.
                 */
                var connStringInOptions = Options.ConnectionStrings.GetOrDefault(connectionStringName);
                if (connStringInOptions != null)
                {
                    return connStringInOptions;
                }
                return tenant.ConnectionStrings.Default ??
                       Options.ConnectionStrings.Default;
            }
        }

 

 

posted on 2019-11-06 11:51  dollymi  阅读(553)  评论(0编辑  收藏  举报

导航