From 41909dd4ba2bcb1d65fb4729979fd2944c972360 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Fri, 30 Aug 2019 09:22:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0MongoDB=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E8=BF=9E=E6=8E=A5=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E4=B8=AA=E5=BA=93=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/appsettings.json | 11 +++--- .../DbManager.cs | 36 +++++++++--------- .../Bootstrap.Client.DataAccess.csproj | 2 +- Bootstrap.Client/appsettings.Development.json | 3 +- Bootstrap.Client/appsettings.json | 6 +-- Bootstrap.DataAccess.MongoDB/DbManager.cs | 38 ++++++++++--------- .../Bootstrap.DataAccess.csproj | 2 +- Bootstrap.DataAccess/DbManager.cs | 2 +- 8 files changed, 52 insertions(+), 48 deletions(-) diff --git a/Bootstrap.Admin/appsettings.json b/Bootstrap.Admin/appsettings.json index e35f4879..1a3979c6 100644 --- a/Bootstrap.Admin/appsettings.json +++ b/Bootstrap.Admin/appsettings.json @@ -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 @@ } ] } -} +} \ No newline at end of file diff --git a/Bootstrap.Client.DataAccess.MongoDB/DbManager.cs b/Bootstrap.Client.DataAccess.MongoDB/DbManager.cs index d1c95972..70d0414a 100644 --- a/Bootstrap.Client.DataAccess.MongoDB/DbManager.cs +++ b/Bootstrap.Client.DataAccess.MongoDB/DbManager.cs @@ -9,7 +9,7 @@ using MongoDB.Driver; namespace Bootstrap.Client.DataAccess.MongoDB { /// - /// + /// 数据库操作类 /// internal static class DbManager { @@ -18,9 +18,9 @@ namespace Bootstrap.Client.DataAccess.MongoDB private static readonly object _locker = new object(); /// - /// + /// /// - 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 /// - /// + /// Dicts 集合 /// public static IMongoCollection Dicts { get { - return DBAccess.GetCollection("Dicts"); + return BADatabase.GetCollection("Dicts"); } } /// - /// + /// Users 集合 /// public static IMongoCollection Users { get { - return DBAccess.GetCollection("Users"); + return BADatabase.GetCollection("Users"); } } /// - /// + /// Roles 集合 /// public static IMongoCollection Roles { get { - return DBAccess.GetCollection("Roles"); + return BADatabase.GetCollection("Roles"); } } /// - /// + /// Groups 集合 /// public static IMongoCollection Groups { get { - return DBAccess.GetCollection("Groups"); + return BADatabase.GetCollection("Groups"); } } /// - /// + /// Menus 集合 /// public static IMongoCollection Menus { get { - return DBAccess.GetCollection("Navigations"); + return BADatabase.GetCollection("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() diff --git a/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj b/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj index 5d003875..612bfef5 100644 --- a/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj +++ b/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj @@ -5,7 +5,7 @@ - + diff --git a/Bootstrap.Client/appsettings.Development.json b/Bootstrap.Client/appsettings.Development.json index 9ddc6e49..9d06767e 100644 --- a/Bootstrap.Client/appsettings.Development.json +++ b/Bootstrap.Client/appsettings.Development.json @@ -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" } } ], diff --git a/Bootstrap.Client/appsettings.json b/Bootstrap.Client/appsettings.json index 4df83da3..aef8585e 100644 --- a/Bootstrap.Client/appsettings.json +++ b/Bootstrap.Client/appsettings.json @@ -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 @@ } ] } -} +} \ No newline at end of file diff --git a/Bootstrap.DataAccess.MongoDB/DbManager.cs b/Bootstrap.DataAccess.MongoDB/DbManager.cs index bfb1bb8d..b529215b 100644 --- a/Bootstrap.DataAccess.MongoDB/DbManager.cs +++ b/Bootstrap.DataAccess.MongoDB/DbManager.cs @@ -9,7 +9,7 @@ using MongoDB.Driver; namespace Bootstrap.DataAccess.MongoDB { /// - /// + /// 数据库操作类 /// internal static class DbManager { @@ -18,7 +18,7 @@ namespace Bootstrap.DataAccess.MongoDB private static readonly object _locker = new object(); /// - /// + /// IMongoDatabase 实例 /// 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 /// - /// + /// Logs 集合 /// public static IMongoCollection Logs { @@ -54,7 +55,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// Exceptions 集合 /// public static IMongoCollection Exceptions { @@ -64,7 +65,7 @@ namespace Bootstrap.DataAccess.MongoDB } } /// - /// + /// Dicts 集合 /// public static IMongoCollection Dicts { @@ -75,7 +76,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// Users 集合 /// public static IMongoCollection Users { @@ -86,7 +87,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// Groups 集合 /// public static IMongoCollection Groups { @@ -97,7 +98,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// Roles 集合 /// public static IMongoCollection Roles { @@ -108,7 +109,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// Menus 集合 /// public static IMongoCollection Menus { @@ -119,7 +120,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// LoginUsers 集合 /// public static IMongoCollection LoginUsers { @@ -130,7 +131,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// ResetUsers 集合 /// public static IMongoCollection ResetUsers { @@ -141,7 +142,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// Traces 集合 /// public static IMongoCollection Traces { @@ -152,7 +153,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// RejectUsers 集合 /// public static IMongoCollection RejectUsers { @@ -163,7 +164,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// Messages 集合 /// public static IMongoCollection Messages { @@ -174,7 +175,7 @@ namespace Bootstrap.DataAccess.MongoDB } /// - /// + /// Tasks 集合 /// public static IMongoCollection 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() diff --git a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index 8919646e..e5f02365 100644 --- a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -6,7 +6,7 @@ - + diff --git a/Bootstrap.DataAccess/DbManager.cs b/Bootstrap.DataAccess/DbManager.cs index 7ba531a6..b63ea92b 100644 --- a/Bootstrap.DataAccess/DbManager.cs +++ b/Bootstrap.DataAccess/DbManager.cs @@ -7,7 +7,7 @@ namespace Bootstrap.DataAccess /// /// /// - internal static class DbManager + public static class DbManager { /// ///