Refactor namespaces and enhance application structure
This commit reorganizes namespaces from `DigitalData.Core.Abstractions` and `DigitalData.Core.DTO` to `DigitalData.Core.Application.Interfaces` and `DigitalData.Core.Application.DTO`, improving maintainability and clarity. Updated using directives across multiple files to reflect the new structure, ensuring functionality remains intact. Project references in `DigitalData.Core.API.csproj` have been consolidated to include the new Application project. Introduced new classes and interfaces such as `BaseDTO`, `CookieConsentSettings`, `DataResult`, `Notice`, and `Result` to enhance data transfer and service result handling. Updated `IRepository`, `ICRUDRepository`, and `IEntityMapper` interfaces to facilitate CRUD operations and entity mapping. Added extension methods in `Extensions.cs` to improve repository usability. New interfaces for HTTP client services have been added, enhancing external API call handling. Overall, these changes reflect a significant restructuring aimed at improving organization and preparing for future development.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace DigitalData.Core.Application.Interfaces.Repository;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
#region Create
|
||||
public static Task<TEntity> CreateAsync<TEntity, TDto>(this IRepository<TEntity> repository, TDto dto, CancellationToken ct = default)
|
||||
{
|
||||
var entity = repository.Mapper.Map(dto);
|
||||
return repository.CreateAsync(entity, ct);
|
||||
}
|
||||
|
||||
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity, TDto>(this IRepository<TEntity> repository, IEnumerable<TDto> dtos, CancellationToken ct = default)
|
||||
{
|
||||
var entities = dtos.Select(dto => repository.Mapper.Map(dto));
|
||||
return repository.CreateAsync(entities, ct);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Read
|
||||
public static async Task<TEntity?> ReadFirstOrDefaultAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
||||
=> (await repository.ReadAllAsync(expression)).FirstOrDefault();
|
||||
|
||||
public static async Task<TEntity> ReadFirstAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
||||
=> (await repository.ReadAllAsync(expression)).First();
|
||||
|
||||
public static async Task<TEntity?> ReadSingleOrDefaultAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
||||
=> (await repository.ReadAllAsync(expression)).SingleOrDefault();
|
||||
|
||||
public static async Task<TEntity> ReadSingleAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
|
||||
=> (await repository.ReadAllAsync(expression)).Single();
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using DigitalData.Core.Abstractions;
|
||||
|
||||
namespace DigitalData.Core.Application.Interfaces.Repository
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the contract for CRUD operations on a repository for entities of type TEntity.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">The type of the entity this repository works with.</typeparam>
|
||||
/// <typeparam name="TId">The type of the identifier for the entity.</typeparam>
|
||||
public interface ICRUDRepository<TEntity, TId> where TEntity : class, IUnique<TId>
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a new entity to the repository.
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to add.</param>
|
||||
/// <returns>The added entity, or null if the entity cannot be added.</returns>
|
||||
Task<TEntity?> CreateAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves an entity by its identifier from the repository.
|
||||
/// </summary>
|
||||
/// <param name="id">The identifier of the entity to retrieve.</param>
|
||||
/// <returns>The entity found, or null if no entity is found.</returns>
|
||||
Task<TEntity?> ReadByIdAsync(TId id);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all entities from the repository.
|
||||
/// </summary>
|
||||
/// <returns>A collection of all entities.</returns>
|
||||
Task<IEnumerable<TEntity>> ReadAllAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Updates an existing entity in the repository.
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to update.</param>
|
||||
/// <returns>The updated entity.</returns>
|
||||
Task<bool> UpdateAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes an entity from the repository.
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to delete.</param>
|
||||
/// <returns>If entity is deleted, return true othwerwise return false.</returns>
|
||||
Task<bool> DeleteAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously counts all entities in the repository.
|
||||
/// </summary>
|
||||
/// <returns>The total number of entities in the repository.</returns>
|
||||
Task<int> CountAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously counts the number of entities in the repository that match a specific identifier.
|
||||
/// </summary>
|
||||
/// <param name="id">The identifier of the entities to count.</param>
|
||||
/// <returns>The number of entities with the specified identifier.</returns>
|
||||
/// <remarks>
|
||||
/// This method provides a count of entities in the database that match the given identifier.
|
||||
/// If there are multiple entities with the same identifier, they will all be counted.
|
||||
/// The default implementation assumes that the identifier is unique for each entity.
|
||||
/// </remarks>
|
||||
Task<int> CountAsync(TId id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
namespace DigitalData.Core.Application.Interfaces.Repository
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines methods for mapping between entities and Data Transfer Objects (DTOs).
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">The type of the entity to be mapped.</typeparam>
|
||||
public interface IEntityMapper<TEntity>
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps an entity to a DTO.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDto">The type of the DTO to map to.</typeparam>
|
||||
/// <param name="entity">The entity to be mapped.</param>
|
||||
/// <returns>The mapped DTO.</returns>
|
||||
TDto Map<TDto>(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// Maps an entity list to a DTO list.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDto">The type of the DTO to map to.</typeparam>
|
||||
/// <param name="entities">The entity list to be mapped.</param>
|
||||
/// <returns>The mapped DTO list.</returns>
|
||||
IEnumerable<TDto> Map<TDto>(IEnumerable<TEntity> entities);
|
||||
|
||||
/// <summary>
|
||||
/// Maps a DTO to an entity.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDto">The type of the DTO to be mapped.</typeparam>
|
||||
/// <param name="dto">The DTO to be mapped.</param>
|
||||
/// <returns>The mapped entity.</returns>
|
||||
TEntity Map<TDto>(TDto dto);
|
||||
|
||||
/// <summary>
|
||||
/// Maps a DTO list to an entity list.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDto">The type of the DTO to be mapped.</typeparam>
|
||||
/// <param name="dtos">The DTO list to be mapped.</param>
|
||||
/// <returns>The mapped entity list.</returns>
|
||||
IEnumerable<TEntity> Map<TDto>(IEnumerable<TDto> dtos);
|
||||
|
||||
/// <summary>
|
||||
/// Maps a DTO to an existing entity.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDto">The type of the DTO to be mapped.</typeparam>
|
||||
/// <param name="dto">The DTO to be mapped.</param>
|
||||
/// <param name="entity">The existing entity to be updated with the mapped values.</param>
|
||||
/// <returns>The updated entity.</returns>
|
||||
TEntity Map<TDto>(TDto dto, TEntity entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace DigitalData.Core.Application.Interfaces.Repository;
|
||||
|
||||
public interface IRepository<TEntity>
|
||||
{
|
||||
public IEntityMapper<TEntity> Mapper { get; }
|
||||
|
||||
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken ct = default);
|
||||
|
||||
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken ct = default);
|
||||
|
||||
public Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
|
||||
|
||||
public Task<TEntity?> ReadOrDefaultAsync(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default);
|
||||
|
||||
public Task<IEnumerable<TDto>> ReadAllAsync<TDto>(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
|
||||
|
||||
public Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default);
|
||||
|
||||
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
|
||||
|
||||
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
|
||||
}
|
||||
Reference in New Issue
Block a user