Projektstruktur optimiert und Benutzer- & Gruppenverzeichnisdienste abgeschlossen.
This commit is contained in:
82
DigitalData.Core.Infrastructure/CRUDRepository.cs
Normal file
82
DigitalData.Core.Infrastructure/CRUDRepository.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using DigitalData.Core.Contracts.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>
|
||||
/// 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 _dbSet.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user