-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7619 from abpframework/module-db-group
Define and handle AbpDbConnectionOptions.Databases option.
- Loading branch information
Showing
6 changed files
with
165 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
framework/src/Volo.Abp.Data/Volo/Abp/Data/AbpDatabaseInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>(); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
framework/src/Volo.Abp.Data/Volo/Abp/Data/AbpDatabaseInfoDictionary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
} | ||
} |
44 changes: 43 additions & 1 deletion
44
framework/src/Volo.Abp.Data/Volo/Abp/Data/AbpDbConnectionOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters