mirror of https://github.com/abpframework/abp.git
committed by
GitHub
6 changed files with 165 additions and 29 deletions
@ -0,0 +1,28 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public class AbpDatabaseInfo |
|||
{ |
|||
public string DatabaseName { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// List of connection names mapped to this database.
|
|||
/// </summary>
|
|||
public HashSet<string> MappedConnections { get; } |
|||
|
|||
/// <summary>
|
|||
/// Is this database used by tenants. Set this to true if this database
|
|||
/// can't owned by tenants.
|
|||
///
|
|||
/// Default: true.
|
|||
/// </summary>
|
|||
public bool IsUsedByTenants { get; set; } = true; |
|||
|
|||
internal AbpDatabaseInfo(string databaseName) |
|||
{ |
|||
DatabaseName = databaseName; |
|||
MappedConnections = new HashSet<string>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,58 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public class AbpDatabaseInfoDictionary : Dictionary<string, AbpDatabaseInfo> |
|||
{ |
|||
private Dictionary<string, AbpDatabaseInfo> ConnectionIndex { get; set; } |
|||
|
|||
public AbpDatabaseInfoDictionary() |
|||
{ |
|||
ConnectionIndex = new Dictionary<string, AbpDatabaseInfo>(); |
|||
} |
|||
|
|||
[CanBeNull] |
|||
public AbpDatabaseInfo GetMappedDatabaseOrNull(string connectionStringName) |
|||
{ |
|||
return ConnectionIndex.GetOrDefault(connectionStringName); |
|||
} |
|||
|
|||
public AbpDatabaseInfoDictionary Configure(string databaseName, Action<AbpDatabaseInfo> configureAction) |
|||
{ |
|||
var databaseInfo = this.GetOrAdd( |
|||
databaseName, |
|||
() => new AbpDatabaseInfo(databaseName) |
|||
); |
|||
|
|||
configureAction(databaseInfo); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// This method should be called if this dictionary changes.
|
|||
/// It refreshes indexes for quick access to the connection informations.
|
|||
/// </summary>
|
|||
public void RefreshIndexes() |
|||
{ |
|||
ConnectionIndex = new Dictionary<string, AbpDatabaseInfo>(); |
|||
|
|||
foreach (var databaseInfo in Values) |
|||
{ |
|||
foreach (var mappedConnection in databaseInfo.MappedConnections) |
|||
{ |
|||
if (ConnectionIndex.ContainsKey(mappedConnection)) |
|||
{ |
|||
throw new AbpException( |
|||
$"A connection name can not map to multiple databases: {mappedConnection}." |
|||
); |
|||
} |
|||
|
|||
ConnectionIndex[mappedConnection] = databaseInfo; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,12 +1,54 @@ |
|||
namespace Volo.Abp.Data |
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public class AbpDbConnectionOptions |
|||
{ |
|||
public ConnectionStrings ConnectionStrings { get; set; } |
|||
|
|||
public AbpDatabaseInfoDictionary Databases { get; set; } |
|||
|
|||
public AbpDbConnectionOptions() |
|||
{ |
|||
ConnectionStrings = new ConnectionStrings(); |
|||
Databases = new AbpDatabaseInfoDictionary(); |
|||
} |
|||
|
|||
public string GetConnectionStringOrNull( |
|||
string connectionStringName, |
|||
bool fallbackToDatabaseMappings = true, |
|||
bool fallbackToDefault = true) |
|||
{ |
|||
var connectionString = ConnectionStrings.GetOrDefault(connectionStringName); |
|||
if (!connectionString.IsNullOrEmpty()) |
|||
{ |
|||
return connectionString; |
|||
} |
|||
|
|||
if (fallbackToDatabaseMappings) |
|||
{ |
|||
var database = Databases.GetMappedDatabaseOrNull(connectionStringName); |
|||
if (database != null) |
|||
{ |
|||
connectionString = ConnectionStrings.GetOrDefault(database.DatabaseName); |
|||
if (!connectionString.IsNullOrEmpty()) |
|||
{ |
|||
return connectionString; |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (fallbackToDefault) |
|||
{ |
|||
connectionString = ConnectionStrings.Default; |
|||
if (!connectionString.IsNullOrWhiteSpace()) |
|||
{ |
|||
return connectionString; |
|||
} |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue