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:
@@ -37,7 +37,7 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.16" />
|
||||
@@ -52,24 +52,28 @@
|
||||
<PackageReference Include="AutoMapper" Version="10.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="3.1.32" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.20" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
|
||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.15" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
|
||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="9.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
#if NETFRAMEWORK
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -8,11 +9,29 @@ using System.Linq;
|
||||
#endif
|
||||
|
||||
namespace DigitalData.Core.Abstraction.Application.Repository
|
||||
#if NET
|
||||
;
|
||||
#elif NETFRAMEWORK
|
||||
{
|
||||
public interface IRepository
|
||||
{
|
||||
#if NET
|
||||
public
|
||||
#endif
|
||||
Task<int> ExecuteQueryRawAsync([NotParameterized] string sql, IEnumerable<object> parameters, CancellationToken cancel = default);
|
||||
|
||||
#if NET
|
||||
public
|
||||
#endif
|
||||
Task<int> ExecuteQueryInterpolatedAsync(FormattableString sql, CancellationToken cancel = default);
|
||||
|
||||
#if NET
|
||||
public
|
||||
#endif
|
||||
int ExecuteQueryRaw([NotParameterized] string sql, params object[] parameters);
|
||||
|
||||
#if NET
|
||||
public
|
||||
#endif
|
||||
int ExecuteQueryInterpolated(FormattableString sql);
|
||||
}
|
||||
|
||||
public interface IRepository<TEntity>
|
||||
{
|
||||
@@ -44,6 +63,16 @@ namespace DigitalData.Core.Abstraction.Application.Repository
|
||||
#endif
|
||||
IQueryable<TEntity> Query { get; }
|
||||
|
||||
#if NET
|
||||
public
|
||||
#endif
|
||||
IQueryable<TEntity> QueryRaw([NotParameterized] string sql, params object[] parameters);
|
||||
|
||||
#if NET
|
||||
public
|
||||
#endif
|
||||
IQueryable<TEntity> QueryInterpolated([NotParameterized] FormattableString sql);
|
||||
|
||||
#if NET
|
||||
public
|
||||
#endif
|
||||
@@ -108,6 +137,4 @@ namespace DigitalData.Core.Abstraction.Application.Repository
|
||||
IQueryable<TEntity> ReadOnly();
|
||||
#endregion
|
||||
}
|
||||
#if NETFRAMEWORK
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user