Cosmos Repository

IEvangelist.Azure.CosmosRepository · MIT licensed

Azure Cosmos DB,
the repository way.

A dependency-injection-friendly IRepository<T> wrapper around the official Microsoft.Azure.Cosmos SDK. Configure once, inject everywhere, ship faster.

$ dotnet add package IEvangelist.Azure.CosmosRepository

Three steps from dotnet add to running

Install the package, register the services, and inject IRepository<T> wherever you need data access.

  1. Step 1

    Wire it up once

    Call AddCosmosRepository on your IServiceCollection. One call configures the Cosmos client, options, and registers IRepository<T> for every item you define.

    Register the repository
    using Microsoft.Azure.CosmosRepository;
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddCosmosRepository(options =>
    {
    options.CosmosConnectionString = "<connection string>";
    options.DatabaseId = "samples";
    options.ContainerId = "data-store";
    });
  2. Step 2

    Model your data

    Inherit from Item (or EtagItem, TimeToLiveItem, FullItem). The base type owns the id, type discriminator, and partition key so your class stays focused on the domain.

    Define an item
    using Microsoft.Azure.CosmosRepository;
    public class Person : Item
    {
    public string FirstName { get; set; } = null!;
    public string LastName { get; set; } = null!;
    }
  3. Step 3

    Use it anywhere

    Inject IRepository<Person> into your services, endpoints, or background workers. Predicates, batching, paging, and the Specification pattern are all built in.

    Inject and use
    public class PeopleService(IRepository<Person> repository)
    {
    public Task<Person> CreateAsync(Person person) =>
    repository.CreateAsync(person).AsTask();
    public Task<IEnumerable<Person>> GetWithoutMiddleNamesAsync() =>
    repository.GetAsync(p => p.MiddleName == null).AsTask();
    }

Want more? Browse the full docs or jump straight to Getting started.