Skip to content

Commit 3e34402

Browse files
committed
Fixes #18
1 parent 91cd199 commit 3e34402

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/ObjectBuilder/Strategies/LifetimeStrategy.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Unity.ObjectBuilder.Strategies
1919
public class LifetimeStrategy : BuilderStrategy
2020
{
2121
private readonly object _genericLifetimeManagerLock = new object();
22+
private static readonly TransientLifetimeManager TransientManager = new TransientLifetimeManager();
2223

2324
/// <summary>
2425
/// Called during the chain of responsibility for a build operation. The
@@ -30,13 +31,13 @@ public override void PreBuildUp(IBuilderContext context)
3031
{
3132
if ((context ?? throw new ArgumentNullException(nameof(context))).Existing == null)
3233
{
33-
ILifetimePolicy lifetimePolicy = GetLifetimePolicy(context);
34+
var lifetimePolicy = GetLifetimePolicy(context);
3435
if (lifetimePolicy is IRequiresRecovery recovery)
3536
{
3637
context.RecoveryStack.Add(recovery);
3738
}
3839

39-
object existing = lifetimePolicy.GetValue();
40+
var existing = lifetimePolicy.GetValue();
4041
if (existing != null)
4142
{
4243
context.Existing = existing;
@@ -69,7 +70,7 @@ private ILifetimePolicy GetLifetimePolicy(IBuilderContext context)
6970

7071
if (policy == null)
7172
{
72-
policy = new TransientLifetimeManager();
73+
policy = TransientManager;
7374
context.PersistentPolicies.Set(policy, context.BuildKey);
7475
}
7576

@@ -78,13 +79,12 @@ private ILifetimePolicy GetLifetimePolicy(IBuilderContext context)
7879

7980
private ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context)
8081
{
81-
Type typeToBuild = context.BuildKey.Type;
82+
var typeToBuild = context.BuildKey.Type;
8283
object openGenericBuildKey = new NamedTypeBuildKey(typeToBuild.GetGenericTypeDefinition(),
8384
context.BuildKey.Name);
8485

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);
8888

8989
if (factoryPolicy != null)
9090
{
@@ -97,7 +97,7 @@ private ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context)
9797
lock (_genericLifetimeManagerLock)
9898
{
9999
// 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);
101101
if (lifetime == null)
102102
{
103103
factorySource.Set(newLifetime, context.BuildKey);

0 commit comments

Comments
 (0)