XYS.Mongo 3.0.0

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

XYS.Mongo

基于 MongoDB.Driver 的通用数据访问库:泛型仓储 + 上下文工厂,异步优先。

特性

  • MongoContext 入口:按连接串缓存 IMongoClient(官方推荐的单例复用),分发集合/仓储/索引管理器;FromConfigxys.config.json
  • MongoRepository<T> 泛型仓储:CRUD 同步 + 异步(异步带 CancellationToken),消费者可继承扩展。
  • MongoRepositoryManager<T> 索引管理:EnsureIndex/DropIndex/IndexExists 等,同步 + 异步。
  • 驱动异常原样冒泡(不吞不包装);仅配置错误抛 MongoConfigurationException
  • 多目标:net8.0 / net462 / netcoreapp3.1 / netstandard2.0 / netstandard2.1。

快速开始

using XYS.Mongo;
using XYS.Mongo.Repository;

// 1. 从 xys.config.json 的 connectionStrings 节点构造
var ctx = MongoContext.FromConfig("sms");

// 2. 取仓储
var repo = ctx.Repository<SmsSendME>();

// 3. 异步 CRUD
await repo.AddAsync(new SmsSendME { /* ... */ });
var list = await repo.FindMultipleAsync(repo.FilterBuiler.Eq(x => x.Status, 0));
long n = await repo.CountAsync();

继承用法(保留)

public class SmsSendReqRepository : MongoRepository<SmsSendME>
{
    // 经 MongoContext 获取实例,或沿用既有静态单例
}

实体与集合名

  • 实体实现 IEntity<TKey> 或继承 BaseME(字符串 ObjectId 主键)。
  • 集合名解析顺序:[CollectionName("...")] 特性 → BaseME 继承链顶层类名 → 类型名。

配置(xys.config.json)

"connectionStrings": {
  "sms": {
    "connectionString": "mongodb://user:pwd@host:27017/smsdb"
  }
}

连接串含库名即可;库名缺失时用 MongoOptions.DatabaseName 覆盖。仓库内 xys.config.json 不写明文凭据。

索引管理

var mgr = ctx.Manager<SmsSendME>();
mgr.EnsureIndex("Status");
bool ok = mgr.IndexExists("Status");

3.0.0 变更(不兼容点)

  • 新增 MongoContext 工厂层,按连接串缓存 MongoClient(修复旧版每仓储实例新建 client 的反模式)。
  • 修复必崩 Bug:Count()/Count(predicate) 自递归爆栈、DeleteAsync(TKey) 假异步、DeleteAllAsync()async void
  • Delete*Async 返回类型 void → Task
  • IRepository<T,TKey> 去掉 IQueryable<T> 基接口,改为 AsQueryable() 返回 IMongoQueryable<T>
  • 删除面向 MMAPv1 的 MongoRepositoryManager 统计/校验(GetStats/Validate/ReIndex/GetTotalDataSize/GetTotalStorageSize/IsCapped)——在 WiredTiger 上本就抛异常。
  • MongoRepository 构造函数标 [Obsolete](仍可用,内部改走缓存 client)。
Product 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 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 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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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 (4)

Showing the top 4 NuGet packages that depend on XYS.Mongo:

Package Downloads
XYS.Lab.Mongo

Package Description

XYS.His.Hos.Mongo

Package Description

XYS.Message.Mongo

Package Description

XYS.Emr.Mongo

Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0 112 7/23/2026
2.1.0.2 811 1/12/2023
2.1.0.1 546 10/16/2022
2.0.2.2 599 12/20/2021
2.0.1.3 5,031 5/28/2021
2.0.1.2 1,771 4/25/2021
2.0.1.1 550 4/22/2021
2.0.0.1 517 4/22/2021
1.0.0.10 1,093 5/27/2020