You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: AGENTS.md
+233-3Lines changed: 233 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2471,6 +2471,236 @@ To run the above code, the following NuGet packages must be added:
2471
2471
2472
2472
## Scope
2473
2473
2474
+
Demonstrates scoped lifetime with `Hint(Hint.ScopeFactory, "on")` where scopes are represented by generated `Scope` objects created via `CreateScope()`.
2475
+
2476
+
```c#
2477
+
usingShouldly;
2478
+
usingPure.DI;
2479
+
usingstaticPure.DI.Lifetime;
2480
+
2481
+
varcomposition=newComposition(desc: "Checkout");
2482
+
IRequestContextctx1;
2483
+
IRequestContextctx2;
2484
+
2485
+
// Scope #1
2486
+
using (varscope1=composition.NewScope)
2487
+
{
2488
+
varcheckout11=scope1.Checkout;
2489
+
varcheckout12=scope1.Checkout;
2490
+
ctx1=checkout11.Context;
2491
+
2492
+
// Same request => same scoped instance
2493
+
ctx1.ShouldBe(checkout12.Context);
2494
+
ctx1.IsDisposed.ShouldBeFalse();
2495
+
}
2496
+
2497
+
// End of request #1 => scoped instance is disposed
2498
+
ctx1.IsDisposed.ShouldBeTrue();
2499
+
2500
+
// Request #2
2501
+
using (varscope1=composition.NewScope)
2502
+
{
2503
+
varcheckout2=scope1.Checkout;
2504
+
ctx2=checkout2.Context;
2505
+
}
2506
+
2507
+
// Different request => different scoped instance
2508
+
ctx1.ShouldNotBe(ctx2);
2509
+
2510
+
// End of request #2 => scoped instance is disposed
2511
+
ctx2.IsDisposed.ShouldBeTrue();
2512
+
2513
+
interfaceIIdGenerator
2514
+
{
2515
+
GuidGenerate();
2516
+
}
2517
+
2518
+
classIdGenerator : IIdGenerator
2519
+
{
2520
+
publicGuidGenerate() =>Guid.NewGuid();
2521
+
}
2522
+
2523
+
interfaceIRequestContext
2524
+
{
2525
+
GuidCorrelationId { get; }
2526
+
2527
+
boolIsDisposed { get; }
2528
+
}
2529
+
2530
+
// Typically: DbContext / UnitOfWork / RequestTelemetry / Activity, etc.
>This approach is useful when you need runtime scope creation without deriving a child composition type.
2593
+
2594
+
## Scope factory
2595
+
2596
+
Demonstrates scoped lifetime with `Hint(Hint.ScopeFactory, "on")` where scopes are represented by generated `Scope` objects created via `CreateScope()`.
2597
+
2598
+
```c#
2599
+
usingShouldly;
2600
+
usingPure.DI;
2601
+
usingstaticPure.DI.Lifetime;
2602
+
2603
+
varcomposition=newComposition();
2604
+
IRequestContextctx1;
2605
+
IRequestContextctx2;
2606
+
2607
+
// Request #1
2608
+
using (varrequest1=composition.CreateScope())
2609
+
{
2610
+
varcheckout11=request1.RequestRoot;
2611
+
varcheckout12=request1.RequestRoot;
2612
+
ctx1=checkout11.Context;
2613
+
2614
+
// Same request => same scoped instance
2615
+
ctx1.ShouldBe(checkout12.Context);
2616
+
ctx1.IsDisposed.ShouldBeFalse();
2617
+
}
2618
+
2619
+
// End of request #1 => scoped instance is disposed
2620
+
ctx1.IsDisposed.ShouldBeTrue();
2621
+
2622
+
// Request #2
2623
+
using (varrequest2=composition.CreateScope())
2624
+
{
2625
+
varcheckout2=request2.RequestRoot;
2626
+
ctx2=checkout2.Context;
2627
+
}
2628
+
2629
+
// Different request => different scoped instance
2630
+
ctx1.ShouldNotBe(ctx2);
2631
+
2632
+
// End of request #2 => scoped instance is disposed
2633
+
ctx2.IsDisposed.ShouldBeTrue();
2634
+
2635
+
interfaceIIdGenerator
2636
+
{
2637
+
GuidGenerate();
2638
+
}
2639
+
2640
+
classIdGenerator : IIdGenerator
2641
+
{
2642
+
publicGuidGenerate() =>Guid.NewGuid();
2643
+
}
2644
+
2645
+
interfaceIRequestContext
2646
+
{
2647
+
GuidCorrelationId { get; }
2648
+
2649
+
boolIsDisposed { get; }
2650
+
}
2651
+
2652
+
// Typically: DbContext / UnitOfWork / RequestTelemetry / Activity, etc.
0 commit comments