refactor: 更新MongoDB数据库连接字符串支持多个库同时使用

This commit is contained in:
Argo Zhang 2019-08-30 09:22:03 +08:00 committed by Argo Zhang
parent ea0bd37405
commit 41909dd4ba
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
8 changed files with 52 additions and 48 deletions

View File

@ -15,7 +15,6 @@
"ConnectionStrings": {
"ba": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa"
},
"MongoDB": "BootstrapAdmin",
"DB": [
{
"Enabled": false
@ -28,25 +27,25 @@
}
},
{
"Enabled": true,
"Enabled": false,
"ProviderName": "MySql",
"ConnectionStrings": {
"ba": "Server=localhost;Database=BA;Uid=argozhang;Pwd=argo@163.com;SslMode=none;"
}
},
{
"Enabled": true,
"Enabled": false,
"ProviderName": "Npgsql",
"ConnectionStrings": {
"ba": "Server=localhost;Database=BootstrapAdmin;User ID=argozhang;Password=argo@163.com;"
}
},
{
"Enabled": true,
"Enabled": false,
"Widget": "Bootstrap.DataAccess.MongoDB",
"ProviderName": "MongoDB",
"ConnectionStrings": {
"ba": "mongodb://localhost:27017"
"ba": "mongodb://localhost:27017;Database=BootstrapAdmin"
}
}
],
@ -244,4 +243,4 @@
}
]
}
}
}

View File

@ -9,7 +9,7 @@ using MongoDB.Driver;
namespace Bootstrap.Client.DataAccess.MongoDB
{
/// <summary>
///
/// 数据库操作类
/// </summary>
internal static class DbManager
{
@ -18,9 +18,9 @@ namespace Bootstrap.Client.DataAccess.MongoDB
private static readonly object _locker = new object();
/// <summary>
///
///
/// </summary>
private static IMongoDatabase DBAccess
private static IMongoDatabase BADatabase
{
get
{
@ -34,7 +34,8 @@ namespace Bootstrap.Client.DataAccess.MongoDB
ChangeToken.OnChange(() => ConfigurationManager.AppSettings.GetReloadToken(), () => _db = null);
InitClassMap();
}
InitDb();
if (_db == null)
InitDb("ba");
}
}
return _db;
@ -43,65 +44,66 @@ namespace Bootstrap.Client.DataAccess.MongoDB
#region Collections
/// <summary>
///
/// Dicts 集合
/// </summary>
public static IMongoCollection<BootstrapDict> Dicts
{
get
{
return DBAccess.GetCollection<BootstrapDict>("Dicts");
return BADatabase.GetCollection<BootstrapDict>("Dicts");
}
}
/// <summary>
///
/// Users 集合
/// </summary>
public static IMongoCollection<User> Users
{
get
{
return DBAccess.GetCollection<User>("Users");
return BADatabase.GetCollection<User>("Users");
}
}
/// <summary>
///
/// Roles 集合
/// </summary>
public static IMongoCollection<Role> Roles
{
get
{
return DBAccess.GetCollection<Role>("Roles");
return BADatabase.GetCollection<Role>("Roles");
}
}
/// <summary>
///
/// Groups 集合
/// </summary>
public static IMongoCollection<Group> Groups
{
get
{
return DBAccess.GetCollection<Group>("Groups");
return BADatabase.GetCollection<Group>("Groups");
}
}
/// <summary>
///
/// Menus 集合
/// </summary>
public static IMongoCollection<BootstrapMenu> Menus
{
get
{
return DBAccess.GetCollection<BootstrapMenu>("Navigations");
return BADatabase.GetCollection<BootstrapMenu>("Navigations");
}
}
#endregion
private static void InitDb()
private static void InitDb(string name = null)
{
var client = new MongoClient(Longbow.Data.DbManager.GetConnectionString("ba"));
_db = client.GetDatabase(ConfigurationManager.AppSettings["MongoDB"]);
var (connectString, databaseName) = Longbow.Data.DbManager.GetMongoDB(name);
var client = new MongoClient(connectString);
_db = client.GetDatabase(databaseName);
}
private static void InitClassMap()

View File

@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Longbow.Data" Version="2.3.3" />
<PackageReference Include="Longbow.Data" Version="2.3.4" />
<PackageReference Include="Longbow.Logging" Version="2.2.12" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.13" />
</ItemGroup>

View File

@ -36,7 +36,8 @@
"Widget": "Bootstrap.Client.DataAccess.MongoDB",
"ProviderName": "MongoDB",
"ConnectionStrings": {
"ba": "mongodb://localhost:27017"
"ba": "mongodb://localhost:27017;Database=BootstrapAdmin",
"client": "mongodb://localhost:27017;Database=BootstrapClient"
}
}
],

View File

@ -29,11 +29,11 @@
}
},
{
"Enabled": true,
"Enabled": false,
"Widget": "Bootstrap.Client.DataAccess.MongoDB",
"ProviderName": "MongoDB",
"ConnectionStrings": {
"ba": "mongodb://localhost:27017"
"ba": "mongodb://localhost:27017;Database=BootstrapAdmin"
}
}
],
@ -96,4 +96,4 @@
}
]
}
}
}

View File

@ -9,7 +9,7 @@ using MongoDB.Driver;
namespace Bootstrap.DataAccess.MongoDB
{
/// <summary>
///
/// 数据库操作类
/// </summary>
internal static class DbManager
{
@ -18,7 +18,7 @@ namespace Bootstrap.DataAccess.MongoDB
private static readonly object _locker = new object();
/// <summary>
///
/// IMongoDatabase 实例
/// </summary>
private static IMongoDatabase DBAccess
{
@ -34,7 +34,8 @@ namespace Bootstrap.DataAccess.MongoDB
ChangeToken.OnChange(() => ConfigurationManager.AppSettings.GetReloadToken(), () => _db = null);
InitClassMap();
}
InitDb();
if (_db == null)
InitDb();
}
}
return _db;
@ -43,7 +44,7 @@ namespace Bootstrap.DataAccess.MongoDB
#region Collections
/// <summary>
///
/// Logs 集合
/// </summary>
public static IMongoCollection<DataAccess.Log> Logs
{
@ -54,7 +55,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// Exceptions 集合
/// </summary>
public static IMongoCollection<DataAccess.Exceptions> Exceptions
{
@ -64,7 +65,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
}
/// <summary>
///
/// Dicts 集合
/// </summary>
public static IMongoCollection<BootstrapDict> Dicts
{
@ -75,7 +76,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// Users 集合
/// </summary>
public static IMongoCollection<User> Users
{
@ -86,7 +87,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// Groups 集合
/// </summary>
public static IMongoCollection<Group> Groups
{
@ -97,7 +98,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// Roles 集合
/// </summary>
public static IMongoCollection<Role> Roles
{
@ -108,7 +109,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// Menus 集合
/// </summary>
public static IMongoCollection<BootstrapMenu> Menus
{
@ -119,7 +120,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// LoginUsers 集合
/// </summary>
public static IMongoCollection<DataAccess.LoginUser> LoginUsers
{
@ -130,7 +131,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// ResetUsers 集合
/// </summary>
public static IMongoCollection<DataAccess.ResetUser> ResetUsers
{
@ -141,7 +142,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// Traces 集合
/// </summary>
public static IMongoCollection<DataAccess.Trace> Traces
{
@ -152,7 +153,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// RejectUsers 集合
/// </summary>
public static IMongoCollection<RejectUser> RejectUsers
{
@ -163,7 +164,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// Messages 集合
/// </summary>
public static IMongoCollection<DataAccess.Message> Messages
{
@ -174,7 +175,7 @@ namespace Bootstrap.DataAccess.MongoDB
}
/// <summary>
///
/// Tasks 集合
/// </summary>
public static IMongoCollection<DataAccess.Task> Tasks
{
@ -187,8 +188,9 @@ namespace Bootstrap.DataAccess.MongoDB
private static void InitDb()
{
var client = new MongoClient(Longbow.Data.DbManager.GetConnectionString());
_db = client.GetDatabase(ConfigurationManager.AppSettings["MongoDB"]);
var (connectString, databaseName) = Longbow.Data.DbManager.GetMongoDB();
var client = new MongoClient(connectString);
_db = client.GetDatabase(databaseName);
}
private static void InitClassMap()

View File

@ -6,7 +6,7 @@
<ItemGroup>
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.12" />
<PackageReference Include="Longbow.Data" Version="2.3.3" />
<PackageReference Include="Longbow.Data" Version="2.3.4" />
<PackageReference Include="Longbow.Logging" Version="2.2.12" />
<PackageReference Include="Longbow.PetaPoco" Version="1.0.2" />
<PackageReference Include="Longbow.Security.Cryptography" Version="1.3.0" />

View File

@ -7,7 +7,7 @@ namespace Bootstrap.DataAccess
/// <summary>
///
/// </summary>
internal static class DbManager
public static class DbManager
{
/// <summary>
///