Developer 02 4b71836fea Erweiterung der DTOs und Implementierung der Lokalisierungsdienste
- Neue DTO-Extension-Methoden hinzugefügt, um die Verarbeitung und Zuweisung von Nachrichten und Benachrichtigungen in Ergebnisobjekten zu vereinfachen.
- Lokalisierungsunterstützung in der API-Schicht implementiert, einschließlich Cookie-basierter Lokalisierung und Konfiguration unterstützter Kulturen.
- Die Integration von StringLocalizer in die API-Schicht wurde durchgeführt, um eine nahtlose Mehrsprachigkeit zu ermöglichen.
- Fehlerbehandlung für fehlende Konfigurationseinstellungen verbessert.

Die Änderungen verbessern die Flexibilität und Wartbarkeit des Codes und unterstützen eine effizientere Internationalisierung der Anwendung.
2024-04-30 17:01:26 +02:00

88 lines
4.1 KiB
C#

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;
}
/// <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();
}
}