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
$h=The `Scoped` lifetime ensures that there will be a single instance of the dependency for each scope.
5
+
$h=Demonstrates scoped lifetime with `Hint(Hint.ScopeFactory, "on")` where scopes are represented by generated `Scope` objects created via `CreateScope()`.
6
6
$f=>[!NOTE]
7
-
$f=>`Scoped` lifetime is essential for request-based or session-based scenarios where instances should be shared within a scope but isolated between scopes.
7
+
$f=>This approach is useful when you need runtime scope creation without deriving a child composition type.
8
8
$r=Shouldly
9
9
*/
10
10
@@ -33,45 +33,53 @@ public class Scenario
33
33
publicvoidRun()
34
34
{
35
35
// {
36
-
varcomposition=newComposition();
37
-
varapp=composition.AppRoot;
36
+
varcomposition=newComposition(desc:"Checkout");
37
+
IRequestContextctx1;
38
+
IRequestContextctx2;
38
39
39
-
// Real-world analogy:
40
-
// each HTTP request (or message consumer handling) creates its own scope.
41
-
// Scoped services live exactly as long as the request is being processed.
40
+
// Scope #1
41
+
using(varscope1=composition.NewScope)
42
+
{
43
+
varcheckout11=scope1.Checkout;
44
+
varcheckout12=scope1.Checkout;
45
+
ctx1=checkout11.Context;
42
46
43
-
// Request #1
44
-
varrequest1=app.CreateRequestScope();
45
-
varcheckout1=request1.RequestRoot;
47
+
// Same request => same scoped instance
48
+
ctx1.ShouldBe(checkout12.Context);
49
+
ctx1.IsDisposed.ShouldBeFalse();
50
+
}
46
51
47
-
varctx11=checkout1.Context;
48
-
varctx12=checkout1.Context;
49
-
50
-
// Same request => same scoped instance
51
-
ctx11.ShouldBe(ctx12);
52
+
// End of request #1 => scoped instance is disposed
53
+
ctx1.IsDisposed.ShouldBeTrue();
52
54
53
55
// Request #2
54
-
varrequest2=app.CreateRequestScope();
55
-
varcheckout2=request2.RequestRoot;
56
-
57
-
varctx2=checkout2.Context;
56
+
using(varscope1=composition.NewScope)
57
+
{
58
+
varcheckout2=scope1.Checkout;
59
+
ctx2=checkout2.Context;
60
+
}
58
61
59
62
// Different request => different scoped instance
60
-
ctx11.ShouldNotBe(ctx2);
63
+
ctx1.ShouldNotBe(ctx2);
61
64
62
-
// End of Request #1 => scoped instance is disposed
63
-
request1.Dispose();
64
-
ctx11.IsDisposed.ShouldBeTrue();
65
-
66
-
// End of Request #2 => scoped instance is disposed
67
-
request2.Dispose();
65
+
// End of request #2 => scoped instance is disposed
68
66
ctx2.IsDisposed.ShouldBeTrue();
69
67
// }
70
68
composition.SaveClassDiagram();
71
69
}
72
70
}
73
71
74
72
// {
73
+
interfaceIIdGenerator
74
+
{
75
+
GuidGenerate();
76
+
}
77
+
78
+
classIdGenerator:IIdGenerator
79
+
{
80
+
publicGuidGenerate()=>Guid.NewGuid();
81
+
}
82
+
75
83
interfaceIRequestContext
76
84
{
77
85
GuidCorrelationId{get;}
@@ -80,9 +88,10 @@ interface IRequestContext
80
88
}
81
89
82
90
// Typically: DbContext / UnitOfWork / RequestTelemetry / Activity, etc.
$h=The `Scoped` lifetime ensures that there will be a single instance of the dependency for each scope.
6
+
$f=>[!NOTE]
7
+
$f=>`Scoped` lifetime is essential for request-based or session-based scenarios where instances should be shared within a scope but isolated between scopes.
0 commit comments