diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/IQueryExecutor.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/IQueryExecutor.cs new file mode 100644 index 00000000..b64073e9 --- /dev/null +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/IQueryExecutor.cs @@ -0,0 +1,70 @@ +namespace EnvelopeGenerator.Application.Contracts.SQLExecutor; + +/// +/// Provides methods for executing common Entity Framework queries on a given entity type. +/// This interface abstracts away the direct usage of Entity Framework methods for querying data +/// and provides asynchronous and synchronous operations for querying a collection or single entity. +/// +/// The type of the entity being queried. +public interface IQueryExecutor +{ + /// + /// Asynchronously retrieves the first entity or a default value if no entity is found. + /// + /// A task that represents the asynchronous operation. The task result contains the entity or a default value. + public Task FirstOrDefaultAsync(); + + /// + /// Asynchronously retrieves a single entity or a default value if no entity is found. + /// + /// A task that represents the asynchronous operation. The task result contains the entity or a default value. + public Task SingleOrDefaultAsync(); + + /// + /// Asynchronously retrieves a list of entities. + /// + /// A task that represents the asynchronous operation. The task result contains the list of entities. + public Task> ToListAsync(); + + /// + /// Asynchronously retrieves the first entity. Throws an exception if no entity is found. + /// + /// A task that represents the asynchronous operation. The task result contains the first entity. + public Task FirstAsync(); + + /// + /// Asynchronously retrieves a single entity. Throws an exception if no entity is found. + /// + /// A task that represents the asynchronous operation. The task result contains the single entity. + public Task SingleAsync(); + + /// + /// Synchronously retrieves the first entity or a default value if no entity is found. + /// + /// The first entity or a default value. + public TEntity? FirstOrDefault(); + + /// + /// Synchronously retrieves a single entity or a default value if no entity is found. + /// + /// The single entity or a default value. + public TEntity? SingleOrDefault(); + + /// + /// Synchronously retrieves a list of entities. + /// + /// The list of entities. + public IEnumerable ToList(); + + /// + /// Synchronously retrieves the first entity. Throws an exception if no entity is found. + /// + /// The first entity. + public TEntity First(); + + /// + /// Synchronously retrieves a single entity. Throws an exception if no entity is found. + /// + /// The single entity. + public TEntity Single(); +} diff --git a/EnvelopeGenerator.Infrastructure/QueryExecutor.cs b/EnvelopeGenerator.Infrastructure/QueryExecutor.cs new file mode 100644 index 00000000..dcf148a2 --- /dev/null +++ b/EnvelopeGenerator.Infrastructure/QueryExecutor.cs @@ -0,0 +1,34 @@ +using EnvelopeGenerator.Application.Contracts.SQLExecutor; +using Microsoft.EntityFrameworkCore; + +namespace EnvelopeGenerator.Infrastructure; + +public record QueryExecutor(IQueryable Queryable) : IQueryExecutor +{ + public TEntity First() => Queryable.First(); + + public Task FirstAsync() => Queryable.FirstAsync(); + + public TEntity? FirstOrDefault() => Queryable.FirstOrDefault(); + + + public Task FirstOrDefaultAsync() => Queryable.FirstOrDefaultAsync(); + + + public TEntity Single() => Queryable.Single(); + + + public Task SingleAsync() => Queryable.SingleAsync(); + + + public TEntity? SingleOrDefault() => Queryable.SingleOrDefault(); + + + public Task SingleOrDefaultAsync() => Queryable.SingleOrDefaultAsync(); + + + public IEnumerable ToList() => Queryable.ToList(); + + + public async Task> ToListAsync() => await Queryable.ToListAsync(); +}