CosmosDbContainerRepository<T>
provides a Cosmos DB specific implementation of the AzureGems.Repository IRepository<T>
interface.
The extension method AddCosmosContext<CosmosContext>()
allows you to define a DbContext (just like EFCore) and have it added to the ServiceCollection
and injected into your services.
Defining your CosmosContext
is straight forward.
- Create a new class that inherits from
CosmosContext
. - Declare a public
IRepository<T>
property for each model type (aka Domain Entity) with bothget
andset
accessors.
public class LittleNorthwindCosmosContext : CosmosContext
{
public IRepository<Customer> Customers { get; set; }
public IRepository<Order> Orders { get; set; }
public IRepository<Invoice> Invoices { get; set; }
}
NOTE: Each model type must inherit from
BaseType
to satisfy the generic constraints forIRepository<T>
- Register your
CosmosContext
with theServiceCollection
which will take care of instantiating it and initializing all the repositories to be backed by the correct Cosmos DB containers.
In
Startup.cs
you should already have added Cosmos DB and specified your Container Configurations. See Adding AzureGems.CosmosDb
- Register your
CosmosContext
using theAddCosmosContext()
extension method:
services.AddCosmosDb(builder =>
{
builder
.WithContainerConfig(c =>
{
c.AddContainer<Vehicle>(containerId: "Cars", partitionKeyPath: "/brand", queryByDiscriminator: false, throughput: 20000);
c.AddContainer<Receipt>(containerId: "Receipts", partitionKeyPath: "/id");
c.AddContainer<Accessory>(containerId: "Accessories", partitionKeyPath: "/category");
});
});
// Add your CosmosContext
services.AddCosmosContext<LittleNorthwindCosmosContext>();
Now your CosmosContext
is ready to be injected and used by your controllers/services.
public class LittleNorthwindService
{
private readonly LittleNorthwindCosmosContext _dbContext;
public LittleNorthwindService(LittleNorthwindCosmosContext dbContext)
{
_dbContext = dbContext;
}
public async Task<Customer> AddCustomer(string firstName, string lastName, string emailAddress)
{
return await _dbContext.Customers.Add(
new Customer()
{
FirstName = firstName,
LastName = lastName,
Email = emailAddress,
});
}
public async Task<Customer> FindCustomer(string firstname, string lastname)
{
return _dbContext.Customers.Query(q => q
.Where(c => c.FirstName == firstname && c.LastName == lastname)
.OrderBy(c => c.FirstName))
.FirstOrDefault();
}
public async Task<IEnumerable<Customer>> GetValuableCustomers(decimal dollarThreshold)
{
return _dbContext.Customers.Query(q => q
.Where(c => c.TotalSpend > dollarThreshold)
.OrderBy(c => c.TotalSpend));
}
// ... you get the idea ... //
}