/// <summary> /// 获取 Provider 的配置。 /// </summary> /// <param name="setting">数据库实例配置对象。</param> /// <param name="section">提供者配置节对象。</param> /// <returns>一个提供者配置对象。</returns> private static ProviderConfigurationSetting GetProviderSetting(IInstanceConfigurationSetting setting, ProviderConfigurationSection section) { if (section == null || string.IsNullOrEmpty(setting.ProviderName)) { return(null); } return(section.Settings[setting.ProviderName]); }
/// <summary> /// 根据 <paramref name="setting"/> 获取对应的 <see cref="IProvider"/> 对象。 /// </summary> /// <param name="setting">配置节信息。</param> /// <returns></returns> public static IProvider GetDefinedProviderInstance(IInstanceConfigurationSetting setting) { if (!string.IsNullOrEmpty(setting.ProviderName)) { return(GetDefinedProviderInstance(setting.ProviderName)); } return(GetDefinedProviderInstance(setting.ProviderType)); }
/// <summary> /// 使用数据库配置实例创建一个 <see cref="Database"/> 实例对象。 /// </summary> /// <param name="setting">实例配置对象。</param> /// <returns>一个 <see cref="Database"/> 实例对象。</returns> public static IDatabase CreateDatabase(IInstanceConfigurationSetting setting) { Guard.ArgumentNull(setting, "setting"); var section = ConfigurationUnity.GetSection <ProviderConfigurationSection>(); var providerSetting = GetProviderSetting(setting, section); var provider = GetProvider(setting, providerSetting); Guard.Assert(provider != null, new Exception(SR.GetString(SRKind.ProviderNotSupported))); var database = new Database(setting.ConnectionString, provider); return(database); }
/// <summary> /// 使用数据库配置实例创建一个 <see cref="Database"/> 实例对象。 /// </summary> /// <param name="setting">实例配置对象。</param> /// <param name="serviceProvider">检索服务提供者。</param> /// <returns>一个 <see cref="Database"/> 实例对象。</returns> public static IDatabase CreateDatabase(IInstanceConfigurationSetting setting, IServiceProvider serviceProvider = null) { Guard.ArgumentNull(setting, nameof(setting)); var section = ConfigurationUnity.GetSection <ProviderConfigurationSection>(); var providerSetting = GetProviderSetting(setting, section); var provider = GetProvider(setting, providerSetting); Guard.Assert(provider != null, new Exception(SR.GetString(SRKind.ProviderNotSupported))); if (setting.Clusters.Count > 0) { return(new ScopedDatabase(GetDistributedConnections(setting), provider)); } return(new ScopedDatabase(setting.ConnectionString, provider)); }
/// <summary> /// 获取分布式的数据库连接串。 /// </summary> /// <param name="setting"></param> /// <returns></returns> private static List <DistributedConnectionString> GetDistributedConnections(IInstanceConfigurationSetting setting) { if (setting.Clusters.Count > 0) { var connections = new List <DistributedConnectionString>(); foreach (var cluster in setting.Clusters) { connections.Add(new DistributedConnectionString(cluster.ConnectionString) { Mode = cluster.Mode, Weight = cluster.Weight }); } return(connections); } return(null); }
/// <summary> /// 获取 IProvidre,如果没有 setting,则获取预定义类别的 IProvider 单例。 /// </summary> /// <param name="setting">数据库实例配置对象。</param> /// <param name="providerSetting">提供者配置对象。</param> /// <returns>一个提供者对象。</returns> private static IProvider GetProvider(IInstanceConfigurationSetting setting, ProviderConfigurationSetting providerSetting) { var providerName = providerSetting != null ? providerSetting.Name : setting.ProviderType; return(ProviderHelper.GetDefinedProviderInstance(providerName)); }