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

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

相关推荐
IT规划师4 小时前
Redis 数据类型全解 - Hash 还是 String + JSON?一文终结存储选型之争(三)
redis·.net core·哈希表·.net10
IT规划师2 天前
Redis 数据类型全解 - String 不只是 get set:90% 开发者忽略的 8 个高阶用法与内存陷阱(一)
redis·.net core
后台模板学习3 天前
MongoDB 与 MySQL 在 IM 即时聊天场景中该
数据库·mysql·mongodb
ChaITSimpleLove3 天前
Layout.WatchDog.NET 1.5.0-beta.2 发布:.NET 实时应用监控再升级,新增资源监控看板与 SQLite 支持
mysql·mongodb·pgsql·mssql·litedb\sqlite·.net轻量级日志记录器·node资源监控看板
IT规划师4 天前
MQTT实战 - MQTT高频面试题 + 我的8年实战答案(十一)
物联网·mqtt·面试·.net core·iot·mqttnet
编码追梦人6 天前
基于 ESP32-S3 的便携式多参数环境监护仪设计:从硬件选型到 FreeRTOS 多任务落地
数据库·单片机·嵌入式硬件·mongodb·esp32·freertos·低功耗设计