Soenneker.Cosmos.Repository 4.0.8166

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

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Soenneker.Cosmos.Repository

An extensible Azure Cosmos DB repository base with point reads, queries, paging, queued and parallel writes, ETag concurrency, patching, bulk deletion, and audit records.

Installation

dotnet add package Soenneker.Cosmos.Repository

Define a repository

The package provides an abstract base rather than a registrar. Documents must derive from Soenneker.Documents.Document and provide the DocumentId, PartitionKey, and other base fields expected by the repository.

public interface IOrderRepository : ICosmosRepository<OrderDocument>
{
}

public sealed class OrderRepository : CosmosRepository<OrderDocument>, IOrderRepository
{
    public override string ContainerName => "orders";

    public OrderRepository(
        ICosmosContainerUtil containerUtil,
        IConfiguration configuration,
        ILogger<CosmosRepository<OrderDocument>> logger,
        IUserContext userContext,
        IBackgroundQueue backgroundQueue,
        IMemoryStreamUtil memoryStreamUtil)
        : base(containerUtil, configuration, logger, userContext, backgroundQueue, memoryStreamUtil)
    {
    }
}

Register the derived repository as scoped when using the scoped user-context implementation:

services.AddScoped<IOrderRepository, OrderRepository>();

Register the constructor dependencies separately. Soenneker.Cosmos.Container supplies the container utility and its Azure:Cosmos configuration contract.

IDs and partition keys

Single-string overloads accept a full ID in partitionKey:documentId form. When the partition key and document ID are the same, one value is sufficient. Two-string overloads consistently take documentId first and partitionKey second.

OrderDocument? order = await orders.GetItem("tenant-42:order-100", cancellationToken);

bool exists = await orders.Exists(
    documentId: "order-100",
    partitionKey: "tenant-42",
    cancellationToken: cancellationToken);

Override ResolvePartitionKey if a derived repository uses a different full-ID convention.

Writes and optimistic concurrency

string fullId = await orders.AddItem(order, cancellationToken: cancellationToken);

CosmosItem<OrderDocument>? current = await orders.GetItemWithETag(fullId, cancellationToken);
if (current is not null)
{
    current.Document.Status = "shipped";
    CosmosItem<OrderDocument> updated = await orders.UpdateItemIfMatch(current, cancellationToken);
}

Conditional update, patch, and delete methods send the supplied ETag through Cosmos If-Match. A concurrent change results in the Cosmos 412 Precondition Failed error. MutateItem wraps this pattern with retries; its mutation delegate can run more than once and must not perform external side effects.

AddItem uses create semantics and fails when the addressed item already exists. Updates use replace semantics. Patch operations are sent directly to Cosmos.

Queued and parallel work

Methods with useQueue: true return after the work is accepted by the configured background queue, not after Cosmos completes it. A queued patch returns null, and response-excluding writes return the caller's document because Cosmos does not send the updated resource body. Observe the background queue for execution failures and drain it during graceful shutdown.

Parallel methods perform direct Cosmos operations with bounded concurrency. Failures propagate to the caller; successful operations completed before a failure are not rolled back.

Queries and paging

Prefer QueryDefinition with parameters for dynamic values:

var query = new QueryDefinition(
        "SELECT * FROM c WHERE c.partitionKey = @partitionKey ORDER BY c.createdAt DESC")
    .WithParameter("@partitionKey", "tenant-42");

(List<OrderDocument> page, string? next) = await orders.GetItemsPaged(
    query,
    pageSize: 50,
    continuationToken: null,
    cancellationToken: cancellationToken);

Pass the returned continuation token unchanged to request the next page. Use a deterministic ORDER BY when results must remain stable between pages. Collection-returning query methods drain every page into memory; use paged or callback-based methods for large result sets.

GetItem, GetItemWithETag, and delete operations treat 404 Not Found as an absent or already-deleted item where documented. Authentication, throttling, service, query, and cancellation failures propagate rather than being reported as “not found.”

Auditing

AuditEnabled defaults to true. After successful creates, updates, patches, and actual deletes, the repository queues an AuditDocument in the audits container using the target document ID as its audit partition key. Override AuditEnabled to disable this for a repository, including the audit repository itself.

Primary writes and audit writes are not transactional. A primary write can succeed before audit enqueueing or execution fails. Queued write auditing captures the serialized document at enqueue time.

Logging

{
  "Azure": {
    "Cosmos": {
      "Log": false,
      "AuditLog": false
    }
  }
}

These optional flags enable diagnostic operation and audit metadata logs. Document bodies, audit payloads, query parameter values, and continuation tokens are not logged.

Deletion behavior

Delete-all and query-delete operations are permanent and non-transactional across the complete result set. Transactional batch deletion groups documents by partition key, limits each Cosmos batch to 100 operations, and now fails the call when Cosmos rejects a batch. Time-range deletion expects a queryable createdAt field.

Queued deletion means “enqueued,” while direct and parallel deletion means Cosmos acknowledged the operation. Cancellation or failure does not undo completed or queued deletes.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 (3)

Showing the top 3 NuGet packages that depend on Soenneker.Cosmos.Repository:

Package Downloads
Soenneker.Managers.Entities

An abstract generic manager class provides CRUD operations for entities mapped to Cosmos DB documents

Soenneker.Cosmos.Repositories.Audits

A data persistence abstraction layer for Cosmos DB Audit type documents

Soenneker.Cosmos.Repositories.Shared

A data persistence abstraction layer for Cosmos DB containers that have multiple document types

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.8171 0 8/30/2026
4.0.8166 0 8/30/2026
4.0.8163 0 8/30/2026
4.0.8161 0 8/30/2026
4.0.8160 0 8/30/2026
4.0.8156 0 8/29/2026
4.0.8151 22 8/29/2026
4.0.8150 29 8/29/2026
4.0.8149 28 8/29/2026
4.0.8145 123 8/27/2026
4.0.8144 132 8/26/2026
4.0.8143 99 8/26/2026
4.0.8142 94 8/26/2026
4.0.8141 111 8/26/2026
4.0.8140 116 8/25/2026
4.0.8139 71 8/25/2026
4.0.8136 180 8/22/2026
4.0.8135 155 8/22/2026
4.0.8134 131 8/22/2026
4.0.8133 129 8/22/2026
Loading failed

Update dependency Soenneker.Utils.UserContext to 4.0.1776 (#9055)