@@ -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; | namespace BPA.Component.MongoClient; | ||||
public class BaseMongoDbClient<TMongoDbClient> : MongoDB.Driver.MongoClient | |||||
where TMongoDbClient : BaseMongoDbClient<TMongoDbClient> | |||||
public class BaseMongoDbClient<TMongoDbClient> where TMongoDbClient : BaseMongoDbClient<TMongoDbClient> | |||||
{ | { | ||||
/// <summary> | /// <summary> | ||||
/// Logger | /// Logger | ||||
@@ -15,7 +12,7 @@ public class BaseMongoDbClient<TMongoDbClient> : MongoDB.Driver.MongoClient | |||||
/// <summary> | /// <summary> | ||||
/// MongoConfig | /// MongoConfig | ||||
/// </summary> | /// </summary> | ||||
private static MongoConfig _config; | |||||
protected internal readonly MongoConfig Config; | |||||
/// <summary> | /// <summary> | ||||
/// CurrentDatabaseName | /// CurrentDatabaseName | ||||
@@ -30,20 +27,7 @@ public class BaseMongoDbClient<TMongoDbClient> : MongoDB.Driver.MongoClient | |||||
public BaseMongoDbClient(MongoConfig config, ILogger<TMongoDbClient> logger = null) | public BaseMongoDbClient(MongoConfig config, ILogger<TMongoDbClient> logger = null) | ||||
{ | { | ||||
CurrentDatabaseName = config.DatabasesName; | CurrentDatabaseName = config.DatabasesName; | ||||
_config = config; | |||||
Config = config; | |||||
_logger = logger; | _logger = logger; | ||||
} | } | ||||
/// <summary> | |||||
/// BuildMongoConfig | |||||
/// </summary> | |||||
/// <param name="connectionString"></param> | |||||
/// <param name="databaseName"></param> | |||||
/// <returns></returns> | |||||
protected static MongoConfig BuildMongoConfig(string connectionString, string databaseName) | |||||
{ | |||||
_config = new MongoConfig {ConnectionString = connectionString, DatabasesName = databaseName}; | |||||
return _config; | |||||
} | |||||
} | } |
@@ -1,4 +1,5 @@ | |||||
using BPA.Component.MongoClient.Conventions; | |||||
using System.Linq.Expressions; | |||||
using BPA.Component.MongoClient.Conventions; | |||||
using MongoDB.Bson; | using MongoDB.Bson; | ||||
using MongoDB.Bson.Serialization; | using MongoDB.Bson.Serialization; | ||||
using MongoDB.Bson.Serialization.Conventions; | using MongoDB.Bson.Serialization.Conventions; | ||||
@@ -13,7 +14,7 @@ namespace BPA.Component.MongoClient.Repository; | |||||
/// </summary> | /// </summary> | ||||
public class BaseMongoDbRepository<TMongoDbClient, TDocument> : IBaseMongoDbRepository<TDocument> | public class BaseMongoDbRepository<TMongoDbClient, TDocument> : IBaseMongoDbRepository<TDocument> | ||||
where TMongoDbClient : BaseMongoDbClient<TMongoDbClient> | where TMongoDbClient : BaseMongoDbClient<TMongoDbClient> | ||||
where TDocument : class, new() | |||||
where TDocument : new() | |||||
{ | { | ||||
/// <summary> | /// <summary> | ||||
/// MongoClient | /// MongoClient | ||||
@@ -44,9 +45,8 @@ public class BaseMongoDbRepository<TMongoDbClient, TDocument> : IBaseMongoDbRepo | |||||
/// </summary> | /// </summary> | ||||
public BaseMongoDbRepository(BaseMongoDbClient<TMongoDbClient> mongoDbClient) | public BaseMongoDbRepository(BaseMongoDbClient<TMongoDbClient> mongoDbClient) | ||||
{ | { | ||||
_mongoDbClient = mongoDbClient; | |||||
_mongoDbClient = new MongoDB.Driver.MongoClient(mongoDbClient.Config.ConnectionString); | |||||
_database = _mongoDbClient.GetDatabase(mongoDbClient.CurrentDatabaseName); | _database = _mongoDbClient.GetDatabase(mongoDbClient.CurrentDatabaseName); | ||||
var serializer = new DateTimeSerializer(DateTimeKind.Local, BsonType.DateTime); | var serializer = new DateTimeSerializer(DateTimeKind.Local, BsonType.DateTime); | ||||
BsonSerializer.RegisterSerializer(typeof(DateTime), serializer); | BsonSerializer.RegisterSerializer(typeof(DateTime), serializer); | ||||
BsonSerializer.RegisterSerializer(typeof(JObject), new JObjectSerializer()); | BsonSerializer.RegisterSerializer(typeof(JObject), new JObjectSerializer()); | ||||
@@ -65,35 +65,50 @@ public class BaseMongoDbRepository<TMongoDbClient, TDocument> : IBaseMongoDbRepo | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// 变更集合 | |||||
/// 插入一个 | |||||
/// </summary> | /// </summary> | ||||
/// <param name="collectionName"></param> | |||||
public virtual void ChangeCollection(string collectionName) | |||||
/// <param name="document"></param> | |||||
/// <typeparam name="TDocument"></typeparam> | |||||
/// <returns></returns> | |||||
public async Task<int> AddAsync<TDocument>(TDocument document) | |||||
{ | { | ||||
_collection = _database.GetCollection<TDocument>(collectionName); | |||||
if (document == null) return 0; | |||||
var coll = _database.GetCollection<TDocument>(document.GetType().Name); | |||||
await coll.InsertOneAsync(document); | |||||
return 1; | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// 检查当前文档集 | |||||
/// 插入多个 | |||||
/// </summary> | /// </summary> | ||||
private void Checkcollection() | |||||
/// <typeparam name="TDocument"></typeparam> | |||||
/// <returns></returns> | |||||
public async Task<int> AddListAsync<TDocument>(IEnumerable<TDocument> documents) | |||||
{ | { | ||||
if (_collection == null) | |||||
{ | |||||
throw new ArgumentNullException($"当前要操作的文档集为空,请调用{nameof(ChangeCollection)}方法切换集合文档"); | |||||
} | |||||
if (documents == null) return 0; | |||||
var coll = _database.GetCollection<TDocument>(documents.First().GetType().Name); | |||||
await coll.InsertManyAsync(documents); | |||||
return 1; | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// 插入一个 | |||||
/// 修改一个 | |||||
/// </summary> | /// </summary> | ||||
/// <param name="document"></param> | /// <param name="document"></param> | ||||
/// <typeparam name="TDocument"></typeparam> | |||||
/// <param name="whereExpression"></param> | |||||
/// <param name="options"></param> | |||||
/// <returns></returns> | /// <returns></returns> | ||||
public async Task<int> AddAsync<TDocument>(TDocument document) where TDocument : class, new() | |||||
/// <exception cref="ArgumentNullException"></exception> | |||||
public async Task<UpdateResult> UpdateOneAsync<TDocument>(TDocument document, Expression<Func<TDocument, bool>> whereExpression, UpdateOptions options = null) | |||||
{ | { | ||||
var coll = _database.GetCollection<TDocument>(document.GetType().Name); | |||||
await coll.InsertOneAsync(document); | |||||
return 1; | |||||
if (document == null) throw new ArgumentNullException(""); | |||||
throw new NotImplementedException(); | |||||
// return await _database | |||||
// .GetCollection<TDocument>(document.GetType().Name) | |||||
// .UpdateOneAsync(whereExpression, new JsonUpdateDefinition<TDocument>(document), options); | |||||
} | } | ||||
} | } |
@@ -1,12 +1,18 @@ | |||||
using MongoDB.Driver; | |||||
using System.Linq.Expressions; | |||||
using MongoDB.Driver; | |||||
namespace BPA.Component.MongoClient.Repository | |||||
namespace BPA.Component.MongoClient.Repository; | |||||
/// <summary> | |||||
/// MongoDB仓储接口 | |||||
/// </summary> | |||||
public interface IBaseMongoDbRepository<TDocument> where TDocument : new() | |||||
{ | { | ||||
/// <summary> | |||||
/// MongoDB仓储接口 | |||||
/// </summary> | |||||
public interface IBaseMongoDbRepository<TDocument> where TDocument : class, new() | |||||
{ | |||||
MongoDB.Driver.MongoClient _mongoDbClient { get; } | |||||
} | |||||
MongoDB.Driver.MongoClient _mongoDbClient { get; } | |||||
Task<int> AddAsync<TDocument>(TDocument document); | |||||
Task<int> AddListAsync<TDocument>(IEnumerable<TDocument> documents); | |||||
Task<UpdateResult> UpdateOneAsync<TDocument>(TDocument document, Expression<Func<TDocument, bool>> whereExpression, UpdateOptions options = null); | |||||
} | } |
@@ -10,7 +10,7 @@ namespace BPA.Component.MongoClientTester; | |||||
internal class Program | internal class Program | ||||
{ | { | ||||
private static async Task Main(string[] args) | |||||
private static async Task Main() | |||||
{ | { | ||||
var builder = new ConfigurationBuilder(); | var builder = new ConfigurationBuilder(); | ||||
builder.AddEnvironmentVariables(); | builder.AddEnvironmentVariables(); | ||||
@@ -24,33 +24,86 @@ internal class Program | |||||
services.AddTransient<ICorpInfoRepository, CorpInfoRepository>(); | services.AddTransient<ICorpInfoRepository, CorpInfoRepository>(); | ||||
var serviceProvider = services.BuildServiceProvider(); | var serviceProvider = services.BuildServiceProvider(); | ||||
var testDbClient = serviceProvider.GetService<TestMongoDcClient>(); | |||||
if (testDbClient == null) return; | |||||
var userRepository = serviceProvider.GetService<ICorpInfoRepository>(); | var userRepository = serviceProvider.GetService<ICorpInfoRepository>(); | ||||
var tags = new Dictionary<string, object> | |||||
{ | |||||
{"datetime", DateTime.Now}, | |||||
{"array0-1", new object[] {1, true, 66.2D, Enums.Aaa, DateTime.Now, ""}}, | |||||
{"array00", null}, | |||||
{"array01", new List<string> {"sss", "bbb"}}, | |||||
{"array02", new List<string[]> {new[] {"1", "2"}, new[] {"3", "4"}}}, | |||||
{"array03", new List<CorpAddressInfo> {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, | DB = long.MaxValue, | ||||
AddressInfos = new List<CorpAddressInfo> | AddressInfos = new List<CorpAddressInfo> | ||||
{ | { | ||||
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 = "一号办公地点"}, | Creator = new CorpAddressInfo {Id = DateTime.Now.Ticks, Latitude = 66.52, Longitude = 451.63, Address = "天府大道天府三街", Remark = "一号办公地点"}, | ||||
Tags = tags, | |||||
Tags = new Dictionary<string, object> | |||||
{ | |||||
{"datetime", DateTime.Now}, | |||||
{"array0-1", new object[] {1, true, 66.2D, Enums.Aaa, DateTime.Now, ""}}, | |||||
{"array00", null}, | |||||
{"array01", new List<string> {"sss", "bbb"}}, | |||||
{"array02", new List<string[]> {new[] {"1", "2"}, new[] {"3", "4"}}}, | |||||
{"array03", new List<CorpAddressInfo> {new() {Address = "ss", Id = DateTime.Now.Ticks, Latitude = 1, Longitude = 2, Remark = ""}}}, | |||||
}, | |||||
Phones = new List<string> {"1921682012", "1921682012", "1921682012"} | Phones = new List<string> {"1921682012", "1921682012", "1921682012"} | ||||
}); | |||||
}; | |||||
// 添加一个 | |||||
// await userRepository.AddAsync(data); | |||||
// 添加多个 | |||||
// await userRepository.AddListAsync(new List<CorpInfo>() | |||||
// { | |||||
// new() | |||||
// { | |||||
// Id = DateTime.Now.Ticks, | |||||
// DB = long.MaxValue, | |||||
// AddressInfos = new List<CorpAddressInfo> | |||||
// { | |||||
// 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<string, object> | |||||
// { | |||||
// {"datetime", DateTime.Now}, | |||||
// {"array0-1", new object[] {1, true, 66.2D, Enums.Aaa, DateTime.Now, ""}}, | |||||
// {"array00", null}, | |||||
// {"array01", new List<string> {"sss", "bbb"}}, | |||||
// {"array02", new List<string[]> {new[] {"1", "2"}, new[] {"3", "4"}}}, | |||||
// {"array03", new List<CorpAddressInfo> {new() {Address = "ss", Id = DateTime.Now.Ticks, Latitude = 1, Longitude = 2, Remark = ""}}}, | |||||
// }, | |||||
// Phones = new List<string> {"1921682012", "1921682012", "1921682012"} | |||||
// }, | |||||
// new() | |||||
// { | |||||
// Id = DateTime.Now.Ticks, | |||||
// DB = long.MaxValue, | |||||
// AddressInfos = new List<CorpAddressInfo> | |||||
// { | |||||
// 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<string, object> | |||||
// { | |||||
// {"datetime", DateTime.Now}, | |||||
// {"array0-1", new object[] {1, true, 66.2D, Enums.Aaa, DateTime.Now, ""}}, | |||||
// {"array00", null}, | |||||
// {"array01", new List<string> {"sss", "bbb"}}, | |||||
// {"array02", new List<string[]> {new[] {"1", "2"}, new[] {"3", "4"}}}, | |||||
// {"array03", new List<CorpAddressInfo> {new() {Address = "ss", Id = DateTime.Now.Ticks, Latitude = 1, Longitude = 2, Remark = ""}}}, | |||||
// }, | |||||
// Phones = new List<string> {"1921682012", "1921682012", "1921682012"} | |||||
// } | |||||
// }); | |||||
await userRepository.UpdateOneAsync(data, info => info.CorpId == 1); | |||||
} | } | ||||
} | } | ||||
@@ -5,6 +5,6 @@ namespace BPA.Component.MongoClientTester.Repositorys | |||||
{ | { | ||||
public interface ICorpInfoRepository : IBaseMongoDbRepository<CorpInfo> | public interface ICorpInfoRepository : IBaseMongoDbRepository<CorpInfo> | ||||
{ | { | ||||
Task<int> AddAsync<TDocument>(TDocument document) where TDocument : class, new(); | |||||
} | } | ||||
} | } |
@@ -7,7 +7,7 @@ namespace BPA.Component.MongoClientTester; | |||||
public class TestMongoDcClient : BaseMongoDbClient<TestMongoDcClient> | public class TestMongoDcClient : BaseMongoDbClient<TestMongoDcClient> | ||||
{ | { | ||||
public TestMongoDcClient(TestConfig config, ILogger<TestMongoDcClient> logger = null) | public TestMongoDcClient(TestConfig config, ILogger<TestMongoDcClient> logger = null) | ||||
: base(BuildMongoConfig(config.MongoDbForWebApi1Db.ConnectionString, config.MongoDbForWebApi1Db.DatabasesName), logger) | |||||
: base(config.MongoDbForWebApi1Db, logger) | |||||
{ | { | ||||
} | } | ||||
} | } |