-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#125) Bulk Insert/Update/Delete/Merge
- Loading branch information
1 parent
f753eb8
commit 506d5fd
Showing
57 changed files
with
752 additions
and
1,401 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/Microservices/Common/ClassifiedAds.Infrastructure/Persistence/DbContextRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using ClassifiedAds.CrossCuttingConcerns.OS; | ||
using ClassifiedAds.Domain.Entities; | ||
using ClassifiedAds.Domain.Repositories; | ||
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkDelete; | ||
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkInsert; | ||
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkMerge; | ||
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkUpdate; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.Infrastructure.Persistence | ||
{ | ||
public class DbContextRepository<TDbContext, TEntity, TKey> : IRepository<TEntity, TKey> | ||
where TEntity : AggregateRoot<TKey> | ||
where TDbContext : DbContext, IUnitOfWork | ||
{ | ||
private readonly TDbContext _dbContext; | ||
private readonly IDateTimeProvider _dateTimeProvider; | ||
|
||
protected DbSet<TEntity> DbSet => _dbContext.Set<TEntity>(); | ||
|
||
public IUnitOfWork UnitOfWork | ||
{ | ||
get | ||
{ | ||
return _dbContext; | ||
} | ||
} | ||
|
||
public DbContextRepository(TDbContext dbContext, IDateTimeProvider dateTimeProvider) | ||
{ | ||
_dbContext = dbContext; | ||
_dateTimeProvider = dateTimeProvider; | ||
} | ||
|
||
public async Task AddOrUpdateAsync(TEntity entity, CancellationToken cancellationToken = default) | ||
{ | ||
if (entity.Id.Equals(default(TKey))) | ||
{ | ||
entity.CreatedDateTime = _dateTimeProvider.OffsetNow; | ||
await DbSet.AddAsync(entity, cancellationToken); | ||
} | ||
else | ||
{ | ||
entity.UpdatedDateTime = _dateTimeProvider.OffsetNow; | ||
} | ||
} | ||
|
||
public void Delete(TEntity entity) | ||
{ | ||
DbSet.Remove(entity); | ||
} | ||
|
||
public IQueryable<TEntity> GetAll() | ||
{ | ||
return _dbContext.Set<TEntity>(); | ||
} | ||
|
||
public Task<T1> FirstOrDefaultAsync<T1>(IQueryable<T1> query) | ||
{ | ||
return query.FirstOrDefaultAsync(); | ||
} | ||
|
||
public Task<T1> SingleOrDefaultAsync<T1>(IQueryable<T1> query) | ||
{ | ||
return query.SingleOrDefaultAsync(); | ||
} | ||
|
||
public Task<List<T1>> ToListAsync<T1>(IQueryable<T1> query) | ||
{ | ||
return query.ToListAsync(); | ||
} | ||
|
||
public void BulkInsert(IEnumerable<TEntity> entities, Expression<Func<TEntity, object>> columnNamesSelector) | ||
{ | ||
_dbContext.BulkInsert(entities, columnNamesSelector); | ||
} | ||
|
||
public void BulkInsert(IEnumerable<TEntity> entities, Expression<Func<TEntity, object>> columnNamesSelector, Expression<Func<TEntity, object>> idSelector) | ||
{ | ||
_dbContext.BulkInsert(entities, columnNamesSelector, idSelector); | ||
} | ||
|
||
public void BulkUpdate(IList<TEntity> data, Expression<Func<TEntity, object>> idSelector, Expression<Func<TEntity, object>> columnNamesSelector) | ||
{ | ||
_dbContext.BulkUpdate(data, idSelector, columnNamesSelector); | ||
} | ||
|
||
public void BulkDelete(IList<TEntity> data, Expression<Func<TEntity, object>> idSelector) | ||
{ | ||
_dbContext.BulkDelete(data, idSelector); | ||
} | ||
|
||
public void BulkMerge(IEnumerable<TEntity> data, Expression<Func<TEntity, object>> idSelector, Expression<Func<TEntity, object>> updateColumnNamesSelector, Expression<Func<TEntity, object>> insertColumnNamesSelector) | ||
{ | ||
_dbContext.BulkMerge(data, idSelector, updateColumnNamesSelector, insertColumnNamesSelector); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Microservices/Common/ClassifiedAds.Infrastructure/Persistence/DbContextUnitOfWork.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using ClassifiedAds.Domain.Repositories; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using System; | ||
using System.Data; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.Infrastructure.Persistence | ||
{ | ||
public class DbContextUnitOfWork<TDbContext> : DbContext, IUnitOfWork | ||
where TDbContext : DbContext | ||
{ | ||
private IDbContextTransaction _dbContextTransaction; | ||
|
||
public DbContextUnitOfWork(DbContextOptions<TDbContext> options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public IDisposable BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) | ||
{ | ||
_dbContextTransaction = Database.BeginTransaction(isolationLevel); | ||
return _dbContextTransaction; | ||
} | ||
|
||
public async Task<IDisposable> BeginTransactionAsync(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, CancellationToken cancellationToken = default) | ||
{ | ||
_dbContextTransaction = await Database.BeginTransactionAsync(isolationLevel, cancellationToken); | ||
return _dbContextTransaction; | ||
} | ||
|
||
public void CommitTransaction() | ||
{ | ||
_dbContextTransaction.Commit(); | ||
} | ||
|
||
public async Task CommitTransactionAsync(CancellationToken cancellationToken = default) | ||
{ | ||
await _dbContextTransaction.CommitAsync(cancellationToken); | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder builder) | ||
{ | ||
base.OnModelCreating(builder); | ||
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); | ||
} | ||
} | ||
} |
40 changes: 22 additions & 18 deletions
40
...s.AuditLog/ClassifiedAds.Services.AuditLog.Api/ClassifiedAds.Services.AuditLog.Api.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<CodeAnalysisRuleSet>ClassifiedAds.ruleset</CodeAnalysisRuleSet> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<CodeAnalysisRuleSet>ClassifiedAds.ruleset</CodeAnalysisRuleSet> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Common\ClassifiedAds.Application\ClassifiedAds.Application.csproj" /> | ||
<ProjectReference Include="..\..\Common\ClassifiedAds.CrossCuttingConcerns\ClassifiedAds.CrossCuttingConcerns.csproj" /> | ||
<ProjectReference Include="..\..\Common\ClassifiedAds.Domain\ClassifiedAds.Domain.csproj" /> | ||
<ProjectReference Include="..\..\Common\ClassifiedAds.Infrastructure\ClassifiedAds.Infrastructure.csproj" /> | ||
<ProjectReference Include="..\ClassifiedAds.Services.AuditLog\ClassifiedAds.Services.AuditLog.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\Common\ClassifiedAds.Application\ClassifiedAds.Application.csproj" /> | ||
<ProjectReference Include="..\..\Common\ClassifiedAds.CrossCuttingConcerns\ClassifiedAds.CrossCuttingConcerns.csproj" /> | ||
<ProjectReference Include="..\..\Common\ClassifiedAds.Domain\ClassifiedAds.Domain.csproj" /> | ||
<ProjectReference Include="..\..\Common\ClassifiedAds.Infrastructure\ClassifiedAds.Infrastructure.csproj" /> | ||
<ProjectReference Include="..\ClassifiedAds.Services.AuditLog\ClassifiedAds.Services.AuditLog.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 2 additions & 31 deletions
33
...vices/Services.AuditLog/ClassifiedAds.Services.AuditLog/Repositories/AuditLogDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.