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

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

相关推荐
葫芦和十三12 小时前
图解 MongoDB 03|CRUD 全链路:一条 find 怎么穿过 WiredTiger
后端·mongodb·agent
葫芦和十三20 小时前
图解 MongoDB 04|索引模型:每建一个索引,就是在 B+-tree 森林里多栽一棵
后端·mongodb·agent
葫芦和十三2 天前
图解 MongoDB 02|BSON:你以为存的是 JSON,其实是带类型的二进制
后端·mongodb·agent
葫芦和十三2 天前
图解 MongoDB 01|文档数据库
后端·mongodb·agent
✎ ﹏梦醒͜ღ҉繁华落℘4 天前
单片机基础知识---stm32单片机的优先级
stm32·单片机·mongodb
JLWcai202510094 天前
铸造领域树脂砂轮|金利威多场景解决方案,20 + 配方覆盖全需求
mongodb·zookeeper·eureka·spark·rabbitmq·memcached·storm
终将老去的穷苦程序员4 天前
MongoDB Community Server 安装教程
数据库·mongodb
曾阿伦4 天前
深入了解MongoDB 两地三中心架构
数据库·mongodb·架构
Yue1684 天前
球球了主人,请把我当傻子调教吧系列之MongoDB数据库
mongodb
H__Rick5 天前
C51单片机学习-DAY3
单片机·学习·mongodb