Browse Source

提交代码

dev/1.0.1
stevelee 2 years ago
parent
commit
80d94f411b
6 changed files with 129 additions and 71 deletions
  1. +4
    -20
      src/BPA.Component.MongoClient/BPA.Component.MongoClient/BaseMongoDbClient.cs
  2. +35
    -20
      src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/BaseMongoDbRepository.cs
  3. +15
    -9
      src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/IBaseMongoDbRepository.cs
  4. +73
    -20
      src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Program.cs
  5. +1
    -1
      src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Repositorys/ICorpInfoRepository.cs
  6. +1
    -1
      src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/TestMongoDcClient.cs

+ 4
- 20
src/BPA.Component.MongoClient/BPA.Component.MongoClient/BaseMongoDbClient.cs View File

@@ -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<TMongoDbClient> : MongoDB.Driver.MongoClient
where TMongoDbClient : BaseMongoDbClient<TMongoDbClient>
public class BaseMongoDbClient<TMongoDbClient> where TMongoDbClient : BaseMongoDbClient<TMongoDbClient>
{
/// <summary>
/// Logger
@@ -15,7 +12,7 @@ public class BaseMongoDbClient<TMongoDbClient> : MongoDB.Driver.MongoClient
/// <summary>
/// MongoConfig
/// </summary>
private static MongoConfig _config;
protected internal readonly MongoConfig Config;

/// <summary>
/// CurrentDatabaseName
@@ -30,20 +27,7 @@ public class BaseMongoDbClient<TMongoDbClient> : MongoDB.Driver.MongoClient
public BaseMongoDbClient(MongoConfig config, ILogger<TMongoDbClient> logger = null)
{
CurrentDatabaseName = config.DatabasesName;
_config = config;
Config = config;
_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;
}
}

+ 35
- 20
src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/BaseMongoDbRepository.cs View File

@@ -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;
/// </summary>
public class BaseMongoDbRepository<TMongoDbClient, TDocument> : IBaseMongoDbRepository<TDocument>
where TMongoDbClient : BaseMongoDbClient<TMongoDbClient>
where TDocument : class, new()
where TDocument : new()
{
/// <summary>
/// MongoClient
@@ -44,9 +45,8 @@ public class BaseMongoDbRepository<TMongoDbClient, TDocument> : IBaseMongoDbRepo
/// </summary>
public BaseMongoDbRepository(BaseMongoDbClient<TMongoDbClient> 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<TMongoDbClient, TDocument> : IBaseMongoDbRepo
}

/// <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>
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>
/// <param name="document"></param>
/// <typeparam name="TDocument"></typeparam>
/// <param name="whereExpression"></param>
/// <param name="options"></param>
/// <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);
}
}

+ 15
- 9
src/BPA.Component.MongoClient/BPA.Component.MongoClient/Repository/IBaseMongoDbRepository.cs View File

@@ -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);
}

+ 73
- 20
src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Program.cs View File

@@ -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<ICorpInfoRepository, CorpInfoRepository>();

var serviceProvider = services.BuildServiceProvider();
var testDbClient = serviceProvider.GetService<TestMongoDcClient>();
if (testDbClient == null) return;

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,
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 = "一号办公地点"},
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"}
});
};

// 添加一个
// 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);
}
}



+ 1
- 1
src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/Repositorys/ICorpInfoRepository.cs View File

@@ -5,6 +5,6 @@ namespace BPA.Component.MongoClientTester.Repositorys
{
public interface ICorpInfoRepository : IBaseMongoDbRepository<CorpInfo>
{
Task<int> AddAsync<TDocument>(TDocument document) where TDocument : class, new();
}
}

+ 1
- 1
src/BPA.Component.MongoClient/BPA.Component.MongoClientTester/TestMongoDcClient.cs View File

@@ -7,7 +7,7 @@ namespace BPA.Component.MongoClientTester;
public class TestMongoDcClient : BaseMongoDbClient<TestMongoDcClient>
{
public TestMongoDcClient(TestConfig config, ILogger<TestMongoDcClient> logger = null)
: base(BuildMongoConfig(config.MongoDbForWebApi1Db.ConnectionString, config.MongoDbForWebApi1Db.DatabasesName), logger)
: base(config.MongoDbForWebApi1Db, logger)
{
}
}

Loading…
Cancel
Save