HMEFramework.MongoDB
2.8.0
dotnet add package HMEFramework.MongoDB --version 2.8.0
NuGet\Install-Package HMEFramework.MongoDB -Version 2.8.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HMEFramework.MongoDB" Version="2.8.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HMEFramework.MongoDB" Version="2.8.0" />
<PackageReference Include="HMEFramework.MongoDB" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HMEFramework.MongoDB --version 2.8.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HMEFramework.MongoDB, 2.8.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HMEFramework.MongoDB@2.8.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HMEFramework.MongoDB&version=2.8.0
#tool nuget:?package=HMEFramework.MongoDB&version=2.8.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HMEFramework.MongoDB
简介
HMEFramework.MongoDB 是基于官方MongoDB.Driver的增强封装库,专为.NET Core应用提供更便捷的MongoDB集成方案。支持MongoDB 4.0+版本,提供符合领域驱动设计(DDD)的仓储模式实现。
核心优势
🚀 性能优化
- 智能连接池管理(默认100-500连接)
- 批量操作自动优化
- 异步全链路支持
🔐 企业级安全
- TLS/SSL加密支持
- 完善的错误重试机制
- 事务ACID保证
🧩 开箱即用
- 自动集合命名映射
- 内置常用CRUD操作
- 支持LINQ表达式查询
功能特性
- 清洁架构:Context → Repository → Service 清晰分层
- 全异步支持:所有方法均提供Async版本
- 事务支持:跨操作事务一致性保证
- 灵活配置:支持多节点集群、SSL连接、连接池调优
- 智能映射:自动处理集合命名与类型映射
运行环境
- 跨平台支持: Supports .NET Standard 2.1, .NET 6+.
安装
dotnet add package HMEFramework.MongoDB
配置数据库连接
添加配置到 appsettings.json:
{
"MongoConfig": {
"ConnectionStrings": ["server1:27017", "server2:27017"],
"DbName": "your_db",
"UserName": "user",
"Password": "pwd",
"UseSsl": true,
"MaxPoolSize": 500
}
}
服务注册
builder.Services.Configure<MongoConfig>(builder.Configuration.GetSection("MongoConfig"));
builder.Services.AddMongoDBSetup(builder.Configuration.Get<MongoConfig>()!);
使用案例:
1.定义实体
[Table("users")]
public class User
{
[BsonId]
public string Id { get; set; }
public string Name { get; set; }
public DateTime CreateTime { get; set; }
}
2.创建仓储
public class UserRepository : BaseMongoRepository<User>
{
public UserRepository(IMongoContext context) : base(context)
{
}
// 自定义查询方法
public async Task<List<User>> GetActiveUsersAsync()
{
var collection = GetCollection();
return await collection.Find(u => u.CreateTime > DateTime.UtcNow.AddDays(-30))
.ToListAsync();
}
}
3.业务服务
public class UserService : BaseMongoService<User>
{
public UserService(IMongoCRUD<User> repository) : base(repository)
{
}
public async Task UpdateUserWithTransactionAsync(string userId, Action<User> update)
{
using var session = await StartSessionAsync();
session.StartTransaction();
try {
var user = await FindByIdAsync(userId);
update(user);
await ReplaceOneAsync(userId, user);
await session.CommitTransactionAsync();
}
catch {
await session.AbortTransactionAsync();
throw;
}
}
}
核心组件
1. IMongoContext
提供对 MongoContext 的基本操作接口。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- HMEFramework (>= 2.8.0)
- Microsoft.Extensions.Options (>= 10.0.2)
- MongoDB.Driver (>= 3.5.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on HMEFramework.MongoDB:
| Package | Downloads |
|---|---|
|
HMEFramework.Repository
HMEFramework.Repository for .net core |
GitHub repositories
This package is not used by any popular GitHub repositories.