Skip to content

In Memory Service CRUD Data

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

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

public interface IModernCrudInMemoryService<TEntityDto, TId>
    where TEntity : class
    where TId : IEquatable<TId>

TEntityDto is a type of entity contained in the cache (is mapped for a repository)
TId is a type of the entity's identifier (mainly primary key)

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

✅ All methods of in memory service create, update and delete data in the database and in the in-memory cache

IModernCrudInMemoryService<TEntityDto, TId> has the following methods to create entities:

/// <summary>
/// Creates the new entity
/// </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="InternalErrorException">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<TEntityDto> CreateAsync(TEntityDto 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="InternalErrorException">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<TEntityDto>> CreateAsync(List<TEntityDto> entities, CancellationToken cancellationToken = default);

Example:

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

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

IModernCrudInMemoryService<TEntityDto, 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="InternalErrorException">Thrown if an error occurred while updating the entity in the data store</exception>
/// <returns>Updated entity</returns>
Task<TEntityDto> UpdateAsync(TId id, TEntityDto 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="InternalErrorException">Thrown if an error occurred while updating the entities in the data store</exception>
/// <returns>List of updated entities</returns>
Task<List<TEntityDto>> UpdateAsync(List<TEntityDto> 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="InternalErrorException">Thrown if an error occurred while updating the entity in the data store</exception>
/// <returns>Updated entity</returns>
Task<TEntityDto> UpdateAsync(TId id, Action<TEntityDto> update, CancellationToken cancellationToken = default);

Example:

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

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

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

IModernCrudInMemoryService<TEntityDto, 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="InternalErrorException">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="InternalErrorException">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="InternalErrorException">Thrown if an error occurred while deleting the entity in the data store</exception>
/// <returns>Deleted entity</returns>
Task<TEntityDto> 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 service.DeleteAsync(1);

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

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