@@ -19,6 +19,7 @@ namespace Unity.ObjectBuilder.Strategies
19
19
public class LifetimeStrategy : BuilderStrategy
20
20
{
21
21
private readonly object _genericLifetimeManagerLock = new object ( ) ;
22
+ private static readonly TransientLifetimeManager TransientManager = new TransientLifetimeManager ( ) ;
22
23
23
24
/// <summary>
24
25
/// Called during the chain of responsibility for a build operation. The
@@ -30,13 +31,13 @@ public override void PreBuildUp(IBuilderContext context)
30
31
{
31
32
if ( ( context ?? throw new ArgumentNullException ( nameof ( context ) ) ) . Existing == null )
32
33
{
33
- ILifetimePolicy lifetimePolicy = GetLifetimePolicy ( context ) ;
34
+ var lifetimePolicy = GetLifetimePolicy ( context ) ;
34
35
if ( lifetimePolicy is IRequiresRecovery recovery )
35
36
{
36
37
context . RecoveryStack . Add ( recovery ) ;
37
38
}
38
39
39
- object existing = lifetimePolicy . GetValue ( ) ;
40
+ var existing = lifetimePolicy . GetValue ( ) ;
40
41
if ( existing != null )
41
42
{
42
43
context . Existing = existing ;
@@ -69,7 +70,7 @@ private ILifetimePolicy GetLifetimePolicy(IBuilderContext context)
69
70
70
71
if ( policy == null )
71
72
{
72
- policy = new TransientLifetimeManager ( ) ;
73
+ policy = TransientManager ;
73
74
context . PersistentPolicies . Set ( policy , context . BuildKey ) ;
74
75
}
75
76
@@ -78,13 +79,12 @@ private ILifetimePolicy GetLifetimePolicy(IBuilderContext context)
78
79
79
80
private ILifetimePolicy GetLifetimePolicyForGenericType ( IBuilderContext context )
80
81
{
81
- Type typeToBuild = context . BuildKey . Type ;
82
+ var typeToBuild = context . BuildKey . Type ;
82
83
object openGenericBuildKey = new NamedTypeBuildKey ( typeToBuild . GetGenericTypeDefinition ( ) ,
83
84
context . BuildKey . Name ) ;
84
85
85
- IPolicyList factorySource ;
86
- ILifetimeFactoryPolicy factoryPolicy =
87
- context . Policies . Get < ILifetimeFactoryPolicy > ( openGenericBuildKey , out factorySource ) ;
86
+ var factoryPolicy = context . Policies
87
+ . Get < ILifetimeFactoryPolicy > ( openGenericBuildKey , out var factorySource ) ;
88
88
89
89
if ( factoryPolicy != null )
90
90
{
@@ -97,7 +97,7 @@ private ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context)
97
97
lock ( _genericLifetimeManagerLock )
98
98
{
99
99
// check whether the policy for closed-generic has been added since first checked
100
- ILifetimePolicy lifetime = factorySource . GetNoDefault < ILifetimePolicy > ( context . BuildKey , false ) ;
100
+ var lifetime = factorySource . GetNoDefault < ILifetimePolicy > ( context . BuildKey , false ) ;
101
101
if ( lifetime == null )
102
102
{
103
103
factorySource . Set ( newLifetime , context . BuildKey ) ;
0 commit comments