Oldmansoft.ClassicDomain.Driver.Mongo 5.3.2

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

Oldmansoft.ClassicDomain.Driver.Mongo

Oldmansoft.ClassicDomain 的 MongoDB 驱动。用仓储和工作单元访问 MongoDB:在上下文里登记实体,查询走 LINQ,Commit 时批量写入。

目标框架 netstandard2.0,依赖 MongoDB.Driver 2.30.0。

dotnet add package Oldmansoft.ClassicDomain.Driver.Mongo

连接

在第一次创建上下文之前设置连接字符串。数据库名取路径部分,路径为空时使用 DefaultDatabase。

using Oldmansoft.ClassicDomain;

ConnectionString.Set<Mapping>("mongodb://localhost/Sample");

格式:mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]。详见 MongoDB 连接字符串。

每个上下文类型会缓存一个 MongoClient。连接字符串需要在该上下文第一次使用前设好。

映射

继承 Oldmansoft.ClassicDomain.Driver.Mongo.Context,在 OnModelCreating 里登记实体。主键属性名必须是 Id,文档里写成 _id。集合名默认是类名,可用 ToTable 修改。

using Oldmansoft.ClassicDomain.Driver.Mongo;

public class Mapping : Context
{
    protected override void OnModelCreating()
    {
        Add<Person, Guid>(o => o.Id)
            .ToTable("people")
            .SetUnique(o => o.CreateGroup(x => x.LastName).Add(x => x.FirstName));
    }
}

索引在登记时创建(已存在则跳过):

方法 作用
SetIndex / SetIndexDescending 升序或降序索引,可组合多字段
SetUnique / SetUniqueDescending 唯一索引
SetGeoSpatial 2d 索引
SetGeoSpatialSpherical 2dsphere 索引

主键为 Guid 且值为 Guid.Empty 时,Add 会自动赋值。Guid 按标准 UUID 存储。

仓储

using System.Linq;
using Oldmansoft.ClassicDomain;
using Oldmansoft.ClassicDomain.Driver.Mongo;

public interface IPersonRepository : IRepository<Person, Guid>
{
    Person GetByName(string lastName, string firstName);
}

public class PersonRepository : Repository<Person, Guid, Mapping>, IPersonRepository
{
    public Person GetByName(string lastName, string firstName)
    {
        return Query().FirstOrDefault(o => o.LastName == lastName && o.FirstName == firstName);
    }
}

public class PersonFactory : RepositoryFactory
{
    static PersonFactory()
    {
        Add<IPersonRepository, PersonRepository>();
    }
}

实体只要有名为 Id 的主键。下面是一个最小领域类:

public class Person
{
    public Guid Id { get; private set; }

    public string FirstName { get; private set; }

    public string LastName { get; private set; }

    private Person() { }

    public static Person Create(string firstName, string lastName)
    {
        return new Person { FirstName = firstName, LastName = lastName };
    }

    public void Change(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

写入

Add、Replace、Remove 只登记变更,调用 Commit 后才写入。同一上下文里的写入会合并成一次 BulkWrite。

using (var factory = new PersonFactory())
{
    var repository = factory.GetRepository<IPersonRepository>();
    repository.Add(Person.Create("Ming", "Li"));
    factory.GetUnitOfWork().Commit();
}

更新和删除:

using (var factory = new PersonFactory())
{
    var repository = factory.GetRepository<IPersonRepository>();
    var person = repository.GetByName("Li", "Ming");
    person.Change("Hua", "Li");
    repository.Replace(person);
    factory.GetUnitOfWork().Commit();
}

Replace 的行为:

  • 实体是当前工作单元里 Get 或 Query 加载出来的:按标识映射做差量更新,只写变化的字段。
  • 实体不是这次加载的:整文档 ReplaceOne。

唯一索引冲突(MongoDB 错误码 11000)抛出 UniqueException。

分页从 1 开始:

var page = repository.Query()
    .Paging()
    .OrderBy(o => o.LastName)
    .ThenBy(o => o.FirstName)
    .Size(20)
    .ToList(1, out int totalCount);

直接访问集合

Execute 把操作登记到本次 Commit。带返回值的 Execute 立刻执行。

repository.Execute(collection =>
{
    collection.InsertMany(people);
    return true;
});
factory.GetUnitOfWork().Commit();

子类实体要按父类仓储接口暴露时,继承 RepositoryDefinedSuperClass<TDomain, TSuperDomain, TKey, TContext>。

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 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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  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 (2)

Showing the top 2 NuGet packages that depend on Oldmansoft.ClassicDomain.Driver.Mongo:

Package Downloads
Oldmansoft.ApplicationLibrary.FileStore.Mongo

应用库:文件存储管理,MongoDB 驱动

Oldmansoft.Identity.Driver.Mongo

身份权限 Mongo 存储组件

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.3.2 97 11/8/2026
5.2.1 2,446 3/28/2021
4.0.15 1,843 4/11/2019