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>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.16" />
|
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.16" />
|
||||||
@@ -52,24 +52,28 @@
|
|||||||
<PackageReference Include="AutoMapper" Version="10.1.1" />
|
<PackageReference Include="AutoMapper" Version="10.1.1" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" 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>
|
||||||
|
|
||||||
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
|
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
|
||||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" 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>
|
||||||
|
|
||||||
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
|
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
|
||||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.15" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
|
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
|
||||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.5" />
|
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.5" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" 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>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
using Microsoft.EntityFrameworkCore.Query;
|
||||||
#if NETFRAMEWORK
|
#if NETFRAMEWORK
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -8,11 +9,29 @@ using System.Linq;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace DigitalData.Core.Abstraction.Application.Repository
|
namespace DigitalData.Core.Abstraction.Application.Repository
|
||||||
#if NET
|
{
|
||||||
;
|
public interface IRepository
|
||||||
#elif NETFRAMEWORK
|
|
||||||
{
|
{
|
||||||
|
#if NET
|
||||||
|
public
|
||||||
#endif
|
#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>
|
public interface IRepository<TEntity>
|
||||||
{
|
{
|
||||||
@@ -44,6 +63,16 @@ namespace DigitalData.Core.Abstraction.Application.Repository
|
|||||||
#endif
|
#endif
|
||||||
IQueryable<TEntity> Query { get; }
|
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
|
#if NET
|
||||||
public
|
public
|
||||||
#endif
|
#endif
|
||||||
@@ -108,6 +137,4 @@ namespace DigitalData.Core.Abstraction.Application.Repository
|
|||||||
IQueryable<TEntity> ReadOnly();
|
IQueryable<TEntity> ReadOnly();
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
#if NETFRAMEWORK
|
}
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@@ -23,12 +23,22 @@ namespace DigitalData.Core.Infrastructure
|
|||||||
Context = context;
|
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);
|
return Context.Database.ExecuteSqlRaw(sql, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int ExecuteSqlInterpolated(FormattableString sql)
|
public int ExecuteQueryInterpolated(FormattableString sql)
|
||||||
{
|
{
|
||||||
return Context.Database.ExecuteSqlInterpolated(sql);
|
return Context.Database.ExecuteSqlInterpolated(sql);
|
||||||
}
|
}
|
||||||
@@ -57,11 +67,6 @@ namespace DigitalData.Core.Infrastructure
|
|||||||
Mapper = mapper;
|
Mapper = mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IQueryable<TEntity> Sql([NotParameterized] string sql, params object[] parameters)
|
|
||||||
{
|
|
||||||
return Entities.FromSqlRaw(sql, parameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Create
|
#region Create
|
||||||
public virtual async Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default)
|
public virtual async Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default)
|
||||||
{
|
{
|
||||||
@@ -95,6 +100,10 @@ namespace DigitalData.Core.Infrastructure
|
|||||||
#region Read
|
#region Read
|
||||||
public virtual IQueryable<TEntity> Query => Entities.AsNoTracking();
|
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 IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression) => Entities.AsNoTracking().Where(expression);
|
||||||
|
|
||||||
public virtual IEnumerable<TEntity> GetAll() => Entities.AsNoTracking().ToList();
|
public virtual IEnumerable<TEntity> GetAll() => Entities.AsNoTracking().ToList();
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ public static class DependencyInjection
|
|||||||
// 3. register db set factories (can overwrite)
|
// 3. register db set factories (can overwrite)
|
||||||
private readonly Queue<Action<IServiceCollection>> RegsDbSetFactory = new Queue<Action<IServiceCollection>>();
|
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)
|
internal void RegisterAllServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
// 1. register from assembly
|
// 1. register from assembly
|
||||||
@@ -113,6 +116,11 @@ public static class DependencyInjection
|
|||||||
where TDbContext : DbContext
|
where TDbContext : DbContext
|
||||||
where TEntity : class
|
where TEntity : class
|
||||||
=> RegsDbSetFactory.Enqueue(s => s.AddDbSetFactory(dbSetFactory));
|
=> 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)
|
private static void InvokeAll<T>(this Queue<Action<T>> queue, T services)
|
||||||
|
|||||||
Reference in New Issue
Block a user