Added comments, changed generic repository, removed warnings

This commit is contained in:
2024-08-15 14:15:01 +03:00
parent 798811b093
commit deaa9d9670
14 changed files with 71 additions and 24 deletions

View File

@@ -4,35 +4,50 @@ using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Database.Generic
{
public abstract class GenericRepository<T>(IGenericWriter writer, IUnitOfWork unitOfWork) : IGenericRepository<T>
/// Generic repository pattern
/// <param name="writer"><inheritdoc cref="IGenericWriter"/></param>
/// <param name="unitOfWork"><inheritdoc cref="IUnitOfWork"/></param>
/// <typeparam name="T">Type of entity</typeparam>
/// <remarks>Should be inherited to create typed repositories</remarks>
public abstract class GenericRepository<T>(IGenericReader reader, IGenericWriter writer, IUnitOfWork unitOfWork) : IGenericRepository<T>
where T : class, IEntity
{
/// <inheritdoc cref="IGenericRepository{T}.GetAllAsync"/>
public async Task<List<T>> GetAllAsync(CancellationToken cancellationToken)
=> await LoadDomain().ToListAsync(cancellationToken);
/// <inheritdoc cref="IGenericRepository{T}.GetOneAsync"/>
public async Task<T> GetOneAsync(Guid id, CancellationToken cancellationToken)
{
var result = await LoadDomain().SingleOrDefaultAsync(a => a.Id == id, cancellationToken);
return result ?? throw new EntityNotFoundException<T>(id);
}
/// <inheritdoc cref="IGenericRepository{T}.AddAsync"/>
public async Task AddAsync(T entity, CancellationToken cancellationToken)
=> await writer.AddAsync(entity, cancellationToken);
/// <inheritdoc cref="IGenericRepository{T}.UpdateAsync"/>
public async Task UpdateAsync(T entity, CancellationToken cancellationToken)
{
await GetOneAsync(entity.Id, cancellationToken);
writer.Update(entity);
}
/// <inheritdoc cref="IGenericRepository{T}.Remove"/>
public void Remove(T entity)
{
writer.Remove(entity);
}
/// <inheritdoc cref="IGenericRepository{T}.SaveAsync"/>
public async Task SaveAsync(CancellationToken cancellationToken)
=> await unitOfWork.SaveAsync(cancellationToken);
protected abstract IQueryable<T> LoadDomain();
/// Should be overriden to load navigation properties of entity
protected virtual IQueryable<T> LoadDomain()
{
return reader.GetAll<T>();
}
}
}

View File

@@ -2,9 +2,10 @@
namespace Infrastructure.Database.Generic
{
/// Reads from data storage
public interface IGenericReader
{
/// Get all entities of type <typeparamref name="T"/> stored in storage
/// Get all entities of type T stored in storage
/// <typeparam name="T">Entity type to seek in storage</typeparam>
IQueryable<T> GetAll<T>() where T : class, IEntity;
}

View File

@@ -2,18 +2,36 @@
namespace Infrastructure.Database.Generic
{
/// <summary>
/// Generic repository pattern
/// </summary>
/// <typeparam name="T">Entity type</typeparam>
public interface IGenericRepository<T> where T : class, IEntity
{
/// Get all entities from data storage
Task<List<T>> GetAllAsync(CancellationToken cancellationToken);
/// Get one entity with specific id
/// <param name="id">Identifier of entity</param>
Task<T> GetOneAsync(Guid id, CancellationToken cancellationToken);
/// Add entity to storage
/// <param name="entity">Entity to add</param>
Task AddAsync(T entity, CancellationToken cancellationToken);
/// <summary>
/// Update entity in storage
/// </summary>
/// <param name="entity">Entity to update</param>
Task UpdateAsync(T entity, CancellationToken cancellationToken);
/// <summary>
/// Remove entity from storage
/// </summary>
/// <param name="entity">Entity to remove</param>
void Remove(T entity);
/// Save changes in storage
Task SaveAsync(CancellationToken cancellationToken);
}
}

View File

@@ -6,18 +6,18 @@ namespace Infrastructure.Database.Generic
/// <remarks><see cref="IUnitOfWork"/> should be used to save changes</remarks>
public interface IGenericWriter
{
/// Add <paramref name="entity"/> to data storage
/// Add entity to data storage
/// <param name="entity">Entity to add</param>
/// <param name="cancellationToken">Cancellation Token</param>
/// <typeparam name="T">Entity type</typeparam>
Task AddAsync<T>(T entity, CancellationToken cancellationToken) where T : class, IEntity;
/// Update <paramref name="entity"/> in data storage
/// Update entity in data storage
/// <param name="entity">Entity to update</param>
/// <typeparam name="T">Entity type</typeparam>
void Update<T>(T entity) where T : class, IEntity;
/// Remove <paramref name="entity"/> from data storage
/// Remove entity from data storage
/// <param name="entity">Entity to remove</param>
/// <typeparam name="T">Entity type</typeparam>
void Remove<T>(T entity) where T : class, IEntity;