IEvangelist / azure-cosmos-dotnet-repository

Wraps the .NET SDK for Azure Cosmos DB abstracting away the complexity, exposing a simple CRUD-based repository pattern
https://ievangelist.github.io/azure-cosmos-dotnet-repository
MIT License
311 stars 92 forks source link
azure-cosmos-db crud dotnet hacktoberfest repository-pattern sdk

build CodeQL NuGet Nuget .NET code metrics Discord

All Contributors

Azure Cosmos DB Repository .NET SDK

This package wraps the NuGet: Microsoft.Azure.Cosmos package, exposing a simple dependency-injection enabled IRepository<T> interface.

Documentation

The core library IEvangelist.Azure.CosmosRepository and IEvangelist.Azure.CosmosEventSourcing both host there own documentation site. See below:

Cosmos Repository

The repository is responsible for all of the create, read, update, and delete (CRUD) operations on objects where T : Item. The Item type adds several properties, one which is a globally unique identifier defined as:

[JsonProperty("id")]
public string Id { get; set; } = Guid.NewGuid().ToString();

Additionally, a type property exists which indicates the subclass name (this is used for filtering implicitly on your behalf):

[JsonProperty("type")]
public string Type { get; set; }

Finally, a partition key property is used internally to manage partitioning on your behalf. This can optionally be overridden on an item per item basis.

πŸ“£ Azure Cosmos DB - Official Blog

Getting started

  1. Create an Azure Cosmos DB SQL resource.

  2. Obtain the resource connection string from the Keys blade, be sure to get a connection string and not the key - these are different. The connection string is a compound key and endpoint URL.

  3. Call AddCosmosRepository:

    public void ConfigureServices(IServiceCollection services)
    {
       services.AddCosmosRepository();
    }

    The optional setupAction allows consumers to manually configure the RepositoryOptions object:

    public void ConfigureServices(IServiceCollection services)
    {
       services.AddCosmosRepository(
           options =>
           {
               options.CosmosConnectionString = "< connection string >";
               options.ContainerId = "data-store";
               options.DatabaseId = "samples";
           });
    }
  4. Define your object graph, objects must inherit Item, for example:

    using Microsoft.Azure.CosmosRepository;
    
    public class Person : Item
    {
       public string FirstName { get; set; }
       public string LastName { get; set; }
    }
  5. Ask for an instance of IRepository<TItem>, in this case the TItem is Person:

    using Microsoft.Azure.CosmosRepository;
    
    public class Consumer
    {
       readonly IRepository<Person> _repository;
    
       public Consumer(IRepository<Person> repository) =>
           _repository = repository;
    
       // Use the repo...
    }
  6. Perform any of the operations on the _repository instance, create Person records, update them, read them, or delete.

  7. Enjoy!

Samples

Visit the Microsoft.Azure.CosmosRepository.Samples directory for samples on how to use the library with:

Deep-dive video

A deep dive into the Azure Cosmos DB repository pattern NET SDK

Cosmos Repository Crash Course

Discord

Get extra support on our dedicated Discord channel.

alt Join the conversation

Contributors ✨

Thanks goes to these wonderful people (emoji key):

David Pine
David Pine

πŸ’» ⚠️ πŸ’‘ πŸ‘€
Invvard
Invvard

⚠️ πŸ’»
Richard Mercer
Richard Mercer

πŸ’»
Daniel Marbach
Daniel Marbach

πŸ’»
Manuel Sidler
Manuel Sidler

πŸ’»
Dave Brock
Dave Brock

πŸ“– πŸ’»
Cagdas Erman Afacan
Cagdas Erman Afacan

πŸ’» πŸ’‘
dcuccia
dcuccia

πŸ’»
VeryCautious
VeryCautious

πŸ’» ⚠️
Billy Mumby
Billy Mumby

πŸ’» πŸ“– πŸ€”
Michael Zhang
Michael Zhang

πŸ€” πŸ’»
Shay Rojansky
Shay Rojansky

πŸ‘€
Junior Macedo
Junior Macedo

πŸ’» πŸ€”
Emre KARA
Emre KARA

πŸ’»
Brad Westness
Brad Westness

πŸ‘€
Matt Stannett
Matt Stannett

πŸ’» πŸ“– ⚠️
mustafarabie
mustafarabie

πŸ’» ⚠️
Robert Bennett
Robert Bennett

⚠️ πŸ’»
Rabosa616
Rabosa616

πŸ’» ⚠️ πŸ“–
Adam Storr
Adam Storr

πŸ’» ⚠️
Kevin Benton
Kevin Benton

πŸ’» ⚠️
Filip Persson
Filip Persson

πŸ’» ⚠️
A.F.M. Noorullah
A.F.M. Noorullah

πŸ“–
Ion Dormenco
Ion Dormenco

πŸ’»
Martin Oehlert
Martin Oehlert

πŸ’»
Evan Johnson
Evan Johnson

πŸ’» ⚠️
Phil Reed
Phil Reed

⚠️ πŸ’» πŸ“–
dnitsch
dnitsch

πŸ€”
Sean Farrow
Sean Farrow

πŸ€”
stormbringer766
stormbringer766

πŸ’» ⚠️
Joel Turner
Joel Turner

πŸ’»
Ross
Ross

πŸ›
John Belcher
John Belcher

πŸ“–
Victor Marante
Victor Marante

πŸ’»
Mateusz Kumpf
Mateusz Kumpf

πŸ’» ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!