Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 022a410

Browse files
cleanup
1 parent f459f2c commit 022a410

4 files changed

Lines changed: 22 additions & 18 deletions

File tree

DbContextScope/DbContextScopeFactoryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class DbContextScopeFactoryExtensions
1616
*
1717
* readonly: ((DbContext)invocation.InvocationTarget).ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking
1818
*/
19-
public static TDbContext Create<TDbContext>(this IDbContextScopeFactory self, DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting) where TDbContext : DbContext
19+
public static TDbContext Open<TDbContext>(this IDbContextScopeFactory self, DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting) where TDbContext : DbContext
2020
{
2121
/*
2222
* 1. create DbContextScope

DbContextScope/Implementations/DbContextInterceptor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ void IAsyncInterceptor.InterceptSynchronous(IInvocation invocation)
4141
return;
4242
}
4343

44-
throw new NotImplementedException($"The method '{nameof(DbContext)}.{invocation.Method.Name}' was not chosen to be proxied!");
44+
throw new ArgumentOutOfRangeException($"The method '{nameof(DbContext)}.{invocation.Method.Name}' was not chosen to be proxied!");
4545
}
4646

4747
void IAsyncInterceptor.InterceptAsynchronous(IInvocation invocation)
4848
{
49-
throw new NotImplementedException($"The async method '{nameof(DbContext)}.{invocation.Method.Name}' was not chosen to be proxied!");
49+
throw new ArgumentOutOfRangeException($"The async method '{nameof(DbContext)}.{invocation.Method.Name}' was not chosen to be proxied!");
5050
}
5151

5252
void IAsyncInterceptor.InterceptAsynchronous<TResult>(IInvocation invocation)
@@ -74,7 +74,7 @@ void IAsyncInterceptor.InterceptAsynchronous<TResult>(IInvocation invocation)
7474
return;
7575
}
7676

77-
throw new NotImplementedException($"The async method '{nameof(DbContext)}.{invocation.Method.Name}' was not chosen to be proxied!");
77+
throw new ArgumentOutOfRangeException($"The async method '{nameof(DbContext)}.{invocation.Method.Name}' was not chosen to be proxied!");
7878
}
7979

8080
private async Task<int> saveChangesAndUpdateParentScopeAsync(DetectModifiedEntitiesAndUpdateParentScope parentUpdater, CancellationToken cancellationToken = default(CancellationToken))

Demo/MemoryAmbientDbContextFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public TDbContext CreateDbContext<TDbContext>() where TDbContext : DbContext
1919
return new UserManagementDbContext(config.Options) as TDbContext;
2020
}
2121

22-
throw new NotImplementedException(typeof(TDbContext).Name);
22+
throw new ArgumentOutOfRangeException(typeof(TDbContext).Name);
2323
}
2424
}
2525
}

Tests/DbContextScopeFactoryExtensionsFixture.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ namespace DbContextScope.Tests
1616
public class DbContextScopeFactoryExtensionsFixture
1717
{
1818
[TestMethod]
19-
public void Create_should_create_a_proxy()
19+
public void Open_should_create_a_proxy()
2020
{
2121
// arrange
2222
var counter = new CallCounter();
2323

2424
// act
25-
var dummyDbContext = counter.Create<BlockingDummyDbContext>();
25+
var dummyDbContext = counter.Open<BlockingDummyDbContext>();
2626

2727
// assert
2828
var dummyDbContextTypeName = dummyDbContext.GetType().Name;
@@ -34,7 +34,7 @@ public void Dispose_of_proxy_should_be_forwarded()
3434
{
3535
// arrange
3636
var counter = new CallCounter();
37-
var dummyDbContext = counter.Create<BlockingDummyDbContext>();
37+
var dummyDbContext = counter.Open<BlockingDummyDbContext>();
3838

3939
// act
4040
dummyDbContext.Dispose();
@@ -48,7 +48,7 @@ public void SaveChanges_of_proxy_should_be_forwarded()
4848
{
4949
// arrange
5050
var counter = new CallCounter();
51-
var dummyDbContext = counter.Create<BlockingDummyDbContext>();
51+
var dummyDbContext = counter.Open<BlockingDummyDbContext>();
5252

5353
// act
5454
var changes = dummyDbContext.SaveChanges();
@@ -63,7 +63,7 @@ public void SaveChanges_with_bool_of_proxy_should_be_forwarded()
6363
{
6464
// arrange
6565
var counter = new CallCounter();
66-
var dummyDbContext = counter.Create<BlockingDummyDbContext>();
66+
var dummyDbContext = counter.Open<BlockingDummyDbContext>();
6767

6868
// act
6969
var changes = dummyDbContext.SaveChanges(true);
@@ -78,7 +78,7 @@ public async Task SaveChangesAsync_of_proxy_should_be_forwarded()
7878
{
7979
// arrange
8080
var counter = new CallCounter();
81-
var dummyDbContext = counter.Create<BlockingDummyDbContext>();
81+
var dummyDbContext = counter.Open<BlockingDummyDbContext>();
8282

8383
// act
8484
var changes = await dummyDbContext.SaveChangesAsync();
@@ -94,7 +94,7 @@ public async Task SaveChangesAsync_with_bool_of_proxy_should_be_forwarded()
9494
{
9595
// arrange
9696
var counter = new CallCounter();
97-
var dummyDbContext = counter.Create<BlockingDummyDbContext>();
97+
var dummyDbContext = counter.Open<BlockingDummyDbContext>();
9898

9999
// act
100100
var changes = await dummyDbContext.SaveChangesAsync(true);
@@ -110,7 +110,7 @@ public async Task SaveChangesAsync_with_cancellationToken_of_proxy_should_be_for
110110
{
111111
// arrange
112112
var counter = new CallCounter();
113-
var dummyDbContext = counter.Create<BlockingDummyDbContext>();
113+
var dummyDbContext = counter.Open<BlockingDummyDbContext>();
114114

115115
// act
116116
var changes = await dummyDbContext.SaveChangesAsync(new CancellationToken(false));
@@ -125,7 +125,7 @@ public async Task SaveChangesAsync_with_bool_and_cancellationToken_of_proxy_shou
125125
{
126126
// arrange
127127
var counter = new CallCounter();
128-
var dummyDbContext = counter.Create<BlockingDummyDbContext>();
128+
var dummyDbContext = counter.Open<BlockingDummyDbContext>();
129129

130130
// act
131131
var changes = await dummyDbContext.SaveChangesAsync(true, new CancellationToken(false));
@@ -178,7 +178,7 @@ public void Add_entity_should_call_refesh_entities_in_parent_scope()
178178
{
179179
// arrange
180180
var counter = new CallCounter();
181-
var dummyDbContext = counter.Create<DummyDbContext>();
181+
var dummyDbContext = counter.Open<DummyDbContext>();
182182
dummyDbContext.DummyEntities.Add(new DummyEntity());
183183

184184
// act
@@ -199,7 +199,7 @@ public void Update_entity_should_call_refesh_entities_in_parent_scope()
199199
baseDummyDbContext.SaveChanges();
200200

201201
var counter = new CallCounter();
202-
var dummyDbContext = counter.Create<DummyDbContext>();
202+
var dummyDbContext = counter.Open<DummyDbContext>();
203203
dummyDbContext.ChangeTracker.Tracked += (sender, args) => Console.WriteLine("Tracked: " + args.Entry.GetType());
204204
dummyDbContext.ChangeTracker.StateChanged += (sender, args) => Console.WriteLine("StateChanged: " + args.Entry.GetType());
205205
var savedDummyEntity = dummyDbContext.DummyEntities.Find(dummyEntity.Id);
@@ -218,7 +218,7 @@ public async Task Add_entity_should_call_refesh_entities_async_in_parent_scope()
218218
{
219219
// arrange
220220
var counter = new CallCounter();
221-
var dummyDbContext = counter.Create<DummyDbContext>();
221+
var dummyDbContext = counter.Open<DummyDbContext>();
222222
dummyDbContext.Add(new DummyEntity());
223223

224224
// act
@@ -239,7 +239,7 @@ public async Task Update_entity_should_call_refesh_entities_async_in_parent_scop
239239
await baseDummyDbContext.SaveChangesAsync();
240240

241241
var counter = new CallCounter();
242-
var dummyDbContext = counter.Create<DummyDbContext>();
242+
var dummyDbContext = counter.Open<DummyDbContext>();
243243
dummyDbContext.ChangeTracker.Tracked += (sender, args) => Console.WriteLine("Tracked: " + args.Entry.GetType());
244244
dummyDbContext.ChangeTracker.StateChanged += (sender, args) => Console.WriteLine("StateChanged: " + args.Entry.GetType());
245245
var savedDummyEntity = await dummyDbContext.DummyEntities.FindAsync(dummyEntity.Id);
@@ -253,6 +253,8 @@ public async Task Update_entity_should_call_refesh_entities_async_in_parent_scop
253253
Assert.AreEqual(1, counter.CalledMethods.Count(cm => cm == "SaveChangesAsync(CancellationToken)"), counter.ReportCalledMethods());
254254
}
255255

256+
#region CUT
257+
256258
private class CallCounter : IDbContextScopeFactory, IDbContextScope
257259
{
258260
public readonly List<string> CalledMethods = new List<string>();
@@ -401,5 +403,7 @@ public override void Dispose()
401403
throw new AssertFailedException("Dispose() is not allowed to be called yet a proxy should intercept it.");
402404
}
403405
}
406+
407+
#endregion
404408
}
405409
}

0 commit comments

Comments
 (0)