- Add AutoMapper profile for CfgProfile and ProfileSqlJob entity-to-DTO mappings - Consolidate GetProfileByIdQuery and GetAllActiveProfilesQuery into unified GetProfileQuery - Implement flexible filtering with nullable query options (Id, Active, TypeId, ProfileName) - Add IncludeSqlJobs option for optimized SQL job loading - Move CfgProfileDto to Common/Dtos for better architecture alignment - Add comprehensive unit tests for GetProfileQuery with multiple filter scenarios - Move ISQLExecutor interface from Domain to Application layer - Add GetByIdWithSqlJobsAsync and GetAllActiveWithSqlJobsAsync to repository
58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
#if NET48
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
#else
|
|
using Microsoft.EntityFrameworkCore;
|
|
#endif
|
|
using ECMJobRunner.Application.Common.Interfaces;
|
|
using ECMJobRunner.Infrastructure.Data;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ECMJobRunner.Infrastructure.Services
|
|
{
|
|
/// <summary>
|
|
/// Entity Framework implementation of ISQLExecutor
|
|
/// Executes raw SQL queries and maps results to DTOs
|
|
/// </summary>
|
|
public class SQLExecutor : ISQLExecutor
|
|
{
|
|
private readonly JobRunnerDbContext _context;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of SQLExecutor
|
|
/// </summary>
|
|
/// <param name="context">The database context</param>
|
|
public SQLExecutor(JobRunnerDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes a SQL query and maps the result to the specified type
|
|
/// </summary>
|
|
/// <typeparam name="TResult">The type to map the query result to</typeparam>
|
|
/// <param name="sql">The SQL query to execute</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>The mapped result or null if no result</returns>
|
|
public async Task<TResult?> ExecuteQueryAsync<TResult>(string sql, CancellationToken cancellationToken = default)
|
|
{
|
|
#if NET48
|
|
// Entity Framework 6 implementation
|
|
var result = await _context.Database
|
|
.SqlQuery<TResult>(sql)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
|
|
return result;
|
|
#else
|
|
// Entity Framework Core implementation
|
|
var result = await _context.Database
|
|
.SqlQueryRaw<TResult>(sql)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
|
|
return result;
|
|
#endif
|
|
}
|
|
}
|
|
}
|