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.

All write operations are marked on the underlying change tracker and are only persisted when SaveChangesAsync is called, allowing multiple operations to be composed into a single, atomic transaction.

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.
  • SaveChangesAsync: For persisting all marked changes.

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>Marks a single new entity to be added.
AddRangeAsyncTask<int>Marks a collection of new entities to be added.
UpdateAsyncTask<T>Marks a single existing entity as modified.
UpdateRangeAsyncTask<int>Marks a collection of existing entities as modified.
DeleteAsyncTaskMarks an existing entity for deletion.
DeleteRangeAsyncTask<int>Marks a collection of existing entities for deletion.
SaveChangesAsyncTask<int>Persists all pending changes to the data source.

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.
cancellationTokenCancellationToken(Optional) A token to observe while waiting for the task to complete.

DeleteAsync​

Marks an existing entity for deletion. The method is generic over the key type, so any non-nullable identifier can be used.

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
idsList<Guid>A collection of unique identifiers of the entities to delete.
cancellationTokenCancellationToken(Optional) A token to observe while waiting for the task to complete.

[!NOTE] The current signature is hard-coded to Guid keys. Support for other key types is planned for a future release.

SaveChangesAsync​

Persists all pending changes to the data source. Until it is called, write operations only mark entities in the change tracker.

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