CosmosKit 5.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CosmosKit --version 5.1.0
                    
NuGet\Install-Package CosmosKit -Version 5.1.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="CosmosKit" Version="5.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CosmosKit" Version="5.1.0" />
                    
Directory.Packages.props
<PackageReference Include="CosmosKit" />
                    
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 CosmosKit --version 5.1.0
                    
#r "nuget: CosmosKit, 5.1.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 CosmosKit@5.1.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=CosmosKit&version=5.1.0
                    
Install as a Cake Addin
#tool nuget:?package=CosmosKit&version=5.1.0
                    
Install as a Cake Tool

CosmosKit

CosmosKit provides Repository and Unit of Work patterns on top of the Azure Cosmos DB SDK. It simplifies data access by encapsulating common operations and transactional batch logic.

Installation

Add a reference to the package (once published) or include the project in your solution.

<PackageReference Include="CosmosKit" Version="*" />

Configuration

  1. Register CosmosClient in your service container.
  2. Call AddCosmosKit specifying the database id and your entity containers.
builder.Services.AddSingleton(new CosmosClient(connectionString));

builder.AddCosmosKit(
    databaseId: "AppDb",
    new []
    {
        new DependencyInjection.EntityContainer(typeof(Order), "orders", "TenantId"),
        new DependencyInjection.EntityContainer(typeof(Customer), "customers", "TenantId")
    });

🧩 Optional: Custom JSON Serialization

You can enable System.Text.Json support by using the overload that accepts a JsonSerializerOptions configuration:

builder.AddCosmosKit(
    databaseId: "AppDb",
    entityContainers: new[]
    {
        new DependencyInjection.EntityContainer(typeof(Order), "orders", "TenantId")
    },
    configureJson: options =>
    {
        options.TypeInfoResolver = MyAppJsonContext.Default;
        options.Converters.Add(new MyPolymorphicConverter());
        options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    });

⚠️ Important: When using this overload, you must also ensure that the CosmosClient is constructed with the registered CosmosSerializer:

builder.Services.AddSingleton(sp =>
{
    var serializer = sp.GetRequiredService<CosmosSerializer>();

    return new CosmosClient(connectionString, new CosmosClientOptions
    {
        Serializer = serializer,
        ApplicationName = "MyApp"
    });
});

✅ Entity Model Requirements

🔑 Cosmos DB requires the id field to be lowercase in JSON.

If you're using System.Text.Json (recommended):

using System.Text.Json.Serialization;

public abstract class EntityBase
{
    [JsonPropertyName("id")] // Required by Cosmos DB
    public string Id { get; set; } = default!;
}

⚠️ If you're using Newtonsoft.Json, override the Id property in your entity and annotate it like this:

using Newtonsoft.Json;

public class MyEntity : EntityBase
{
    [JsonProperty("id")]
    public new string Id { get; set; } = default!;
}

Usage

Obtain an IUnitOfWork from dependency injection and use repositories to perform operations. Use transactions when you need to group multiple operations within the same partition key.

public class OrderService
{
    private readonly IUnitOfWork _uow;

    public OrderService(IUnitOfWork uow)
    {
        _uow = uow;
    }

    public async Task CreateOrderAsync(Order order, CancellationToken ct)
    {
        var repo = _uow.GetRepository<Order>();
        await repo.AddAsync(order, ct);
    }
}

GetRepository returns a repository proxy that automatically participates in transactions started on the unit of work.

Streaming Queries

IRepository exposes StreamAsync to asynchronously enumerate large query results without buffering:

await foreach(var order in repo.StreamAsync(o => o.Status == "new", ct))
{
    // process order
}

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET 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. 
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
5.1.6 283 6/9/2025
5.1.5 95 6/6/2025
5.1.4 92 6/6/2025
5.1.3 118 6/6/2025
5.1.2 118 6/6/2025
5.1.1 116 6/6/2025
5.1.0 119 6/6/2025
5.0.1 118 6/6/2025