AzureGems.Repository.CosmosDB
3.0.1
dotnet add package AzureGems.Repository.CosmosDB --version 3.0.1
NuGet\Install-Package AzureGems.Repository.CosmosDB -Version 3.0.1
<PackageReference Include="AzureGems.Repository.CosmosDB" Version="3.0.1" />
paket add AzureGems.Repository.CosmosDB --version 3.0.1
#r "nuget: AzureGems.Repository.CosmosDB, 3.0.1"
// Install AzureGems.Repository.CosmosDB as a Cake Addin #addin nuget:?package=AzureGems.Repository.CosmosDB&version=3.0.1 // Install AzureGems.Repository.CosmosDB as a Cake Tool #tool nuget:?package=AzureGems.Repository.CosmosDB&version=3.0.1
AzureGems Repository - Cosmos DB
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.
Getting Started
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
Example of adding AzureGems.CosmosDb:
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");
});
});
- Register your
CosmosContext
using theAddCosmosContext()
extension method:
// Add your CosmosContext
services.AddCosmosContext<LittleNorthwindCosmosContext>();
Now your CosmosContext
is ready to be injected and used by your controllers/services.
Example Usage
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 ... //
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net7.0
- AzureGems.CosmosDB (>= 3.0.1)
- AzureGems.Repository.Abstractions (>= 3.0.1)
- Microsoft.Azure.Cosmos (>= 3.32.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on AzureGems.Repository.CosmosDB:
Package | Downloads |
---|---|
AzureGems.SpendOps.CosmosDB
SpendOps extensions for AzureGems Cosmos DB. |
GitHub repositories
This package is not used by any popular GitHub repositories.