Table of Contents

Class EntityUpsertCommand<TKey, TUpdateModel, TReadModel>

Namespace
Arbiter.CommandQuery.Commands
Assembly
Arbiter.CommandQuery.dll

Represents a command to create or update an entity identified by a specific key using the provided update model. The result of the command will be of type TReadModel.

public record EntityUpsertCommand<TKey, TUpdateModel, TReadModel> : EntityModelCommand<TUpdateModel, TReadModel>, IRequest<TReadModel>, IRequest, IEquatable<PrincipalCommandBase<TReadModel>>, IEquatable<EntityModelCommand<TUpdateModel, TReadModel>>, ICacheExpire, IEquatable<EntityUpsertCommand<TKey, TUpdateModel, TReadModel>>

Type Parameters

TKey

The type of the key used to identify the entity.

TUpdateModel

The type of the update model containing the data for the operation.

TReadModel

The type of the read model returned as the result of the command.

Inheritance
EntityModelCommand<TUpdateModel, TReadModel>
EntityUpsertCommand<TKey, TUpdateModel, TReadModel>
Implements
IRequest<TReadModel>
IEquatable<EntityModelCommand<TUpdateModel, TReadModel>>
IEquatable<EntityUpsertCommand<TKey, TUpdateModel, TReadModel>>
Inherited Members

Examples

The following example demonstrates how to use the EntityUpsertCommand<TKey, TUpdateModel, TReadModel>:

var updateModel = new ProductUpdateModel
{
    Name = "Updated Product",
    Description = "Updated description of the product",
    Price = 29.99m
};

var principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "JohnDoe") }));
var command = new EntityUpsertCommand<int, ProductUpdateModel, ProductReadModel>(principal, 123, updateModel);

// Pass the command to a handler or mediator
var result = await mediator.Send(command);
Console.WriteLine($"Upsert product name: {result?.Name}");

Remarks

This command is typically used in a CQRS (Command Query Responsibility Segregation) pattern to either create a new entity or update an existing entity, and return a read model representing the resulting entity or a related result.

Constructors

EntityUpsertCommand(ClaimsPrincipal?, TKey, TUpdateModel)

Initializes a new instance of the EntityUpsertCommand<TKey, TUpdateModel, TReadModel> class.

public EntityUpsertCommand(ClaimsPrincipal? principal, TKey id, TUpdateModel model)

Parameters

principal ClaimsPrincipal

The ClaimsPrincipal representing the user executing the command.

id TKey

The identifier of the entity to create or update.

model TUpdateModel

The update model containing the data for the operation.

Exceptions

ArgumentNullException

Thrown when id or model is null.

Properties

Id

Gets the identifier of the entity to create or update.

[JsonPropertyName("id")]
public TKey Id { get; }

Property Value

TKey

The identifier of the entity to create or update.