Skip to main content

arolariu.Backend.Domain.Invoices.Brokers.DataBrokers.DatabaseBroker

arolariu.Backend.Domain.Invoices

arolariu.Backend.Domain.Invoices.Brokers.DataBrokers.DatabaseBroker Namespace

Classes

InvoiceNoSqlBroker Class

Entity Framework Core (Cosmos provider) context implementing the IInvoiceNoSqlBroker contract for invoice and merchant aggregates.

public sealed class InvoiceNoSqlBroker : Microsoft.EntityFrameworkCore.DbContext, arolariu.Backend.Domain.Invoices.Brokers.DatabaseBroker.IInvoiceNoSqlBroker

Inheritance System.Object 🡒 Microsoft.EntityFrameworkCore.DbContext 🡒 InvoiceNoSqlBroker

Implements IInvoiceNoSqlBroker

Remarks

Responsibilities: Configures entity-to-container mappings, JSON property names, value conversions for strongly typed / value object members, owned collections, and partition key assignments. Performs no domain validation or business rule enforcement.

Containers: Invoices mapped to invoices (partitioned by UserIdentifier); Merchants mapped to merchants (partitioned by ParentCompanyId).

Owned Types:Items (products), PossibleRecipes, and PaymentInformation configured as owned to ensure embedded document structure in Cosmos JSON.

Soft Delete: Relies on IsSoftDeleted flags (invoice and product metadata) — filtering is applied by higher layer query logic; the context does not automatically filter them out.

Performance: Explicit JSON property names and conversions reduce implicit reflection cost and ensure stable persisted schema.

Thread-safety: Inherits EF Core DbContext non-thread-safe semantics. Scope per logical unit-of-work.

Constructors

InvoiceNoSqlBroker(CosmosClient, DbContextOptions<InvoiceNoSqlBroker>) Constructor

Initializes the broker DbContext with a pre-configured Cosmos DB client and EF Core options.

public InvoiceNoSqlBroker(Microsoft.Azure.Cosmos.CosmosClient client, Microsoft.EntityFrameworkCore.DbContextOptions<arolariu.Backend.Domain.Invoices.Brokers.DataBrokers.DatabaseBroker.InvoiceNoSqlBroker> options);

Parameters

client Microsoft.Azure.Cosmos.CosmosClient

Shared CosmosClient instance (pooled / singleton at composition root).

options Microsoft.EntityFrameworkCore.DbContextOptions<InvoiceNoSqlBroker>

EF Core options including provider configuration (database name, connection mode).

Exceptions

System.ArgumentNullException
Thrown when client or options is null.

Remarks

Does not open connections eagerly; defers to EF Core lazy initialization. Ensures required dependencies are non-null.

Diagnostics: Upstream configuration may attach logging / tracing interceptors; this constructor performs no instrumentation itself.

Properties

InvoiceNoSqlBroker.CosmosClient Property

Underlying Azure Cosmos DB client used for low-level container operations (point reads, queries outside EF tracking pipeline in partial implementations).

private Microsoft.Azure.Cosmos.CosmosClient CosmosClient { private get; }

Property Value

Microsoft.Azure.Cosmos.CosmosClient

Remarks

Injected externally to allow pooling and centralized configuration (retry policies, diagnostics).

Methods

InvoiceNoSqlBroker.CreateInvoiceAsync(Invoice, CancellationToken) Method

Persists a new invoice document.

public System.Threading.Tasks.ValueTask<arolariu.Backend.Domain.Invoices.DDD.AggregatorRoots.Invoices.Invoice> CreateInvoiceAsync(arolariu.Backend.Domain.Invoices.DDD.AggregatorRoots.Invoices.Invoice invoice, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

invoice Invoice

Fully populated invoice aggregate (identity may be reassigned by upstream factory prior to call).

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements CreateInvoiceAsync(Invoice, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<Invoice>
The persisted invoice including any storage-generated metadata.

Exceptions

System.ArgumentNullException
Thrown if invoice is null.

System.OperationCanceledException
Thrown if the operation is cancelled.

Remarks

Asynchronous I/O-bound single write operation. Assumes the invoice aggregate has been fully validated upstream.

InvoiceNoSqlBroker.CreateMerchantAsync(Merchant, CancellationToken) Method

Persists a new merchant entity.

public System.Threading.Tasks.ValueTask<arolariu.Backend.Domain.Invoices.DDD.Entities.Merchants.Merchant> CreateMerchantAsync(arolariu.Backend.Domain.Invoices.DDD.Entities.Merchants.Merchant merchant, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

merchant Merchant

Merchant entity to persist.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements CreateMerchantAsync(Merchant, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<Merchant>
The persisted merchant.

Exceptions

System.ArgumentNullException
Thrown if merchant is null.

System.OperationCanceledException
Thrown if the operation is cancelled.

Remarks

Performs a single write operation. Caller must ensure uniqueness and upstream validation of fields.

InvoiceNoSqlBroker.DeleteInvoiceAsync(Guid, Nullable<Guid>, CancellationToken) Method

Soft-deletes an invoice by identifier within a known partition.

public System.Threading.Tasks.ValueTask DeleteInvoiceAsync(System.Guid invoiceIdentifier, System.Nullable<System.Guid> userIdentifier=null, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

invoiceIdentifier System.Guid

Invoice identity.

userIdentifier System.Nullable<System.Guid>

Partition (owner) identity.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements DeleteInvoiceAsync(Guid, Nullable<Guid>, CancellationToken)

Returns

System.Threading.Tasks.ValueTask

Exceptions

System.OperationCanceledException
Thrown if the operation is cancelled.

Remarks

More efficient than cross-partition delete variant. Marks invoice and contained products as soft-deleted.

InvoiceNoSqlBroker.DeleteInvoicesAsync(Guid, CancellationToken) Method

Soft-deletes all invoices for a given user partition.

public System.Threading.Tasks.ValueTask DeleteInvoicesAsync(System.Guid userIdentifier, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

userIdentifier System.Guid

Partition (owner) identity whose invoices will be soft-deleted.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements DeleteInvoicesAsync(Guid, CancellationToken)

Returns

System.Threading.Tasks.ValueTask

Exceptions

System.OperationCanceledException
Thrown if the operation is cancelled.

Remarks

Iterates all partition documents; may incur significant RU charges for large partitions.

InvoiceNoSqlBroker.DeleteMerchantAsync(Guid, Nullable<Guid>, CancellationToken) Method

Soft-deletes (or physically replaces) a merchant by identifier via cross-partition search.

public System.Threading.Tasks.ValueTask DeleteMerchantAsync(System.Guid merchantIdentifier, System.Nullable<System.Guid> parentCompanyId=null, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

merchantIdentifier System.Guid

Merchant identity.

parentCompanyId System.Nullable<System.Guid>

The parent company identifier.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements DeleteMerchantAsync(Guid, Nullable<Guid>, CancellationToken)

Returns

System.Threading.Tasks.ValueTask

Exceptions

System.OperationCanceledException
Thrown if the operation is cancelled.

Remarks

Current implementation performs a greedy query. If soft-delete is later introduced for merchants, implementation should mark a flag rather than remove.

InvoiceNoSqlBroker.OnModelCreating(ModelBuilder) Method

Override this method to further configure the model that was discovered by convention from the entity types exposed in Microsoft.EntityFrameworkCore.DbSet<> properties on your derived context. The resulting model may be cached and re-used for subsequent instances of your derived context.

protected override void OnModelCreating(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder);

Parameters

modelBuilder Microsoft.EntityFrameworkCore.ModelBuilder

The builder being used to construct the model for this context. Databases (and other extensions) typically define extension methods on this object that allow you to configure aspects of the model that are specific to a given database.

Remarks

If a model is explicitly set on the options for this context (via Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.UseModel(Microsoft.EntityFrameworkCore.Metadata.IModel)) then this method will not be run. However, it will still run when creating a compiled model.

See Modeling entity types and relationships for more information and examples.

InvoiceNoSqlBroker.ReadInvoiceAsync(Guid, Nullable<Guid>, CancellationToken) Method

Retrieves a single invoice by identifier using either a single point read or a cross-partition (greedy) lookup.

public System.Threading.Tasks.ValueTask<arolariu.Backend.Domain.Invoices.DDD.AggregatorRoots.Invoices.Invoice?> ReadInvoiceAsync(System.Guid invoiceIdentifier, System.Nullable<System.Guid> userIdentifier=null, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

invoiceIdentifier System.Guid

Invoice aggregate identity (GUID).

userIdentifier System.Nullable<System.Guid>

Optional partition key for efficient point read.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements ReadInvoiceAsync(Guid, Nullable<Guid>, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<Invoice>
The matching invoice or null.

Exceptions

System.OperationCanceledException
Thrown if the operation is cancelled.

Remarks

Performs a non-partition-scoped query (higher RU cost). Use the partition-aware overload when the user / owner identifier is available.

Returns null when not found or soft-deleted.

InvoiceNoSqlBroker.ReadInvoicesAsync(Guid, CancellationToken) Method

Lists all non soft-deleted invoices for a specific user/partition.

public System.Threading.Tasks.ValueTask<System.Collections.Generic.IEnumerable<arolariu.Backend.Domain.Invoices.DDD.AggregatorRoots.Invoices.Invoice>> ReadInvoicesAsync(System.Guid userIdentifier, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

userIdentifier System.Guid

Partition key / owner identity.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements ReadInvoicesAsync(Guid, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<System.Collections.Generic.IEnumerable<Invoice>>
An enumerable of invoices (may be empty).

Exceptions

System.OperationCanceledException
Thrown if the operation is cancelled.

InvoiceNoSqlBroker.ReadMerchantAsync(Guid, Nullable<Guid>, CancellationToken) Method

Retrieves a merchant by identifier via cross-partition (greedy) search.

public System.Threading.Tasks.ValueTask<arolariu.Backend.Domain.Invoices.DDD.Entities.Merchants.Merchant?> ReadMerchantAsync(System.Guid merchantIdentifier, System.Nullable<System.Guid> parentCompanyId=null, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

merchantIdentifier System.Guid

Merchant identity.

parentCompanyId System.Nullable<System.Guid>

The parent company identifier.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements ReadMerchantAsync(Guid, Nullable<Guid>, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<Merchant>
The merchant or null if not found or soft-deleted (if soft-delete introduced later).

Exceptions

System.OperationCanceledException
Thrown if the operation is cancelled.

Remarks

Use partition-aware read patterns (parent company id) where possible to reduce RU consumption.

InvoiceNoSqlBroker.ReadMerchantsAsync(Guid, CancellationToken) Method

Lists merchants filtered by parent company partition.

public System.Threading.Tasks.ValueTask<System.Collections.Generic.IEnumerable<arolariu.Backend.Domain.Invoices.DDD.Entities.Merchants.Merchant>> ReadMerchantsAsync(System.Guid parentCompanyId, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

parentCompanyId System.Guid

Partition key representing the parent company identifier.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements ReadMerchantsAsync(Guid, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<System.Collections.Generic.IEnumerable<Merchant>>
An enumerable of merchants (may be empty).

Exceptions

System.OperationCanceledException
Thrown if the operation is cancelled.

InvoiceNoSqlBroker.SetModelReferences(ModelBuilder) Method

Orchestrates model configuration for all aggregates/entities in this broker context.

private static void SetModelReferences(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder);

Parameters

modelBuilder Microsoft.EntityFrameworkCore.ModelBuilder

The mutable model builder.

Remarks

Delegates to specialized configuration methods to maintain separation of concerns and reduce method length.

InvoiceNoSqlBroker.SetModelReferencesForInvoiceModel(ModelBuilder) Method

Configures the Invoice aggregate mapping for the Cosmos provider.

private static void SetModelReferencesForInvoiceModel(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder);

Parameters

modelBuilder Microsoft.EntityFrameworkCore.ModelBuilder

The mutable model builder.

Remarks

Defines container name, partition key (UserIdentifier), JSON property naming, value conversions (including enumerable converters for collection serialization), indices and owned navigations (products, recipes, payment information).

Design Notes:HasNoDiscriminator() used to avoid adding a synthetic type field as only a single aggregate type resides in the container.

InvoiceNoSqlBroker.SetModelReferencesForMerchantModel(ModelBuilder) Method

Configures the Merchant entity mapping for the Cosmos provider.

private static void SetModelReferencesForMerchantModel(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder);

Parameters

modelBuilder Microsoft.EntityFrameworkCore.ModelBuilder

The mutable model builder.

Remarks

Defines container name, partition key (ParentCompanyId), JSON property conversions and indexing strategy on id.

Currently no owned sub-collections. Soft delete flag present at entity level for parity with invoices (future enablement).

InvoiceNoSqlBroker.TranslateMerchantCosmos(CosmosException, Nullable<Guid>) Method

Maps a Microsoft.Azure.Cosmos.CosmosException status code to the corresponding merchant inner exception type.

private static System.Exception TranslateMerchantCosmos(Microsoft.Azure.Cosmos.CosmosException cosmosException, System.Nullable<System.Guid> merchantIdentifier);

Parameters

cosmosException Microsoft.Azure.Cosmos.CosmosException

merchantIdentifier System.Nullable<System.Guid>

Returns

System.Exception

InvoiceNoSqlBroker.TranslateMerchantCosmosAsync(Func<Task>, Nullable<Guid>) Method

Executes a Cosmos operation with no return value and maps any Microsoft.Azure.Cosmos.CosmosException onto a typed merchant inner exception.

private static System.Threading.Tasks.Task TranslateMerchantCosmosAsync(System.Func<System.Threading.Tasks.Task> operation, System.Nullable<System.Guid> merchantIdentifier);

Parameters

operation System.Func<System.Threading.Tasks.Task>

merchantIdentifier System.Nullable<System.Guid>

Returns

System.Threading.Tasks.Task

InvoiceNoSqlBroker.TranslateMerchantCosmosAsync<T>(Func<Task<T>>, Nullable<Guid>) Method

Executes a Cosmos operation returning T and maps any Microsoft.Azure.Cosmos.CosmosException onto a typed merchant inner exception.

private static System.Threading.Tasks.Task<T> TranslateMerchantCosmosAsync<T>(System.Func<System.Threading.Tasks.Task<T>> operation, System.Nullable<System.Guid> merchantIdentifier);

Type parameters

T

Parameters

operation System.Func<System.Threading.Tasks.Task<T>>

merchantIdentifier System.Nullable<System.Guid>

Returns

System.Threading.Tasks.Task<T>

InvoiceNoSqlBroker.UpdateInvoiceAsync(Invoice, Invoice, CancellationToken) Method

Replaces (upserts) an invoice using current and updated aggregate snapshots.

public System.Threading.Tasks.ValueTask<arolariu.Backend.Domain.Invoices.DDD.AggregatorRoots.Invoices.Invoice> UpdateInvoiceAsync(arolariu.Backend.Domain.Invoices.DDD.AggregatorRoots.Invoices.Invoice currentInvoice, arolariu.Backend.Domain.Invoices.DDD.AggregatorRoots.Invoices.Invoice updatedInvoice, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

currentInvoice Invoice

Current persisted invoice snapshot (not validated here).

updatedInvoice Invoice

Updated invoice aggregate snapshot.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements UpdateInvoiceAsync(Invoice, Invoice, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<Invoice>
The persisted invoice after update.

Exceptions

System.ArgumentNullException
Thrown if currentInvoice or updatedInvoice is null.

System.OperationCanceledException
Thrown if the operation is cancelled.

Remarks

currentInvoice is not modified; only updatedInvoice is persisted. No optimistic concurrency token applied.

InvoiceNoSqlBroker.UpdateInvoiceAsync(Guid, Invoice, CancellationToken) Method

Replaces (upserts) an invoice by identifier (partition inferred from updatedInvoice).

public System.Threading.Tasks.ValueTask<arolariu.Backend.Domain.Invoices.DDD.AggregatorRoots.Invoices.Invoice> UpdateInvoiceAsync(System.Guid invoiceIdentifier, arolariu.Backend.Domain.Invoices.DDD.AggregatorRoots.Invoices.Invoice updatedInvoice, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

invoiceIdentifier System.Guid

Target invoice identity.

updatedInvoice Invoice

Updated invoice aggregate snapshot.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements UpdateInvoiceAsync(Guid, Invoice, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<Invoice>
The persisted invoice after update.

Exceptions

System.ArgumentNullException
Thrown if updatedInvoice is null.

System.OperationCanceledException
Thrown if the operation is cancelled.

Remarks

Assumes updatedInvoice carries the correct UserIdentifier. Does not perform concurrency (ETag) checks.

InvoiceNoSqlBroker.UpdateMerchantAsync(Merchant, Merchant, CancellationToken) Method

Replaces (upserts) a merchant using its current and updated snapshots.

public System.Threading.Tasks.ValueTask<arolariu.Backend.Domain.Invoices.DDD.Entities.Merchants.Merchant> UpdateMerchantAsync(arolariu.Backend.Domain.Invoices.DDD.Entities.Merchants.Merchant currentMerchant, arolariu.Backend.Domain.Invoices.DDD.Entities.Merchants.Merchant updatedMerchant, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

currentMerchant Merchant

Current persisted merchant (not modified).

updatedMerchant Merchant

Updated merchant snapshot to persist.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements UpdateMerchantAsync(Merchant, Merchant, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<Merchant>
The persisted merchant.

Exceptions

System.ArgumentNullException
Thrown if currentMerchant or updatedMerchant is null.

System.OperationCanceledException
Thrown if the operation is cancelled.

InvoiceNoSqlBroker.UpdateMerchantAsync(Guid, Merchant, CancellationToken) Method

Replaces (upserts) a merchant by identifier (partition inferred via existing document lookup).

public System.Threading.Tasks.ValueTask<arolariu.Backend.Domain.Invoices.DDD.Entities.Merchants.Merchant> UpdateMerchantAsync(System.Guid merchantIdentifier, arolariu.Backend.Domain.Invoices.DDD.Entities.Merchants.Merchant updatedMerchant, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

merchantIdentifier System.Guid

Merchant identity.

updatedMerchant Merchant

Updated merchant snapshot.

cancellationToken System.Threading.CancellationToken

Optional cancellation token to abort the operation.

Implements UpdateMerchantAsync(Guid, Merchant, CancellationToken)

Returns

System.Threading.Tasks.ValueTask<Merchant>
The persisted merchant.

Exceptions

System.ArgumentNullException
Thrown if updatedMerchant is null.

System.OperationCanceledException
Thrown if the operation is cancelled.

// was this page useful?