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
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AzureGems.Repository.CosmosDB" Version="3.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AzureGems.Repository.CosmosDB --version 3.0.1
#r "nuget: AzureGems.Repository.CosmosDB, 3.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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.

  1. Create a new class that inherits from CosmosContext.
  2. Declare a public IRepository<T> property for each model type (aka Domain Entity) with both get and set 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 for IRepository<T>

  1. Register your CosmosContext with the ServiceCollection 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");
		});
});
  1. Register your CosmosContext using the AddCosmosContext() 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
3.0.1 386 2/23/2023
3.0.0 278 2/23/2023
2.1.0 960 2/21/2021
2.0.0 1,083 2/9/2021
1.2.1 3,402 3/13/2020
1.2.0 833 3/13/2020
1.1.0 846 1/22/2020
1.0.0 1,083 1/5/2020