EdisonTalk.MongoProxy组件发布v0.0.6版本

大家好,我是Edison。

组件发布的背景

之前工作中需要用到MongoDB的事务操作,因此参考了一些资料封装了一个小的组件,提供基础的CRUD Repository基类 和 UnitOfWork工作单元模式。但是,我一直都没有把它正式发布到Nuget仓库中,近日抽空把发布了,大家可以搜到它并使用了。

组件具有哪些功能

EdisonTalk.MongoProxy可以提供以下几个重要功能:

(1)提供标准的配置项注入

比如我们在appsettings中填写如下配置,通过提供的扩展方法可以快速注册和MongoDB的连接客户端。这个配置项兼顾了普通账号用户名 以及 SSL证书验证模式。

复制代码
"MongoDatabaseConfigs": {
  "Servers": "xxx01.edisontalk.net,xxx02.edisontalk.net,xxx03.edisontalk.net",
  "Port": 27017,
  "ReplicaSetName": "edt-replica",
  "DatabaseName": "EDT_Practices",
  "UserName": "xxxxxxxxxxxxx",
  "Password": "xxxxxxxxxxxxx",
  "UseTLS": true, // default: false
  "AllowInsecureTLS": true, // default: true
  "SslCertificatePath": "/etc/pki/tls/certs/EDT_CA.cer" // default: null
}

(2)封装对MongoDB的Repository访问

针对MongoDB封装了MongoRepositoryBase的接口和实现,针对单文档的CRUD都无需再自己实现,只需集成基类即可实现单文档的CRUD。

(3)封装对MongoDB的UnitOfWork操作

针对MongoDB封装了UnitOfWork操作,针对多文档的事务操作,使用该模式可以方便实现。

(4)封装对MongoDB的连接字符串构造

在日常使用中,我们会用到基于配置文件构造MongoDB连接字符串的场景。比如,在CAP项目中,如果我们用到MongoDB作为存储,那么就需要提供MongoDB连接字符串,因此基于标准配置项,我们提供了一个MongoDbConnUtil类用于构造连接字符串。

下面展示了CAP集成MongoDB使用MongoDbConnUtil的GetMongoDbConnectionString方法来构造:

复制代码
option.UseMongoDB(option =>
{
    option.DatabaseConnection = MongoDbConnUtil.GetMongoDbConnectionString(config);
    ......
});

如何使用该组件:三步上篮

预备步骤:安装组件

复制代码
PM> NuGet\Install-Package EdisonTalk.MongoProxy -Version 0.0.6

第一步:注入MongoProxy核心部分

在appsettings中配置MongoDB的连接信息:

复制代码
"MongoDatabaseConfigs": {
  "Servers": "xxx01.edisontalk.net,xxx02.edisontalk.net,xxx03.edisontalk.net",
  "Port": 27017,
  "ReplicaSetName": "edt-replica",
  "DatabaseName": "EDT_Practices",
  "UserName": "xxxxxxxxxxxxx",
  "Password": "xxxxxxxxxxxxx"
}

然后通过扩展方法注入MongoProxy相关部分:

复制代码
builder.Services.AddMongoProxy(builder.Configuration);

第二步:添加Entity 和 Repository

示例Entity:这里的Table标签需要指名你的集合名字,组件会自动映射上对应集合!

复制代码
[Table("Orders")]
public class OrderEntity : MongoEntityBase
{
    public string OrderNumber { get; set; }
    public List<TransmissionEntity> Transmissions { get; set; }
}

示例Repository:

复制代码
public interface ITodoItemRepository : IMongoRepositoryBase<TodoItem>
{
}

public class TodoItemRepository : MongoRepositoryBase<TodoItem>, ITodoItemRepository
{
   public TodoItemRepository(IMongoDbContext mongoDbContext) 
      : base(mongoDbContext)
   {
   }
}

services.AddScoped<ITodoItemRepository, TodoItemRepository>();

第三步:使用Repository 和 UnitOfWork

复制代码
# 非事务模式
await _taskRepository.AddManyAsync(newTasks);
# 事务模式(借助UnitOfWork工作单元)
private readonly IUnitOfWork _unitOfWork;

public OrderService(IUnitOfWork unitOfWork, ......)
{
    _unitOfWork = unitOfWork;
    ......
}

public async Task Example()
{
    using var session = await _unitOfWork.BeginTransactionAsync())
    await _taskRepository.AddManyAsync(newTasks, session);
    await _orderRepository.AddAsync(newOrder, session);

    await _unitOfWork.SaveChangesAsync(session);
}

小结

欢迎大家使用这个组件,我也会持续更新和完善。

附录

GitHub:https://github.com/Coder-EdisonZhou/EdisonTalk.MongoProxy

Nuget:https://www.nuget.org/packages/EdisonTalk.MongoProxy


作者:周旭龙

出处:https://edisonchou.cnblogs.com

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

相关推荐
西门啐血5 小时前
上位机开发之假装有设备,使用 C# 模拟串口设备
开发语言·mongodb·c#
吴声子夜歌11 小时前
MongoDB 8.0——聚合
数据库·mongodb
吴声子夜歌1 天前
MongoDB 8.0——可视化管理工具
数据库·mongodb
吴声子夜歌1 天前
MongoDB 4.2——安装 MongoDB
数据库·mongodb
吴声子夜歌1 天前
MongoDB 8.0——MongoDB的安装
数据库·mongodb
吴声子夜歌3 天前
MongoDB 4.2——了解应用程序的动态
数据库·mongodb
czhaii3 天前
STC ai助手单片机工程项目创建实例
数据库·mongodb
吴声子夜歌3 天前
MongoDB 4.2——配置分片
数据库·mongodb
吴声子夜歌3 天前
MongoDB 4.2——分片简介
数据库·mongodb
吴声子夜歌4 天前
MongoDB 4.2——备份
数据库·mongodb