Add raw SQL support to repository interfaces and DI

Extended IRepository interfaces and DbRepository implementation to support raw and interpolated SQL query execution (sync/async) and querying. Updated DependencyInjection to allow registration of default repository implementations. Added Microsoft.EntityFrameworkCore.Abstractions as a dependency. Performed minor refactoring and cleanup.
This commit is contained in:
2025-12-19 09:54:56 +01:00
4 changed files with 62 additions and 14 deletions

View File

@@ -23,12 +23,22 @@ namespace DigitalData.Core.Infrastructure
Context = context;
}
public int ExecuteSqlRaw([NotParameterized] string sql, params object[] parameters)
public Task<int> ExecuteQueryRawAsync([NotParameterized] string sql, IEnumerable<object> parameters, CancellationToken cancel = default)
{
return Context.Database.ExecuteSqlRawAsync(sql, parameters, cancel);
}
public Task<int> ExecuteQueryInterpolatedAsync(FormattableString sql, CancellationToken cancel = default)
{
return Context.Database.ExecuteSqlInterpolatedAsync(sql, cancel);
}
public int ExecuteQueryRaw([NotParameterized] string sql, params object[] parameters)
{
return Context.Database.ExecuteSqlRaw(sql, parameters);
}
public int ExecuteSqlInterpolated(FormattableString sql)
public int ExecuteQueryInterpolated(FormattableString sql)
{
return Context.Database.ExecuteSqlInterpolated(sql);
}
@@ -57,11 +67,6 @@ namespace DigitalData.Core.Infrastructure
Mapper = mapper;
}
public IQueryable<TEntity> Sql([NotParameterized] string sql, params object[] parameters)
{
return Entities.FromSqlRaw(sql, parameters);
}
#region Create
public virtual async Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default)
{
@@ -95,6 +100,10 @@ namespace DigitalData.Core.Infrastructure
#region Read
public virtual IQueryable<TEntity> Query => Entities.AsNoTracking();
public IQueryable<TEntity> QueryRaw([NotParameterized] string sql, params object[] parameters) => Entities.FromSqlRaw(sql, parameters);
public IQueryable<TEntity> QueryInterpolated([NotParameterized] FormattableString sql) => Entities.FromSqlInterpolated(sql);
public virtual IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression) => Entities.AsNoTracking().Where(expression);
public virtual IEnumerable<TEntity> GetAll() => Entities.AsNoTracking().ToList();

View File

@@ -40,6 +40,9 @@ public static class DependencyInjection
// 3. register db set factories (can overwrite)
private readonly Queue<Action<IServiceCollection>> RegsDbSetFactory = new Queue<Action<IServiceCollection>>();
// 4. register repository
private readonly Queue<Action<IServiceCollection>> RegsRepository = new Queue<Action<IServiceCollection>>();
internal void RegisterAllServices(IServiceCollection services)
{
// 1. register from assembly
@@ -113,6 +116,11 @@ public static class DependencyInjection
where TDbContext : DbContext
where TEntity : class
=> RegsDbSetFactory.Enqueue(s => s.AddDbSetFactory(dbSetFactory));
public void RegisterDefaultRepository<TDbContext, TEntity>()
where TDbContext : DbContext
where TEntity : class
=> RegsRepository.Enqueue(s => s.AddScoped<IRepository<TEntity>, DbRepository<TDbContext, TEntity>>());
}
private static void InvokeAll<T>(this Queue<Action<T>> queue, T services)