Compare commits
2 Commits
14d00653d1
...
21c895c22b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
21c895c22b | ||
|
|
4afeddb7f9 |
@ -1,30 +1,29 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
namespace DigitalData.Core.Abstractions
|
namespace DigitalData.Core.Abstractions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extension methods for the <see cref="IConfiguration"/> interface, providing
|
||||||
|
/// additional functionality for retrieving configuration values with default behavior.
|
||||||
|
/// </summary>
|
||||||
|
public static class ConfigurationExtension
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extension methods for the <see cref="IConfiguration"/> interface, providing
|
/// Retrieves a configuration value for the specified key, or returns a default value
|
||||||
/// additional functionality for retrieving configuration values with default behavior.
|
/// of type <typeparamref name="T"/> if the configuration is not found or the key is null.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class ConfigurationExtension
|
/// <typeparam name="T">The type of the object to retrieve from the configuration.</typeparam>
|
||||||
{
|
/// <param name="configuration">The <see cref="IConfiguration"/> instance.</param>
|
||||||
/// <summary>
|
/// <param name="key">The optional key to look for in the configuration. If null, the method
|
||||||
/// Retrieves a configuration value for the specified key, or returns a default value
|
/// retrieves the root configuration.</param>
|
||||||
/// of type <typeparamref name="T"/> if the configuration is not found or the key is null.
|
/// <returns>
|
||||||
/// </summary>
|
/// An instance of <typeparamref name="T"/> populated from the configuration values, or
|
||||||
/// <typeparam name="T">The type of the object to retrieve from the configuration.</typeparam>
|
/// a new instance of <typeparamref name="T"/> if no matching configuration is found.
|
||||||
/// <param name="configuration">The <see cref="IConfiguration"/> instance.</param>
|
/// </returns>
|
||||||
/// <param name="key">The optional key to look for in the configuration. If null, the method
|
public static T GetOrDefault<T>(this IConfiguration configuration, string? key = null)
|
||||||
/// retrieves the root configuration.</param>
|
where T : new()
|
||||||
/// <returns>
|
=> (key is null
|
||||||
/// An instance of <typeparamref name="T"/> populated from the configuration values, or
|
? configuration.Get<T>()
|
||||||
/// a new instance of <typeparamref name="T"/> if no matching configuration is found.
|
: configuration.GetSection(key).Get<T>())
|
||||||
/// </returns>
|
?? new T();
|
||||||
public static T GetOrDefault<T>(this IConfiguration configuration, string? key = null)
|
|
||||||
where T : new()
|
|
||||||
=> (key is null
|
|
||||||
? configuration.Get<T>()
|
|
||||||
: configuration.GetSection(key).Get<T>())
|
|
||||||
?? new T();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,80 @@
|
|||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace DigitalData.Core.Application.Interfaces.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>
|
||||||
|
/// <returns>A task that represents the asynchronous operation. The task result contains the entity or a default value.</returns>
|
||||||
|
public Task<TEntity?> FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously retrieves a single entity or a default value if no entity is found.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task that represents the asynchronous operation. The task result contains the entity or a default value.</returns>
|
||||||
|
public Task<TEntity?> SingleOrDefaultAsync();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously retrieves a list of entities.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task that represents the asynchronous operation. The task result contains the list of entities.</returns>
|
||||||
|
public Task<IEnumerable<TEntity>> ToListAsync();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously retrieves the first entity. Throws an exception if no entity is found.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task that represents the asynchronous operation. The task result contains the first entity.</returns>
|
||||||
|
public Task<TEntity> FirstAsync();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously retrieves a single entity. Throws an exception if no entity is found.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task that represents the asynchronous operation. The task result contains the single entity.</returns>
|
||||||
|
public Task<TEntity> SingleAsync();
|
||||||
|
|
||||||
|
/// <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();
|
||||||
|
}
|
||||||
@ -9,16 +9,22 @@ public interface IRepository<TEntity>
|
|||||||
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken ct = default);
|
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken ct = default);
|
||||||
|
|
||||||
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, 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 IReadQuery<TEntity> Read(Expression<Func<TEntity, bool>> expression, 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 UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
|
||||||
|
|
||||||
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
|
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
|
||||||
|
|
||||||
|
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
|
||||||
|
public Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
|
||||||
|
|
||||||
|
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
|
||||||
|
public Task<TEntity?> ReadOrDefaultAsync(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default);
|
||||||
|
|
||||||
|
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
|
||||||
|
public Task<IEnumerable<TDto>> ReadAllAsync<TDto>(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
|
||||||
|
|
||||||
|
[Obsolete("Use Read-method returning IReadQuery<TEntity> instead.")]
|
||||||
|
public Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user