This package wraps the NuGet: Microsoft.Azure.Cosmos package,
exposing a simple dependency-injection enabled IRepository<T>
interface.
The core library IEvangelist.Azure.CosmosRepository
and IEvangelist.Azure.CosmosEventSourcing
both host there own documentation site. See below:
IEvangelist.Azure.CosmosRepository Documentation
IEvangelist.Azure.CosmosEventSourcing Documentation
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
Create an Azure Cosmos DB SQL resource.
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.
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";
});
}
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; }
}
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...
}
Perform any of the operations on the _repository
instance, create Person
records, update them, read them, or delete.
Enjoy!
Visit the Microsoft.Azure.CosmosRepository.Samples
directory for samples on how to use the library with:
Get extra support on our dedicated Discord channel.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!