Skip to content

Repository CRUD Data

anton-martyniuk edited this page Oct 26, 2022 · 3 revisions

The following repository interface is used for Creating, Updating and Deleting data:

public interface IModernCrudRepository<TEntity, TId>
    where TEntity : class
    where TId : IEquatable<TId>

TEntity is a type of entity contained in the data store
TId is a type of the entity's identifier (mainly primary key)

Repository interface has methods for creation, updating and deletion of single and multiple entities.

IModernCrudRepository<TEntity, TId> has the following methods to create entities:

/// <summary>
/// Creates the new entity in the data store
/// </summary>
/// <param name="entity">The entity to add to the data store</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete</param>
/// <exception cref="ArgumentNullException">Thrown if provided entity is null</exception>
/// <exception cref="EntityAlreadyExistsException">Thrown if an entity already exists in the data store</exception>
/// <exception cref="RepositoryErrorException">Thrown if an error occurred while saving the entity in the data store</exception>
/// <returns>Updated entity by the data store (primary key, for example)</returns>
Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancellationToken = default);

/// <summary>
/// Creates a list of new entities in the data store
/// </summary>
/// <param name="entities">The list of entities to add to the data store</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete</param>
/// <exception cref="ArgumentNullException">Thrown if provided list of entities is null</exception>
/// <exception cref="RepositoryErrorException">Thrown if an error occurred while saving the entities in the data store</exception>
/// <returns>A list of updated entities by the data store (primary key, for example)</returns>
Task<List<TEntity>> CreateAsync(List<TEntity> entities, CancellationToken cancellationToken = default);

Example:

// Create single entity
var createdEntity = await repository.CreateAsync(new Airplane
{
    Model = "Boeing 737-800",
    YearOfManufacture = 2022
});

// Create multiple entities
var createdEntities = await repository.CreateAsync(new List<Airplane>
{
    new Airplane
    {
        Model = "Boeing 737-800",
        YearOfManufacture = 2022
    },
    new Airplane
    {
        Model = "Boeing 777-200ER",
        YearOfManufacture = 2021
    }
});

IModernCrudRepository<TEntity, TId> has the following methods to update entities:

/// <summary>
/// Updates the entity in the data store with the given <paramref name="id"/>
/// </summary>
/// <param name="id">The entity id</param>
/// <param name="entity">The entity which should be updated in the data store</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete</param>
/// <exception cref="ArgumentNullException">Thrown if provided id or entity is null</exception>
/// <exception cref="EntityNotFoundException">Thrown if an entity does not exist in the data store</exception>
/// <exception cref="EntityConcurrentUpdateException">If an entity concurrent update occurred</exception>
/// <exception cref="RepositoryErrorException">Thrown if an error occurred while updating the entity in the data store</exception>
/// <returns>Updated entity</returns>
Task<TEntity> UpdateAsync(TId id, TEntity entity, CancellationToken cancellationToken = default);

/// <summary>
/// Updates the list of entities in the data store with the given list of <paramref name="entities"/>.<br/>
/// If all or some of entities were not found in the data store - no exception is thrown
/// </summary>
/// <param name="entities">The list of entities which should be updated in the data store</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete</param>
/// <exception cref="ArgumentNullException">Thrown if provided list of entities is null or has no entities in the list</exception>
/// <exception cref="EntityConcurrentUpdateException">If an entity concurrent update occurred</exception>
/// <exception cref="RepositoryErrorException">Thrown if an error occurred while updating the entities in the data store</exception>
/// <returns>List of updated entities</returns>
Task<List<TEntity>> UpdateAsync(List<TEntity> entities, CancellationToken cancellationToken = default);

/// <summary>
/// Updates the entity in the data store with the given <paramref name="id"/>
/// </summary>
/// <param name="id">The entity id</param>
/// <param name="update">The entity update action</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete</param>
/// <exception cref="ArgumentNullException">Thrown if provided id or entity is null</exception>
/// <exception cref="EntityNotFoundException">Thrown if an entity does not exist in the data store</exception>
/// <exception cref="EntityConcurrentUpdateException">If an entity concurrent update occurred</exception>
/// <exception cref="RepositoryErrorException">Thrown if an error occurred while updating the entity in the data store</exception>
/// <returns>Updated entity</returns>
Task<TEntity> UpdateAsync(TId id, Action<TEntity> update, CancellationToken cancellationToken = default);

Example:

// Update single entity by id
var updatedEntity = await repository.UpdateAsync(airplaneEntity.Id, airplaneEntity);

// Update single entity by an action
var updatedEntity = await repository.UpdateAsync(1, airplane =>
{
    airplane.YearOfManufacture = 2020;
});

// Update multiple entities
var updatedEntities = await repository.UpdateAsync(airplaneEntities);

IModernCrudRepository<TEntity, TId> has the following methods to delete entities:

/// <summary>
/// Deletes the entity in the data store with the given <paramref name="id"/>.<br/>
/// This method does NOT query the entity from the data store before deletion
/// </summary>
/// <param name="id">The entity id</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete</param>
/// <exception cref="ArgumentNullException">Thrown if provided id is null</exception>
/// <exception cref="RepositoryErrorException">Thrown if an error occurred while deleting the entity in the data store</exception>
/// <returns><see langword="true"/> if entity was deleted; otherwise, <see langword="false"/></returns>
Task<bool> DeleteAsync(TId id, CancellationToken cancellationToken = default);

/// <summary>
/// Deletes the list of entities in the data store with the given list of <paramref name="ids"/>.<br/>
/// This method does NOT query the entities from the data store before deletion.<br/>
/// If all or some of entities were not found in the data store - no exception is thrown
/// </summary>
/// <param name="ids">The list of entity ids</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete</param>
/// <exception cref="ArgumentNullException">Thrown if provided list of entities is null or has no entities in the list</exception>
/// <exception cref="RepositoryErrorException">Thrown if an error occurred while deleting the entities in the data store</exception>
/// <returns><see langword="true"/> if all entities were deleted; otherwise, <see langword="false"/></returns>
Task<bool> DeleteAsync(List<TId> ids, CancellationToken cancellationToken = default);

/// <summary>
/// Deletes and returns an entity in the data store with the given <paramref name="id"/><br/>
/// This method queries the entity from the data store before deletion
/// </summary>
/// <param name="id">The entity id</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete</param>
/// <exception cref="ArgumentNullException">Thrown if provided id is null</exception>
/// <exception cref="EntityNotFoundException">Thrown if an entity does not exist in the data store</exception>
/// <exception cref="RepositoryErrorException">Thrown if an error occurred while deleting the entity in the data store</exception>
/// <returns>Deleted entity</returns>
Task<TEntity> DeleteAndReturnAsync(TId id, CancellationToken cancellationToken = default);

Both DeleteAsync methods for deletion of single and multiple entities perform bulk delete and do not query entity(s) from the database.
DeleteAndReturnAsync method queries and returns deleted entity from the data store.

Example:

// Delete single entity by id
var isDeleted = await repository.DeleteAsync(1);

// Delete single entity by id and return it
var deletedEntity = await repository.DeleteAndReturnAsync(1);

// Delete multiple entities by ids
var isAllDeleted = await repository.DeleteAsync(new List<int> { 1, 5, 10 });
Clone this wiki locally