CosmosKit 5.1.0
See the version list below for details.
dotnet add package CosmosKit --version 5.1.0
NuGet\Install-Package CosmosKit -Version 5.1.0
<PackageReference Include="CosmosKit" Version="5.1.0" />
<PackageVersion Include="CosmosKit" Version="5.1.0" />
<PackageReference Include="CosmosKit" />
paket add CosmosKit --version 5.1.0
#r "nuget: CosmosKit, 5.1.0"
#:package CosmosKit@5.1.0
#addin nuget:?package=CosmosKit&version=5.1.0
#tool nuget:?package=CosmosKit&version=5.1.0
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
- Register
CosmosClient
in your service container. - 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 registeredCosmosSerializer
:
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 theId
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 | Versions 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. |
-
net8.0
- Microsoft.Azure.Cosmos (>= 3.51.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.5)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.5)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.