Deprecate controllers/services; simplify generics

Added `[Obsolete("Use MediatR")]` attributes to various controller and service classes to indicate deprecation in favor of MediatR. Simplified generic type constraints in `CRUDControllerBase` and related files by removing `IUnique<TId>`. Improved structure and documentation in `CSPMiddleware.cs`. Introduced new extension methods in `EntityExtensions.cs` for safer retrieval of 'Id' properties. Removed `IUnique.cs` interface and updated project dependencies in `DigitalData.Core.Application.csproj` for caching. Overall, these changes enhance code maintainability and clarity.
This commit is contained in:
Developer 02
2025-05-16 14:54:31 +02:00
parent e0c1b856ad
commit 55eb250d7e
18 changed files with 298 additions and 212 deletions

View File

@@ -1,4 +1,3 @@
using DigitalData.Core.Abstractions;
using DigitalData.Core.Application.Interfaces;
using Microsoft.AspNetCore.Mvc;
@@ -6,10 +5,11 @@ namespace DigitalData.Core.API
{
[ApiController]
[Route("api/[controller]")]
[Obsolete("Use MediatR")]
public class BasicCRUDControllerBase<TCRUDService, TDto, TEntity, TId> : CRUDControllerBase<TCRUDService, TDto, TDto, TDto, TEntity, TId>
where TCRUDService : ICRUDService<TDto, TDto, TEntity, TId>
where TDto : class, IUnique<TId>
where TEntity : class, IUnique<TId>
where TDto : class
where TEntity : class
{
public BasicCRUDControllerBase(ILogger logger, TCRUDService service) : base(logger, service)
{

View File

@@ -1,4 +1,3 @@
using DigitalData.Core.Abstractions;
using DigitalData.Core.Application.Interfaces;
using Microsoft.AspNetCore.Mvc;
using DigitalData.Core.Application.DTO;
@@ -15,12 +14,13 @@ namespace DigitalData.Core.API
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
[ApiController]
[Route("api/[controller]")]
[Obsolete("Use MediatR")]
public class CRUDControllerBase<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TEntity, TId>
where TCreateDto : class
where TReadDto : class
where TUpdateDto : class, IUnique<TId>
where TEntity : class, IUnique<TId>
where TUpdateDto : class
where TEntity : class
{
protected readonly ILogger _logger;
protected readonly TCRUDService _service;

View File

@@ -1,4 +1,3 @@
using DigitalData.Core.Abstractions;
using DigitalData.Core.Application.Interfaces;
using Microsoft.AspNetCore.Mvc;
using DigitalData.Core.Application.DTO;
@@ -16,12 +15,13 @@ namespace DigitalData.Core.API
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
[ApiController]
[Route("api/[controller]")]
[Obsolete("Use MediatR")]
public class CRUDControllerBaseWithErrorHandling<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TEntity, TId>
where TCreateDto : class
where TReadDto : class
where TUpdateDto : class, IUnique<TId>
where TEntity : class, IUnique<TId>
where TUpdateDto : class
where TEntity : class
{
protected readonly ILogger _logger;
protected readonly TCRUDService _service;

View File

@@ -1,47 +1,46 @@
namespace DigitalData.Core.API
namespace DigitalData.Core.API;
/// <summary>
/// Middleware to add Content Security Policy (CSP) headers to the HTTP response.
/// </summary>
public class CSPMiddleware
{
private readonly RequestDelegate _next;
private readonly string _policy;
/// <summary>
/// Middleware to add Content Security Policy (CSP) headers to the HTTP response.
/// Initializes a new instance of the <see cref="CSPMiddleware"/> class.
/// </summary>
public class CSPMiddleware
/// <param name="next">The next middleware in the request pipeline.</param>
/// <param name="policy">The CSP policy string with placeholders for nonces.</param>
public CSPMiddleware(RequestDelegate next, string policy)
{
private readonly RequestDelegate _next;
private readonly string _policy;
_next = next;
_policy = policy;
}
/// <summary>
/// Initializes a new instance of the <see cref="CSPMiddleware"/> class.
/// </summary>
/// <param name="next">The next middleware in the request pipeline.</param>
/// <param name="policy">The CSP policy string with placeholders for nonces.</param>
public CSPMiddleware(RequestDelegate next, string policy)
/// <summary>
/// Invokes the middleware to add the CSP header to the response.
/// </summary>
/// <param name="context">The HTTP context.</param>
/// <returns>A task that represents the completion of request processing.</returns>
public async Task Invoke(HttpContext context)
{
// Generate a nonce (number used once) for inline scripts and styles
var nonce = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
// Store the nonce in the context items for later use
context.Items["csp-nonce"] = nonce;
// Add the CSP header to the response
context.Response.OnStarting(() =>
{
_next = next;
_policy = policy;
}
context.Response.Headers.Append("Content-Security-Policy",
string.Format(_policy, nonce));
return Task.CompletedTask;
});
/// <summary>
/// Invokes the middleware to add the CSP header to the response.
/// </summary>
/// <param name="context">The HTTP context.</param>
/// <returns>A task that represents the completion of request processing.</returns>
public async Task Invoke(HttpContext context)
{
// Generate a nonce (number used once) for inline scripts and styles
var nonce = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
// Store the nonce in the context items for later use
context.Items["csp-nonce"] = nonce;
// Add the CSP header to the response
context.Response.OnStarting(() =>
{
context.Response.Headers.Add("Content-Security-Policy",
string.Format(_policy, nonce));
return Task.CompletedTask;
});
// Call the next middleware in the pipeline
await _next(context);
}
// Call the next middleware in the pipeline
await _next(context);
}
}

View File

@@ -12,6 +12,7 @@ namespace DigitalData.Core.API
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
[ApiController]
[Route("api/[controller]")]
[Obsolete("Use MediatR")]
public class ReadControllerBase<TReadService, TReadDto, TEntity, TId> : ControllerBase
where TReadService : IReadService<TReadDto, TEntity, TId>
where TReadDto : class

View File

@@ -13,6 +13,7 @@ namespace DigitalData.Core.API
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
[ApiController]
[Route("api/[controller]")]
[Obsolete("Use MediatR")]
public class ReadControllerBaseWithErrorHandling<TReadService, TReadDto, TEntity, TId> : ControllerBase
where TReadService : IReadService<TReadDto, TEntity, TId>
where TReadDto : class