From d2b311a1f9ecec61a0523485766c06de896d3d88 Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Thu, 20 Dec 2018 17:53:39 -0500 Subject: [PATCH] Optimization Method | Mean | Error | StdDev | Median | ---------------- |----------:|----------:|-----------:|----------:| IUnityContainer | 70.14 ns | 2.644 ns | 3.2472 ns | 69.36 ns | Container | 69.50 ns | 1.439 ns | 1.8711 ns | 69.21 ns | Unregistered | 92.20 ns | 1.474 ns | 1.1504 ns | 92.00 ns | Transient | 99.68 ns | 1.016 ns | 0.7936 ns | 99.90 ns | Mapping | 193.21 ns | 4.370 ns | 12.1832 ns | 188.32 ns | Array | 776.40 ns | 14.911 ns | 12.4516 ns | 776.54 ns | Enumerable | 904.25 ns | 17.715 ns | 16.5706 ns | 897.31 ns | --- src/Strategies/LifetimeStrategy.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Strategies/LifetimeStrategy.cs b/src/Strategies/LifetimeStrategy.cs index 13f4cd6c..79ec3a98 100644 --- a/src/Strategies/LifetimeStrategy.cs +++ b/src/Strategies/LifetimeStrategy.cs @@ -77,9 +77,16 @@ namespace Unity.Strategies public override void PostBuildUp(ref BuilderContext context) { - var lifetimePolicy = context.Get(context.Registration.Type, - context.Registration.Name); - lifetimePolicy?.SetValue(context.Existing, context.Lifetime); + LifetimeManager policy = null; + + if (context.Registration is ContainerRegistration registration) + policy = registration.LifetimeManager; + + if (null == policy || policy is PerResolveLifetimeManager) + policy = context.Get(context.Registration.Type, + context.Registration.Name); + + policy?.SetValue(context.Existing, context.Lifetime); } #endregion