Skip to content

Commit cf2eaa0

Browse files
authored
Merge pull request #169 from cnblogs/support-sql-repository
feat: support sql repository
2 parents 7196ccb + 89a2b4a commit cf2eaa0

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Cnblogs.Architecture.Ddd.Domain.Abstractions;
2+
3+
/// <summary>
4+
/// Repository that support raw sql execution.
5+
/// </summary>
6+
/// <typeparam name="TEntity">The type of entity.</typeparam>
7+
/// <typeparam name="TKey">The type of key.</typeparam>
8+
public interface ISqlRepository<TEntity, TKey> : IRepository<TEntity, TKey>
9+
where TEntity : EntityBase, IAggregateRoot
10+
where TKey : IComparable<TKey>
11+
{
12+
/// <summary>
13+
/// Query entity with raw sql.
14+
/// </summary>
15+
/// <param name="sql">The sql string.</param>
16+
/// <param name="parameters">The parameters</param>
17+
/// <returns></returns>
18+
IQueryable<TEntity> SqlQuery(string sql, params object[] parameters);
19+
20+
/// <summary>
21+
/// Query with raw sql.
22+
/// </summary>
23+
/// <param name="sql">The sql string.</param>
24+
/// <param name="parameters">The parameters.</param>
25+
/// <typeparam name="T">The type of query result.</typeparam>
26+
/// <returns></returns>
27+
IQueryable<T> SqlQuery<T>(string sql, params object[] parameters);
28+
29+
/// <summary>
30+
/// Execute raw sql.
31+
/// </summary>
32+
/// <param name="sql">The sql string.</param>
33+
/// <param name="parameters">The parameters.</param>
34+
/// <returns>The number of rows affected.</returns>
35+
Task<int> ExecuteSqlAsync(string sql, params object[] parameters);
36+
}

src/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework/BaseRepository.cs

+19-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework;
1212
/// <typeparam name="TEntity">该类管理的实体。</typeparam>
1313
/// <typeparam name="TKey"><typeparamref name="TEntity" /> 的主键。</typeparam>
1414
public abstract class BaseRepository<TContext, TEntity, TKey>
15-
: INavigationRepository<TEntity, TKey>, IUnitOfWork<TEntity, TKey>
15+
: INavigationRepository<TEntity, TKey>, IUnitOfWork<TEntity, TKey>, ISqlRepository<TEntity, TKey>
1616
where TContext : DbContext
1717
where TEntity : EntityBase, IEntity<TKey>, IAggregateRoot
1818
where TKey : IComparable<TKey>
@@ -185,4 +185,22 @@ private void CallBeforeUpdate()
185185
.ToList();
186186
domainEntities.ForEach(x => x.Entity.BeforeUpdate());
187187
}
188+
189+
/// <inheritdoc />
190+
public IQueryable<TEntity> SqlQuery(string sql, params object[] parameters)
191+
{
192+
return Context.Set<TEntity>().FromSqlRaw(sql, parameters);
193+
}
194+
195+
/// <inheritdoc />
196+
public IQueryable<T> SqlQuery<T>(string sql, params object[] parameters)
197+
{
198+
return Context.Database.SqlQueryRaw<T>(sql, parameters);
199+
}
200+
201+
/// <inheritdoc />
202+
public Task<int> ExecuteSqlAsync(string sql, params object[] parameters)
203+
{
204+
return Context.Database.ExecuteSqlRawAsync(sql, parameters);
205+
}
188206
}

src/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.13" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

0 commit comments

Comments
 (0)