Skip to content

Commit 7a9ae79

Browse files
committed
Fixing sample application
1 parent 4abc37b commit 7a9ae79

4 files changed

Lines changed: 36 additions & 23 deletions

File tree

src/Strategies/LifetimeStrategy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public override void PostBuildUp(ref BuilderContext context)
8282
if (null == policy || policy is PerResolveLifetimeManager)
8383
policy = (LifetimeManager)context.Get(typeof(LifetimeManager));
8484

85-
policy?.SetValue(context.Existing, context.Lifetime);
85+
if (LifetimeManager.NoValue != context.Existing)
86+
policy?.SetValue(context.Existing, context.Lifetime);
8687
}
8788

8889
#endregion

src/UnityContainer.IUnityContainer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ bool IUnityContainer.IsRegistered(Type type, string name) => ReferenceEquals(All
233233
/// <inheritdoc />
234234
object IUnityContainer.Resolve(Type type, string name, params ResolverOverride[] overrides)
235235
{
236+
var n = type.FullName;
237+
236238
// Verify arguments
237239
if (null == type) throw new ArgumentNullException(nameof(type));
238240

src/UnityContainer.Registration.cs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -478,30 +478,23 @@ private IPolicySet GetOrAddGeneric(Type type, string name, Type definition)
478478
var collisions = 0;
479479
int hashCode;
480480
int targetBucket;
481-
var factory = false;
481+
InternalRegistration factory = null;
482482

483-
if (null != _parent)
483+
hashCode = (definition?.GetHashCode() ?? 0) & 0x7FFFFFFF;
484+
targetBucket = hashCode % _registrations.Buckets.Length;
485+
for (var j = _registrations.Buckets[targetBucket]; j >= 0; j = _registrations.Entries[j].Next)
484486
{
485-
hashCode = (definition?.GetHashCode() ?? 0) & 0x7FFFFFFF;
486-
targetBucket = hashCode % _registrations.Buckets.Length;
487-
for (var j = _registrations.Buckets[targetBucket]; j >= 0; j = _registrations.Entries[j].Next)
487+
ref var candidate = ref _registrations.Entries[j];
488+
if (candidate.HashCode != hashCode || candidate.Key != definition)
488489
{
489-
ref var candidate = ref _registrations.Entries[j];
490-
if (candidate.HashCode != hashCode || candidate.Key != definition)
491-
{
492-
continue;
493-
}
494-
495-
if (null != candidate.Value?[name])
496-
{
497-
factory = true;
498-
break;
499-
}
490+
continue;
500491
}
501492

502-
if (!factory) return _parent._getGenericRegistration(type, name, definition);
493+
if (null != (factory = (InternalRegistration)candidate.Value?[name])) break;
503494
}
504495

496+
if (null == factory && null != _parent) return _parent._getGenericRegistration(type, name, definition);
497+
505498
hashCode = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;
506499
targetBucket = hashCode % _registrations.Buckets.Length;
507500

@@ -527,7 +520,7 @@ private IPolicySet GetOrAddGeneric(Type type, string name, Type definition)
527520
_registrations.Entries[i].Value = existing;
528521
}
529522

530-
return existing.GetOrAdd(name, () => CreateRegistration(type, name));
523+
return existing.GetOrAdd(name, () => CreateRegistration(type, name, factory));
531524
}
532525

533526
if (_registrations.RequireToGrow || ListToHashCutPoint < collisions)
@@ -536,7 +529,7 @@ private IPolicySet GetOrAddGeneric(Type type, string name, Type definition)
536529
targetBucket = hashCode % _registrations.Buckets.Length;
537530
}
538531

539-
var registration = CreateRegistration(type, name);
532+
var registration = CreateRegistration(type, name, factory);
540533
ref var entry = ref _registrations.Entries[_registrations.Count];
541534
entry.HashCode = hashCode;
542535
entry.Next = _registrations.Buckets[targetBucket];

src/UnityContainer.Resolution.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using System.Linq;
54
using System.Linq.Expressions;
65
using System.Reflection;
76
using System.Runtime.CompilerServices;
87
using System.Security;
9-
using System.Text;
108
using System.Threading;
119
using System.Threading.Tasks;
1210
using Unity.Builder;
@@ -39,9 +37,28 @@ private IPolicySet GetDynamicRegistration(Type type, string name)
3937
: GetOrAddGeneric(type, name, info.GetGenericTypeDefinition());
4038
}
4139

42-
private IPolicySet CreateRegistration(Type type, string name)
40+
private IPolicySet CreateRegistration(Type type, string name, InternalRegistration factory)
4341
{
42+
var registration = new InternalRegistration(type, name);
43+
44+
if (null != factory)
45+
{
46+
registration.InjectionMembers = factory.InjectionMembers;
47+
registration.Map = factory.Map;
48+
var lifetime = factory.Get(typeof(LifetimeManager));
49+
if (lifetime is IFactoryLifetimeManager ManagerFactory)
50+
{
51+
var manager = ManagerFactory.CreateLifetimePolicy();
52+
registration.Set(typeof(LifetimeManager), manager);
53+
}
54+
}
4455

56+
registration.BuildChain = GetBuilders(type, registration);
57+
return registration;
58+
}
59+
60+
private IPolicySet CreateRegistration(Type type, string name)
61+
{
4562
var registration = new InternalRegistration(type, name);
4663

4764
if (type.GetTypeInfo().IsGenericType)

0 commit comments

Comments
 (0)