Skip to content

Commit 4ab3be2

Browse files
authored
Merge pull request #7 from msx752/development
Development
2 parents c2a639f + 595fe29 commit 4ab3be2

File tree

7 files changed

+102
-29
lines changed

7 files changed

+102
-29
lines changed

src/SampleDotnet.RepositoryFactory/Interfaces/IRepository.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public interface IRepository : IDisposable
1717
void Update(object entity);
1818

1919
void UpdateRange(params object[] entities);
20-
21-
DbContext RefreshDbContext();
2220
}
2321

2422
public interface IRepository<TDbContext> : IRepository

src/SampleDotnet.RepositoryFactory/Repository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public IQueryable<T> AsQueryable<T>() where T : class
2828

2929
private DbSet<T> CachedContextSet<T>() where T : class
3030
{
31-
return (DbSet<T>)_cachedDbSets.GetOrAdd(typeof(T).FullName, CurrentDbContext.Set<T>());
31+
return (DbSet<T>)_cachedDbSets.GetOrAdd(typeof(T).FullName, DbContext.Set<T>());
3232
}
3333

3434
public void Delete<T>(T entity) where T : class

src/SampleDotnet.RepositoryFactory/RepositoryBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected RepositoryBase(DbContext context)
2121
this._context = context;
2222
}
2323

24-
public DbContext CurrentDbContext => _context;
24+
public DbContext DbContext => _context;
2525

2626
public virtual void Delete(object entity)
2727
{

src/SampleDotnet.RepositoryFactory/SampleDotnet.RepositoryFactory.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
<ContinuousIntegrationBuild>True</ContinuousIntegrationBuild>
3232
<EmbedUntrackedSources>True</EmbedUntrackedSources>
3333
<Copyright>Copyright 2023</Copyright>
34-
<AssemblyVersion>3.0.0.3</AssemblyVersion>
35-
<Version>3.0.0.3-alpha</Version>
34+
<AssemblyVersion>3.0.0.4</AssemblyVersion>
35+
<Version>3.0.0.4-alpha</Version>
3636
</PropertyGroup>
3737

3838
<ItemGroup>

src/SampleDotnet.RepositoryFactory/UnitOfWork.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ public async Task<bool> SaveChangesAsync(CancellationToken cancellationToken = d
7272

7373
for (int i = 0; i < cached.Length; i++)
7474
{
75-
if (_dbContextPool.TryDequeue(out var dbContext) && dbContext != null)
76-
{
77-
dbContext.ChangeTracker.AcceptAllChanges();
78-
}
75+
cached[i].ChangeTracker.AcceptAllChanges();
7976
}
8077
}
8178
catch (Exception e)

test/SampleDotnet.RepositoryFactory.Tests/Cases/DbContextDisposeTests.cs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,36 +81,49 @@ public async Task Case_Repository_Should_Not_Throw_ObjectDisposedException()
8181
});
8282

8383
using (IHost build = host.Build())
84-
//request scope
85-
using (IServiceScope requestScope = build.Services.CreateScope())
86-
using (var unitOfWork = requestScope.ServiceProvider.GetRequiredService<IUnitOfWork>())
87-
using (var cancellationTokenSource = new CancellationTokenSource())
8884
{
89-
using (var repository = unitOfWork.CreateRepository<TestApplicationDbContext>())
85+
//request scope 1
86+
using (IServiceScope requestScope = build.Services.CreateScope())
87+
using (var unitOfWork = requestScope.ServiceProvider.GetRequiredService<IUnitOfWork>())
88+
using (var cancellationTokenSource = new CancellationTokenSource())
9089
{
91-
var user1 = new TestUserEntity()
90+
using (var repository = unitOfWork.CreateRepository<TestApplicationDbContext>())
9291
{
93-
Name = "Name",
94-
Surname = "Surname",
95-
};
92+
var user1 = new TestUserEntity()
93+
{
94+
Name = "Name",
95+
Surname = "Surname",
96+
};
9697

97-
user1.CreatedAt.ShouldBeNull();
98-
await repository.InsertAsync(user1);
98+
user1.CreatedAt.ShouldBeNull();
99+
await repository.InsertAsync(user1);
99100

100-
await unitOfWork.SaveChangesAsync();
101+
await unitOfWork.SaveChangesAsync();
102+
await unitOfWork.SaveChangesAsync();
103+
}
101104
}
102105

103-
Parallel.For(1, 100, new ParallelOptions() { MaxDegreeOfParallelism = 5 }, async (i) =>
106+
//request scope 2
107+
using (IServiceScope requestScope = build.Services.CreateScope())
108+
using (var unitOfWork = requestScope.ServiceProvider.GetRequiredService<IUnitOfWork>())
109+
using (var cancellationTokenSource = new CancellationTokenSource())
104110
{
105-
using (var repository = unitOfWork.CreateRepository<TestApplicationDbContext>())
111+
Parallel.For(0, 25, new ParallelOptions() { MaxDegreeOfParallelism = 3 }, async (i) =>
106112
{
107-
var user1 = await repository.FirstOrDefaultAsync<TestUserEntity>(f => f.Surname == "Surname");
113+
using (var repository = unitOfWork.CreateRepository<TestApplicationDbContext>())
114+
{
115+
var user1 = await repository.FirstOrDefaultAsync<TestUserEntity>(f => f.Surname == "Surname");
108116

109-
user1.Name = "Name" + i.ToString("00");
117+
user1.Name = "Name" + i.ToString("00");
110118

111-
await unitOfWork.SaveChangesAsync();
112-
}
113-
});
119+
await unitOfWork.SaveChangesAsync();
120+
121+
repository.Update(user1);
122+
123+
await unitOfWork.SaveChangesAsync();
124+
}
125+
});
126+
}
114127
}
115128
}
116129
}

test/SampleDotnet.RepositoryFactory.Tests/Cases/UnitOfWorkTests.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,71 @@ await Assert.ThrowsAsync<DbUpdateException>(async () =>
429429
}
430430
}
431431

432+
[Fact]
433+
public async Task Case_UnitOfWork_Do_Not_Skip_DetectChanges()
434+
{
435+
IHostBuilder host = Host.CreateDefaultBuilder().ConfigureServices((services) =>
436+
{
437+
services.AddDbContextFactoryWithUnitOfWork<TestApplicationDbContext>(options =>
438+
{
439+
//var cnnBuilder = new SqlConnectionStringBuilder();
440+
//cnnBuilder.DataSource = "localhost,1433";
441+
//cnnBuilder.InitialCatalog = "TestApplicationDb";
442+
//cnnBuilder.TrustServerCertificate = true;
443+
//cnnBuilder.UserID = "sa";
444+
//cnnBuilder.Password = "Admin123!";
445+
//cnnBuilder.MultipleActiveResultSets = true;
446+
//cnnBuilder.ConnectRetryCount = 5;
447+
//cnnBuilder.ConnectTimeout = TimeSpan.FromMinutes(5).Seconds;
448+
//options.UseSqlServer(cnnBuilder.ToString());
449+
450+
options.UseInMemoryDatabase("Case_UnitOfWork_Do_Not_Skip_DetectChanges");
451+
options.EnableSensitiveDataLogging();
452+
options.EnableDetailedErrors();
453+
});
454+
});
455+
456+
using (IHost build = host.Build())
457+
//request scope
458+
using (IServiceScope requestScope = build.Services.CreateScope())
459+
using (var unitOfWork = requestScope.ServiceProvider.GetRequiredService<IUnitOfWork>())
460+
using (var cancellationTokenSource = new CancellationTokenSource())
461+
{
462+
using (var repository = unitOfWork.CreateRepository<TestApplicationDbContext>())
463+
{
464+
var user1 = new TestUserEntity()
465+
{
466+
Name = "Name",
467+
Surname = "Surname",
468+
};
469+
470+
user1.CreatedAt.ShouldBeNull();
471+
await repository.InsertAsync(user1);
472+
473+
await unitOfWork.SaveChangesAsync();
474+
}
475+
476+
using (var repository = unitOfWork.CreateRepository<TestApplicationDbContext>())
477+
{
478+
var user1 = await repository.FirstOrDefaultAsync<TestUserEntity>(f => f.Surname == "Surname");
479+
480+
user1.ShouldNotBeNull();
481+
user1.CreatedAt.ShouldNotBeNull();
482+
483+
await unitOfWork.SaveChangesAsync();
484+
485+
user1.Name = "NameUpdated2";
486+
487+
await unitOfWork.SaveChangesAsync();
488+
489+
user1.CreatedAt.ShouldNotBeNull();
490+
491+
var user2 = await repository.FirstOrDefaultAsync<TestUserEntity>(f => f.Name == "NameUpdated2");
492+
user2.ShouldNotBeNull();
493+
}
494+
}
495+
}
496+
432497
#region DbContext 1
433498

434499
internal class FirstDbContext : DbContext

0 commit comments

Comments
 (0)