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

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

相关推荐
时光追逐者6 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 51 期(2025年8.18-8.24)
c#·.net·.netcore·.net core
lssjzmn1 天前
SpringBoot + RabbitMQ + MongoDB + Redis:高性能高并发高可用的半导体生产工厂数据处理落地实践
spring boot·mongodb
DONG9131 天前
《三驾马车:MySQL、MongoDB、Redis对比与融合实战》
数据库·redis·sql·mysql·mongodb·database
两张不够花2 天前
Shell脚本源码安装Redis、MySQL、Mongodb、PostgreSQL(无报错版)
linux·数据库·redis·mysql·mongodb·postgresql·云计算
Java 码农2 天前
nodejs 集成mongodb实现增删改查
数据库·mongodb
孙霸天3 天前
Ubuntu20系统上离线安装MongoDB
数据库·mongodb·ubuntu·备份还原
Java 码农3 天前
nodejs mongodb基础
数据库·mongodb·node.js
满目8283 天前
MongoDB 从入门到实践:全面掌握文档型 NoSQL 数据库核心操作
数据库·mongodb·非关系型数据库·文档型数据库·基础查询命令·进阶查询
郭京京4 天前
mongodb基础
mongodb·go
鼠鼠我捏,要死了捏4 天前
生产环境MongoDB分片策略优化与故障排查实战经验分享
数据库·mongodb·分片