HMENetCore.MongoDB 6.0.48

dotnet add package HMENetCore.MongoDB --version 6.0.48
                    
NuGet\Install-Package HMENetCore.MongoDB -Version 6.0.48
                    
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="HMENetCore.MongoDB" Version="6.0.48" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HMENetCore.MongoDB" Version="6.0.48" />
                    
Directory.Packages.props
<PackageReference Include="HMENetCore.MongoDB" />
                    
Project file
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 HMENetCore.MongoDB --version 6.0.48
                    
#r "nuget: HMENetCore.MongoDB, 6.0.48"
                    
#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 HMENetCore.MongoDB@6.0.48
                    
#: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=HMENetCore.MongoDB&version=6.0.48
                    
Install as a Cake Addin
#tool nuget:?package=HMENetCore.MongoDB&version=6.0.48
                    
Install as a Cake Tool

HMENetCore.MongoDB

简介

HMENetCore.MongoDB 是基于官方MongoDB.Driver的增强封装库,专为.NET Core应用提供更便捷的MongoDB集成方案。支持MongoDB 4.0+版本,提供符合领域驱动设计(DDD)的仓储模式实现。

核心优势

🚀 性能优化

  • 智能连接池管理(默认100-500连接)
  • 批量操作自动优化
  • 异步全链路支持

🔐 企业级安全

  • TLS/SSL加密支持
  • 完善的错误重试机制
  • 事务ACID保证

🧩 开箱即用

  • 自动集合命名映射
  • 内置常用CRUD操作
  • 支持LINQ表达式查询

功能特性

  • 清洁架构:Context → Repository → Service 清晰分层
  • 全异步支持:所有方法均提供Async版本
  • 事务支持:跨操作事务一致性保证
  • 灵活配置:支持多节点集群、SSL连接、连接池调优
  • 智能映射:自动处理集合命名与类型映射

.NET支持

  • 跨平台支持: Supports .NET Standard 2.1, .NET 6, .NET 8, and .NET 9.

安装

dotnet add package HMENetCore.MongoDB --version 6.0.48

配置数据库连接

添加配置到 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 的基本操作接口。

2. BaseMongoRepository

仓储层基类,实现了 IMongoCRUD 接口,提供对特定实体的 CRUD 操作。

3. BaseMongoService

服务层基类,封装了仓储层的操作,可以在此基础上添加业务逻辑。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 is compatible.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.0.48 130 8/11/2025
6.0.47 125 8/11/2025
6.0.46 213 8/7/2025
6.0.45 142 7/16/2025
6.0.42 157 6/18/2025
6.0.40 282 6/10/2025
6.0.39 246 5/15/2025
6.0.37 165 5/7/2025
6.0.36 182 4/9/2025
6.0.33 175 3/17/2025
6.0.32 146 2/21/2025
6.0.30 132 2/19/2025
6.0.25 133 2/6/2025
6.0.23 123 2/5/2025
6.0.22 116 1/14/2025
6.0.21 140 12/31/2024
6.0.16 138 12/27/2024
6.0.12 142 12/9/2024
6.0.11 147 11/26/2024