CosmosKit 5.1.4

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

CosmosKit supports both direct repository usage and transactional operations via IUnitOfWork.

🔹 Using Repository (no transaction)

Use the repository directly if you don't need transaction support. This is efficient for most single-entity operations:

public class CustomerService
{
    private readonly IRepository<Customer> _repo;

    public CustomerService(IRepository<Customer> repo)
    {
        _repo = repo;
    }

    public async Task CreateCustomerAsync(Customer customer, CancellationToken ct)
    {
        await _repo.AddAsync(customer, ct);
    }
}

🔹 Using IUnitOfWork (transactional batch)

IUnitOfWork enables grouping operations into a transactional batch within the same container and partition key. This is especially useful when consistency is critical:

⚠️ Important: Cosmos DB transactional batch only works when all operations target the same container and the same partition key.

If you add entities to different containers or with different partition keys, CosmosKit will apply independent transactions per container/partition key combination.

public class OrderService
{
    private readonly IUnitOfWork _uow;

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

    public async Task PlaceOrderAsync(Order order, Customer customer, CancellationToken ct)
    {
        await _unitOfWork.BeginTransactionAsync();

        var orderRepo = _uow.GetRepository<Order>();
        var customerRepo = _uow.GetRepository<Customer>();

        await orderRepo.AddAsync(order, ct);
        await customerRepo.AddAsync(customer, ct);

        await _unitOfWork.CommitTransactionAsync();
    }
}

GetRepository returns a repository proxy that automatically participates in the unit of work transaction, if one is started.

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