Compare commits

...

5 Commits

12 changed files with 29 additions and 221 deletions

View File

@ -8,7 +8,7 @@
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<Description>This package provides a comprehensive set of API controllers and related utilities for the DigitalData.Core library. It includes generic CRUD controllers, localization extensions, middleware for security policies, and application model conventions.</Description> <Description>This package provides a comprehensive set of API controllers and related utilities for the DigitalData.Core library. It includes generic CRUD controllers, localization extensions, middleware for security policies, and application model conventions.</Description>
<PackageId>DigitalData.Core.API</PackageId> <PackageId>DigitalData.Core.API</PackageId>
<Version>2.2.0</Version> <Version>2.2.1</Version>
<Authors>Digital Data GmbH</Authors> <Authors>Digital Data GmbH</Authors>
<Company>Digital Data GmbH</Company> <Company>Digital Data GmbH</Company>
<Product>DigitalData.Core.API</Product> <Product>DigitalData.Core.API</Product>
@ -16,8 +16,8 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl> <RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackageTags>digital data core api</PackageTags> <PackageTags>digital data core api</PackageTags>
<PackageIcon>core_icon.png</PackageIcon> <PackageIcon>core_icon.png</PackageIcon>
<AssemblyVersion>2.2.0</AssemblyVersion> <AssemblyVersion>2.2.1</AssemblyVersion>
<FileVersion>2.2.0</FileVersion> <FileVersion>2.2.1</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -14,9 +14,9 @@
<PackageIcon>core_icon.png</PackageIcon> <PackageIcon>core_icon.png</PackageIcon>
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl> <RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackageTags>digital data core application clean architecture abstraction</PackageTags> <PackageTags>digital data core application clean architecture abstraction</PackageTags>
<Version>1.0.0</Version> <Version>1.1.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion> <AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion> <FileVersion>1.1.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -17,18 +17,4 @@ public static class Extensions
return repository.CreateAsync(entities, ct); return repository.CreateAsync(entities, ct);
} }
#endregion #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
} }

View File

@ -1,85 +0,0 @@
using System.Linq.Expressions;
namespace DigitalData.Core.Abstraction.Application.Repository;
/// <summary>
/// Provides methods for executing common queries on a given entity type.
/// This interface abstracts away the direct usage of ORM libraries (such as Entity Framework) for querying data
/// and provides asynchronous and synchronous operations for querying a collection or single entity.
/// </summary>
/// <typeparam name="TEntity">The type of the entity being queried.</typeparam>
public interface IReadQuery<TEntity>
{
/// <summary>
/// Adds a filter to the query using the specified predicate expression.
/// This method allows chaining multiple filter conditions to refine the query results.
/// </summary>
/// <param name="expression">An expression that defines the filter condition for the entity.</param>
/// <returns>The current <see cref="IReadQuery{TEntity}"/> instance with the applied filter.</returns>
public IReadQuery<TEntity> Where(Expression<Func<TEntity, bool>> expression);
/// <summary>
/// Asynchronously retrieves the first entity or a default value if no entity is found.
/// </summary>
/// <param name="cancellation">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the entity or a default value.</returns>
public Task<TEntity?> FirstOrDefaultAsync(CancellationToken cancellation = default);
/// <summary>
/// Asynchronously retrieves a single entity or a default value if no entity is found.
/// </summary>
/// <param name="cancellation">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the entity or a default value.</returns>
public Task<TEntity?> SingleOrDefaultAsync(CancellationToken cancellation = default);
/// <summary>
/// Asynchronously retrieves a list of entities.
/// </summary>
/// <param name="cancellation">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the list of entities.</returns>
public Task<IEnumerable<TEntity>> ToListAsync(CancellationToken cancellation = default);
/// <summary>
/// Asynchronously retrieves the first entity. Throws an exception if no entity is found.
/// </summary>
/// <param name="cancellation">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the first entity.</returns>
public Task<TEntity> FirstAsync(CancellationToken cancellation = default);
/// <summary>
/// Asynchronously retrieves a single entity. Throws an exception if no entity is found.
/// </summary>
/// <param name="cancellation">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the single entity.</returns>
public Task<TEntity> SingleAsync(CancellationToken cancellation = default);
/// <summary>
/// Synchronously retrieves the first entity or a default value if no entity is found.
/// </summary>
/// <returns>The first entity or a default value.</returns>
public TEntity? FirstOrDefault();
/// <summary>
/// Synchronously retrieves a single entity or a default value if no entity is found.
/// </summary>
/// <returns>The single entity or a default value.</returns>
public TEntity? SingleOrDefault();
/// <summary>
/// Synchronously retrieves a list of entities.
/// </summary>
/// <returns>The list of entities.</returns>
public IEnumerable<TEntity> ToList();
/// <summary>
/// Synchronously retrieves the first entity. Throws an exception if no entity is found.
/// </summary>
/// <returns>The first entity.</returns>
public TEntity First();
/// <summary>
/// Synchronously retrieves a single entity. Throws an exception if no entity is found.
/// </summary>
/// <returns>The single entity.</returns>
public TEntity Single();
}

View File

@ -10,23 +10,11 @@ public interface IRepository<TEntity>
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancellation = default); public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancellation = default);
public IReadQuery<TEntity> Read(params Expression<Func<TEntity, bool>>[] expressions); public IQueryable<TEntity> Read();
public IQueryable<TEntity> ReadOnly();
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancellation = default); public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancellation = default);
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellation = default); public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellation = default);
#region Obsolete
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
public Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken cancellation = default);
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
public Task<TEntity?> ReadOrDefaultAsync(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken cancellation = default);
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
public Task<IEnumerable<TDto>> ReadAllAsync<TDto>(Expression<Func<TEntity, bool>>? expression = null, CancellationToken cancellation = default);
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
public Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken cancellation = default);
#endregion
} }

View File

@ -1,14 +0,0 @@
using System.Linq.Expressions;
namespace DigitalData.Core.Abstraction.Application.Repository;
public static class RepositoryExtensions
{
public static IReadQuery<TEntity> Where<TEntity>(this IReadQuery<TEntity> query, params Expression<Func<TEntity, bool>>[] expressions)
{
foreach (var expression in expressions)
query = query.Where(expression);
return query;
}
}

View File

@ -14,9 +14,9 @@
<PackageIcon>core_icon.png</PackageIcon> <PackageIcon>core_icon.png</PackageIcon>
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl> <RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackageTags>digital data core application clean architecture</PackageTags> <PackageTags>digital data core application clean architecture</PackageTags>
<Version>3.3.4</Version> <Version>3.4.0</Version>
<AssemblyVersion>3.3.4</AssemblyVersion> <AssemblyVersion>3.4.0</AssemblyVersion>
<FileVersion>3.3.4</FileVersion> <FileVersion>3.4.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -17,9 +17,9 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl> <RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackAsTool>False</PackAsTool> <PackAsTool>False</PackAsTool>
<PackageIcon>core_icon.png</PackageIcon> <PackageIcon>core_icon.png</PackageIcon>
<Version>1.0.0</Version> <Version>1.0.1</Version>
<AssemblyVersion>1.0.0</AssemblyVersion> <AssemblyVersion>1.0.1</AssemblyVersion>
<FileVersion>1.0.0</FileVersion> <FileVersion>1.0.1</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -38,6 +38,10 @@
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'"> <ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.5" /> <PackageReference Include="Microsoft.Extensions.Options" Version="9.0.5" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.7" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\DigitalData.Core.Exceptions\DigitalData.Core.Exceptions.csproj" /> <ProjectReference Include="..\DigitalData.Core.Exceptions\DigitalData.Core.Exceptions.csproj" />

View File

@ -42,11 +42,11 @@ public class GlobalExceptionHandlerMiddleware
} }
catch (Exception ex) catch (Exception ex)
{ {
if(ex.GetType() == typeof(Exception)) if(ex.GetType() == typeof(Exception) && _options?.DefaultHandler is not null)
_options?.DefaultHandler?.HandleExceptionAsync.Invoke(context, ex, _logger); await _options.DefaultHandler.HandleExceptionAsync(context, ex, _logger);
if (_options?.Handlers.TryGetValue(ex.GetType(), out var handler) ?? false) if (_options?.Handlers.TryGetValue(ex.GetType(), out var handler) ?? false)
handler?.HandleExceptionAsync.Invoke(context, ex, _logger); await handler.HandleExceptionAsync(context, ex, _logger);
} }
} }
} }

View File

@ -33,8 +33,10 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
return entities; return entities;
} }
public IReadQuery<TEntity> Read(params Expression<Func<TEntity, bool>>[] expressions) => new ReadQuery<TEntity>(Entities.AsNoTracking()).Where(expressions); public IQueryable<TEntity> Read() => Entities.AsQueryable();
public IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
public virtual async Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default) public virtual async Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default)
{ {
var entities = await Entities.Where(expression).ToListAsync(ct); var entities = await Entities.Where(expression).ToListAsync(ct);
@ -59,29 +61,4 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
await Context.SaveChangesAsync(ct); await Context.SaveChangesAsync(ct);
} }
#region Obsolete
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
public virtual async Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
=> expression is null
? await Entities.AsNoTracking().ToListAsync(ct)
: await Entities.AsNoTracking().Where(expression).ToListAsync(ct);
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
public virtual async Task<TEntity?> ReadOrDefaultAsync(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default)
=> single
? await Entities.AsNoTracking().Where(expression).SingleOrDefaultAsync(ct)
: await Entities.AsNoTracking().Where(expression).FirstOrDefaultAsync(ct);
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
public virtual async Task<IEnumerable<TDto>> ReadAllAsync<TDto>(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
=> Mapper.Map<TDto>(await ReadAllAsync(expression, ct));
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
public virtual async Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default)
{
var entity = await ReadOrDefaultAsync(expression, single, ct);
return entity is null ? default : Mapper.Map<TDto>(entity);
}
#endregion
} }

View File

@ -15,9 +15,9 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl> <RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<RepositoryType>digital data core abstractions clean architecture</RepositoryType> <RepositoryType>digital data core abstractions clean architecture</RepositoryType>
<PackageTags>digital data core infrastructure clean architecture</PackageTags> <PackageTags>digital data core infrastructure clean architecture</PackageTags>
<Version>2.1.1</Version> <Version>2.2.0</Version>
<AssemblyVersion>2.1.1</AssemblyVersion> <AssemblyVersion>2.2.0</AssemblyVersion>
<FileVersion>2.1.1</FileVersion> <FileVersion>2.2.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,48 +0,0 @@
using DigitalData.Core.Abstraction.Application.Repository;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
namespace DigitalData.Core.Infrastructure;
public sealed record ReadQuery<TEntity> : IReadQuery<TEntity>
{
private IQueryable<TEntity> _query;
internal ReadQuery(IQueryable<TEntity> queryable)
{
_query = queryable;
}
public TEntity First() => _query.First();
public Task<TEntity> FirstAsync(CancellationToken cancellation = default) => _query.FirstAsync(cancellation);
public TEntity? FirstOrDefault() => _query.FirstOrDefault();
public Task<TEntity?> FirstOrDefaultAsync(CancellationToken cancellation = default) => _query.FirstOrDefaultAsync(cancellation);
public TEntity Single() => _query.Single();
public Task<TEntity> SingleAsync(CancellationToken cancellation = default) => _query.SingleAsync(cancellation);
public TEntity? SingleOrDefault() => _query.SingleOrDefault();
public Task<TEntity?> SingleOrDefaultAsync(CancellationToken cancellation = default) => _query.SingleOrDefaultAsync(cancellation);
public IEnumerable<TEntity> ToList() => _query.ToList();
public async Task<IEnumerable<TEntity>> ToListAsync(CancellationToken cancellation = default) => await _query.ToListAsync(cancellation);
public IReadQuery<TEntity> Where(Expression<Func<TEntity, bool>> expression)
{
_query = _query.Where(expression);
return this;
}
}