diff --git a/src/BPA.Component.MongoClient/BPA.Component.MongoClient/BaseMongoDbClient.cs b/src/BPA.Component.MongoClient/BPA.Component.MongoClient/BaseMongoDbClient.cs index 31025e4..bcf0a10 100644 --- a/src/BPA.Component.MongoClient/BPA.Component.MongoClient/BaseMongoDbClient.cs +++ b/src/BPA.Component.MongoClient/BPA.Component.MongoClient/BaseMongoDbClient.cs @@ -1,11 +1,8 @@ -using System.Runtime.CompilerServices; -using Microsoft.Extensions.Logging; -using MongoDB.Driver.Core.Configuration; +using Microsoft.Extensions.Logging; namespace BPA.Component.MongoClient; -public class BaseMongoDbClient : MongoDB.Driver.MongoClient - where TMongoDbClient : BaseMongoDbClient +public class BaseMongoDbClient where TMongoDbClient : BaseMongoDbClient { /// /// Logger @@ -15,7 +12,7 @@ public class BaseMongoDbClient : MongoDB.Driver.MongoClient /// /// MongoConfig /// - private static MongoConfig _config; + protected internal readonly MongoConfig Config; /// /// CurrentDatabaseName @@ -30,20 +27,7 @@ public class BaseMongoDbClient : MongoDB.Driver.MongoClient public BaseMongoDbClient(MongoConfig config, ILogger logger = null) { CurrentDatabaseName = config.DatabasesName; - _config = config; + Config = config; _logger = logger; } - - /// - /// BuildMongoConfig - /// - /// - /// - /// - protected static MongoConfig BuildMongoConfig(string connectionString, string databaseName) - { - _config = new MongoConfig {ConnectionString = connectionString, DatabasesName = databaseName}; - return _config; - } - } \ No newline at end of file diff --git a/src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/BaseMongoDbRepository.cs b/src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/BaseMongoDbRepository.cs index 4df7b25..71560a6 100644 --- a/src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/BaseMongoDbRepository.cs +++ b/src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/BaseMongoDbRepository.cs @@ -1,4 +1,5 @@ -using BPA.Component.MongoClient.Conventions; +using System.Linq.Expressions; +using BPA.Component.MongoClient.Conventions; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Conventions; @@ -13,7 +14,7 @@ namespace BPA.Component.MongoClient.Repository; /// public class BaseMongoDbRepository : IBaseMongoDbRepository where TMongoDbClient : BaseMongoDbClient - where TDocument : class, new() + where TDocument : new() { /// /// MongoClient @@ -44,9 +45,8 @@ public class BaseMongoDbRepository : IBaseMongoDbRepo /// public BaseMongoDbRepository(BaseMongoDbClient mongoDbClient) { - _mongoDbClient = mongoDbClient; + _mongoDbClient = new MongoDB.Driver.MongoClient(mongoDbClient.Config.ConnectionString); _database = _mongoDbClient.GetDatabase(mongoDbClient.CurrentDatabaseName); - var serializer = new DateTimeSerializer(DateTimeKind.Local, BsonType.DateTime); BsonSerializer.RegisterSerializer(typeof(DateTime), serializer); BsonSerializer.RegisterSerializer(typeof(JObject), new JObjectSerializer()); @@ -65,35 +65,50 @@ public class BaseMongoDbRepository : IBaseMongoDbRepo } /// - /// 变更集合 + /// 插入一个 /// - /// - public virtual void ChangeCollection(string collectionName) + /// + /// + /// + public async Task AddAsync(TDocument document) { - _collection = _database.GetCollection(collectionName); + if (document == null) return 0; + var coll = _database.GetCollection(document.GetType().Name); + await coll.InsertOneAsync(document); + + return 1; } + /// - /// 检查当前文档集 + /// 插入多个 /// - private void Checkcollection() + /// + /// + public async Task AddListAsync(IEnumerable documents) { - if (_collection == null) - { - throw new ArgumentNullException($"当前要操作的文档集为空,请调用{nameof(ChangeCollection)}方法切换集合文档"); - } + if (documents == null) return 0; + var coll = _database.GetCollection(documents.First().GetType().Name); + await coll.InsertManyAsync(documents); + + return 1; } + /// - /// 插入一个 + /// 修改一个 /// /// - /// + /// + /// /// - public async Task AddAsync(TDocument document) where TDocument : class, new() + /// + public async Task UpdateOneAsync(TDocument document, Expression> whereExpression, UpdateOptions options = null) { - var coll = _database.GetCollection(document.GetType().Name); - await coll.InsertOneAsync(document); - return 1; + if (document == null) throw new ArgumentNullException(""); + throw new NotImplementedException(); + // return await _database + // .GetCollection(document.GetType().Name) + // .UpdateOneAsync(whereExpression, new JsonUpdateDefinition(document), options); } } \ No newline at end of file diff --git a/src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/IBaseMongoDbRepository.cs b/src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/IBaseMongoDbRepository.cs index 256f763..498a7d7 100644 --- a/src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/IBaseMongoDbRepository.cs +++ b/src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/IBaseMongoDbRepository.cs @@ -1,12 +1,18 @@ -using MongoDB.Driver; +using System.Linq.Expressions; +using MongoDB.Driver; -namespace BPA.Component.MongoClient.Repository +namespace BPA.Component.MongoClient.Repository; + +/// +/// MongoDB仓储接口 +/// +public interface IBaseMongoDbRepository where TDocument : new() { - /// - /// MongoDB仓储接口 - /// - public interface IBaseMongoDbRepository where TDocument : class, new() - { - MongoDB.Driver.MongoClient _mongoDbClient { get; } - } + MongoDB.Driver.MongoClient _mongoDbClient { get; } + + Task AddAsync(TDocument document); + + Task AddListAsync(IEnumerable documents); + + Task UpdateOneAsync(TDocument document, Expression> whereExpression, UpdateOptions options = null); } \ No newline at end of file diff --git a/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Program.cs b/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Program.cs index 2e6a26a..86b2578 100644 --- a/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Program.cs +++ b/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Program.cs @@ -10,7 +10,7 @@ namespace BPA.Component.MongoClientTester; internal class Program { - private static async Task Main(string[] args) + private static async Task Main() { var builder = new ConfigurationBuilder(); builder.AddEnvironmentVariables(); @@ -24,33 +24,86 @@ internal class Program services.AddTransient(); var serviceProvider = services.BuildServiceProvider(); - var testDbClient = serviceProvider.GetService(); - if (testDbClient == null) return; - var userRepository = serviceProvider.GetService(); - var tags = new Dictionary - { - {"datetime", DateTime.Now}, - {"array0-1", new object[] {1, true, 66.2D, Enums.Aaa, DateTime.Now, ""}}, - {"array00", null}, - {"array01", new List {"sss", "bbb"}}, - {"array02", new List {new[] {"1", "2"}, new[] {"3", "4"}}}, - {"array03", new List {new() {Address = "ss", Id = DateTime.Now.Ticks, Latitude = 1, Longitude = 2, Remark = ""}}}, - }; - await userRepository.AddAsync(new CorpInfo + + var data = new CorpInfo { + // Id = 0, DB = long.MaxValue, AddressInfos = new List { - new() {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府三街", Remark = "一号办公地点"}, - new() {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府五街", Remark = "二号办公地点"}, + new() {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府三街", Remark = "一号办公地点1"}, + new() {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府五街", Remark = "二号办公地点1"}, }, - Name = "太阳国1", - No = "sun1", + Name = "太阳国11111", + No = "sun11111", Creator = new CorpAddressInfo {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府三街", Remark = "一号办公地点"}, - Tags = tags, + Tags = new Dictionary + { + {"datetime", DateTime.Now}, + {"array0-1", new object[] {1, true, 66.2D, Enums.Aaa, DateTime.Now, ""}}, + {"array00", null}, + {"array01", new List {"sss", "bbb"}}, + {"array02", new List {new[] {"1", "2"}, new[] {"3", "4"}}}, + {"array03", new List {new() {Address = "ss", Id = DateTime.Now.Ticks, Latitude = 1, Longitude = 2, Remark = ""}}}, + }, Phones = new List {"1921682012", "1921682012", "1921682012"} - }); + }; + + // 添加一个 + // await userRepository.AddAsync(data); + // 添加多个 + // await userRepository.AddListAsync(new List() + // { + // new() + // { + // Id = DateTime.Now.Ticks, + // DB = long.MaxValue, + // AddressInfos = new List + // { + // new() {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府三街", Remark = "一号办公地点"}, + // new() {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府五街", Remark = "二号办公地点"}, + // }, + // Name = "太阳国1", + // No = "sun1", + // Creator = new CorpAddressInfo {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府三街", Remark = "一号办公地点"}, + // Tags = new Dictionary + // { + // {"datetime", DateTime.Now}, + // {"array0-1", new object[] {1, true, 66.2D, Enums.Aaa, DateTime.Now, ""}}, + // {"array00", null}, + // {"array01", new List {"sss", "bbb"}}, + // {"array02", new List {new[] {"1", "2"}, new[] {"3", "4"}}}, + // {"array03", new List {new() {Address = "ss", Id = DateTime.Now.Ticks, Latitude = 1, Longitude = 2, Remark = ""}}}, + // }, + // Phones = new List {"1921682012", "1921682012", "1921682012"} + // }, + // new() + // { + // Id = DateTime.Now.Ticks, + // DB = long.MaxValue, + // AddressInfos = new List + // { + // new() {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府三街", Remark = "一号办公地点"}, + // new() {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府五街", Remark = "二号办公地点"}, + // }, + // Name = "太阳国1", + // No = "sun1", + // Creator = new CorpAddressInfo {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府三街", Remark = "一号办公地点"}, + // Tags = new Dictionary + // { + // {"datetime", DateTime.Now}, + // {"array0-1", new object[] {1, true, 66.2D, Enums.Aaa, DateTime.Now, ""}}, + // {"array00", null}, + // {"array01", new List {"sss", "bbb"}}, + // {"array02", new List {new[] {"1", "2"}, new[] {"3", "4"}}}, + // {"array03", new List {new() {Address = "ss", Id = DateTime.Now.Ticks, Latitude = 1, Longitude = 2, Remark = ""}}}, + // }, + // Phones = new List {"1921682012", "1921682012", "1921682012"} + // } + // }); + + await userRepository.UpdateOneAsync(data, info => info.CorpId == 1); } } diff --git a/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Repositorys/ICorpInfoRepository.cs b/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Repositorys/ICorpInfoRepository.cs index e8e44ba..8e477ed 100644 --- a/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Repositorys/ICorpInfoRepository.cs +++ b/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Repositorys/ICorpInfoRepository.cs @@ -5,6 +5,6 @@ namespace BPA.Component.MongoClientTester.Repositorys { public interface ICorpInfoRepository : IBaseMongoDbRepository { - Task AddAsync(TDocument document) where TDocument : class, new(); + } } diff --git a/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/TestMongoDcClient.cs b/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/TestMongoDcClient.cs index 8479224..6b8fca1 100644 --- a/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/TestMongoDcClient.cs +++ b/src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/TestMongoDcClient.cs @@ -7,7 +7,7 @@ namespace BPA.Component.MongoClientTester; public class TestMongoDcClient : BaseMongoDbClient { public TestMongoDcClient(TestConfig config, ILogger logger = null) - : base(BuildMongoConfig(config.MongoDbForWebApi1Db.ConnectionString, config.MongoDbForWebApi1Db.DatabasesName), logger) + : base(config.MongoDbForWebApi1Db, logger) { } } \ No newline at end of file