98 lines
4.7 KiB
C#
98 lines
4.7 KiB
C#
using DigitalData.Core.Abstractions.Infrastructure;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace DigitalData.Core.Infrastructure
|
|
{
|
|
/// <summary>
|
|
/// Provides a generic implementation for CRUD (Create, Read, Update, Delete) operations within a given DbContext.
|
|
/// </summary>
|
|
/// <typeparam name="TEntity">The entity type for which the repository is created. Must be a class.</typeparam>
|
|
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
|
|
/// <typeparam name="TDbContext">The DbContext type associated with the entity.</typeparam>
|
|
/// <remarks>
|
|
/// This repository abstracts the common database operations, offering an asynchronous API to work with the entity's data.
|
|
/// It leverages the EF Core's DbContext and DbSet to perform these operations.
|
|
/// </remarks>
|
|
public class CRUDRepository<TEntity, TId, TDbContext> : ICRUDRepository<TEntity, TId>
|
|
where TEntity : class
|
|
where TDbContext : DbContext
|
|
{
|
|
protected readonly TDbContext _dbContext;
|
|
protected readonly DbSet<TEntity> _dbSet;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the CRUDRepository with the specified DbContext.
|
|
/// </summary>
|
|
/// <param name="dbContext">The DbContext instance to be used by the repository.</param>
|
|
public CRUDRepository(TDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
_dbSet = dbContext.Set<TEntity>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously creates a new entity in the database.
|
|
/// </summary>
|
|
/// <param name="entity">The entity to be added.</param>
|
|
/// <returns>The created entity, or null if the entity cannot be added</returns>
|
|
public virtual async Task<TEntity?> CreateAsync(TEntity entity)
|
|
{
|
|
await _dbSet.AddAsync(entity);
|
|
await _dbContext.SaveChangesAsync();
|
|
return entity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously retrieves an entity by its identifier.
|
|
/// </summary>
|
|
/// <param name="id">The identifier of the entity to find.</param>
|
|
/// <returns>The entity found, or null if no entity is found with the specified identifier.</returns>
|
|
public virtual async Task<TEntity?> ReadByIdAsync(TId id) => await _dbSet.FindAsync(id);
|
|
|
|
/// <summary>
|
|
/// Retrieves all entities of type <typeparamref name="TEntity"/> from the database.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This method returns an <see cref="IQueryable{TEntity}"/> of all entities in the database.
|
|
/// The result is not tracked by the context, which improves performance when you only need to read data without making modifications.
|
|
/// </remarks>
|
|
/// <returns>An <see cref="IQueryable{TEntity}"/> containing all entities of type <typeparamref name="TEntity"/>.</returns>
|
|
protected virtual IQueryable<TEntity> ReadOnly() => _dbSet.AsNoTracking();
|
|
|
|
/// <summary>
|
|
/// Asynchronously retrieves all entities of type TEntity.
|
|
/// </summary>
|
|
/// <returns>An enumerable of all entities in the database.</returns>
|
|
public virtual async Task<IEnumerable<TEntity>> ReadAllAsync() => await ReadOnly().ToListAsync();
|
|
|
|
/// <summary>
|
|
/// Asynchronously updates an existing entity in the repository.
|
|
/// </summary>
|
|
/// <param name="entity">The entity to be updated. This entity should already exist in the repository.</param>
|
|
/// <returns>A task that represents the asynchronous operation. The task result contains a boolean value that indicates whether the update operation was successful. Returns true if one or more entities were successfully updated; otherwise, false.</returns>
|
|
public virtual async Task<bool> UpdateAsync(TEntity entity)
|
|
{
|
|
_dbContext.Entry(entity).State = EntityState.Modified;
|
|
var results = await _dbContext.SaveChangesAsync();
|
|
return results > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously deletes an entity from the database.
|
|
/// </summary>
|
|
/// <param name="entity">The entity to be deleted.</param>
|
|
/// <returns>If entity is deleted, return true othwerwise return false.</returns>
|
|
public virtual async Task<bool> DeleteAsync(TEntity entity)
|
|
{
|
|
_dbSet.Remove(entity);
|
|
var result = await _dbContext.SaveChangesAsync();
|
|
return result > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously counts all entities in the repository.
|
|
/// </summary>
|
|
/// <returns>The total number of entities in the repository.</returns>
|
|
public virtual async Task<int> CountAsync() => await _dbSet.CountAsync();
|
|
}
|
|
} |