Skip to main content

IWriteRepositoryBase

Namespace: RA.Utilities.Data.Abstractions

The IWriteRepositoryBase<T> interface is a specialized contract that defines a set of write-only operations for a generic repository. Its primary purpose is to provide a standardized way to create, update, and delete entities in a data source.

This design is a direct application of the Command Query Separation (CQS) principle, which states that methods should either be commands that change the state of the system (like the ones in this interface) or queries that return data, but not both.

🔑 Key Features and Benefits

1. Enforces Write-Only Access

By depending on this interface, a service can be explicitly designed to only modify data, preventing it from performing read operations. This creates a clear and secure separation of concerns.

2. Provides Standard Mutation Methods

It defines a comprehensive set of methods for all common data modification tasks:

  • AddAsync / AddRangeAsync: For creating one or more entities.
  • UpdateAsync / UpdateRangeAsync: For modifying existing entities.
  • DeleteAsync / DeleteRangeAsync: For removing entities.

3. Promotes a Clean Architecture

It allows you to build services that are clearly defined as "command handlers," whose sole responsibility is to change the state of the application, leading to a more maintainable and understandable architecture.

⚙️ Methods

MethodReturn TypeDescription
AddAsyncTask<T>Adds a single new entity.
AddRangeAsyncTask<int>Adds a collection of new entities.
UpdateAsyncTask<T>Updates a single existing entity.
UpdateRangeAsyncTask<int>Updates a collection of existing entities.
DeleteAsyncTaskDeletes a single entity by its ID.
DeleteRangeAsyncTask<int>Deletes a collection of entities by their IDs.

AddAsync

Marks a single new entity to be added to the database.

ParameterTypeDescription
entityTThe entity to add.
cancellationTokenCancellationToken(Optional) A token to observe while waiting for the task to complete.

AddRangeAsync

Marks a collection of new entities to be added.

ParameterTypeDescription
entitiesIEnumerable<T>The collection of entities to add.
cancellationTokenCancellationToken(Optional) A token to observe while waiting for the task to complete.

UpdateAsync

Marks an existing entity as modified.

ParameterTypeDescription
entityTThe entity to update.
cancellationTokenCancellationToken(Optional) A token to observe while waiting for the task to complete.

UpdateRangeAsync

Marks a collection of existing entities as modified.

ParameterTypeDescription
entitiesIEnumerable<T>The collection of entities to update.
cancellationToken`CancellationToken(Optional) A token to observe while waiting for the task to complete.

DeleteAsync

Marks an existing entity for deletion.

ParameterTypeDescription
idTIdThe unique identifier of the entity to delete.
cancellationTokenCancellationToken(Optional) A token to observe while waiting for the task to complete.

DeleteRangeAsync

Marks a collection of existing entities for deletion.

ParameterTypeDescription
idsIEnumerable<TId>A collection of unique identifiers of the entities to delete.
cancellationTokenCancellationToken(Optional) A token to observe while waiting for the task to complete.