Skip to content

Commit f2fe057

Browse files
committed
Releasing v5.8.2
1 parent 95778a8 commit f2fe057

File tree

6 files changed

+62
-82
lines changed

6 files changed

+62
-82
lines changed

package.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<Version>5.8.1</Version>
4+
<Version>5.8.2</Version>
55
<PackageReleaseNotes>This package is distributed as .NET Standard 1.0, .NET 4.0, 4.5, 4.6, 4.7 package.</PackageReleaseNotes>
66
</PropertyGroup>
77

src/Strategies/BuildKeyMappingStrategy.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override void PreBuildUp(IBuilderContext context)
3737

3838
context.BuildKey = policy.Map(context.BuildKey, context);
3939

40-
if (!policy.RequireBuild && context.Container.IsRegistered(context.BuildKey.Type, context.BuildKey.Name))
40+
if (!policy.RequireBuild && ((UnityContainer)context.Container).RegistrationExists(context.BuildKey.Type, context.BuildKey.Name))
4141
{
4242
context.Registration.Set(typeof(IBuildPlanPolicy),
4343
new DynamicMethodBuildPlan(c =>
@@ -48,12 +48,6 @@ public override void PreBuildUp(IBuilderContext context)
4848
c.Existing = c.ChildContext.Existing;
4949
c.BuildComplete = null != context.Existing;
5050

51-
//if (((InternalRegistration)context.Registration).EnableOptimization)
52-
//{
53-
// var plan = c.ChildContext.Registration.Get(typeof(IBuildPlanPolicy));
54-
// if (null != plan) context.Registration.Set(typeof(IBuildPlanPolicy), plan);
55-
//}
56-
5751
((BuilderContext)c).ChildContext = null;
5852
}));
5953
}
@@ -90,24 +84,24 @@ public override bool RequiredToBuildType(IUnityContainer container, INamedType n
9084
switch (namedType)
9185
{
9286
case ContainerRegistration registration:
93-
return AnalysStaticRegistration(container, registration, injectionMembers);
87+
return AnaliseStaticRegistration(registration, injectionMembers);
9488

9589
case InternalRegistration registration:
96-
return AnalysDynamicRegistration(container, registration);
90+
return AnaliseDynamicRegistration(registration);
9791

9892
default:
9993
return false;
10094
}
10195
}
10296

103-
private bool AnalysStaticRegistration(IUnityContainer container, ContainerRegistration registration, params InjectionMember[] injectionMembers)
97+
private bool AnaliseStaticRegistration(ContainerRegistration registration, params InjectionMember[] injectionMembers)
10498
{
105-
// Validate input
99+
// Validate input
106100
if (null == registration.MappedToType || registration.RegisteredType == registration.MappedToType) return false;
107101

108102
// Require Re-Resolve if no injectors specified
109103
var buildRequired = registration.LifetimeManager is IRequireBuildUpPolicy ||
110-
(null == injectionMembers ? false : injectionMembers.Any(m => m.BuildRequired));
104+
(injectionMembers?.Any(m => m.BuildRequired) ?? false);
111105

112106
// Set mapping policy
113107
var policy = registration.RegisteredType.GetTypeInfo().IsGenericTypeDefinition &&
@@ -119,7 +113,7 @@ private bool AnalysStaticRegistration(IUnityContainer container, ContainerRegist
119113
return true;
120114
}
121115

122-
private bool AnalysDynamicRegistration(IUnityContainer container, InternalRegistration registration)
116+
private bool AnaliseDynamicRegistration(InternalRegistration registration)
123117
{
124118
return registration.Type.GetTypeInfo().IsGenericType;
125119
}

src/UnityContainer.Implementation.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Globalization;
45
using System.Linq;
56
using System.Reflection;
@@ -66,16 +67,18 @@ public partial class UnityContainer
6667
internal BuilderStrategy[] _buildChain;
6768

6869
// Methods
69-
internal Func<Type, string, bool> IsTypeRegistered;
7070
internal Func<Type, string, IPolicySet> GetRegistration;
71-
internal Func<IBuilderContext, object> BuilUpPipeline;
71+
internal Func<IBuilderContext, object> BuildUpPipeline;
7272
internal Func<INamedType, IPolicySet> Register;
7373
internal GetPolicyDelegate GetPolicy;
7474
internal SetPolicyDelegate SetPolicy;
7575
internal ClearPolicyDelegate ClearPolicy;
76+
internal Func<Type, string, bool> RegistrationExists;
7677

7778
private Func<Type, string, IPolicySet> _get;
7879
private Func<Type, string, Type, IPolicySet> _getGenericRegistration;
80+
private Func<Type, bool> _isTypeExplicitlyRegistered;
81+
private Func<Type, string, bool> _isExplicitlyRegistered;
7982

8083
#endregion
8184

@@ -101,17 +104,18 @@ public UnityContainer()
101104
_buildPlanStrategies = new StagedStrategyChain<BuilderStrategy, BuilderStage>();
102105

103106
// Methods
104-
BuilUpPipeline = ThrowingBuildUp;
105-
IsTypeRegistered = IsTypeRegisteredLocally;
107+
_get = Get;
108+
_getGenericRegistration = GetOrAddGeneric;
109+
_isExplicitlyRegistered = IsExplicitlyRegisteredLocally;
110+
_isTypeExplicitlyRegistered = IsTypeTypeExplicitlyRegisteredLocally;
111+
112+
BuildUpPipeline = ThrowingBuildUp;
106113
GetRegistration = GetOrAdd;
107114
Register = AddOrUpdate;
108115
GetPolicy = Get;
109116
SetPolicy = Set;
110117
ClearPolicy = Clear;
111-
112-
_get = Get;
113-
_getGenericRegistration = GetOrAddGeneric;
114-
118+
RegistrationExists = (type, name) => null != _get(type, name);
115119

116120
// TODO: Initialize disposables
117121
_lifetimeContainer.Add(_strategies);
@@ -165,16 +169,18 @@ private UnityContainer(UnityContainer parent)
165169
_root = _parent._root;
166170

167171
// Methods
168-
BuilUpPipeline = _parent.BuilUpPipeline;
169-
IsTypeRegistered = _parent.IsTypeRegistered;
172+
_get = _parent._get;
173+
_getGenericRegistration = _parent._getGenericRegistration;
174+
_isExplicitlyRegistered = _parent._isExplicitlyRegistered;
175+
_isTypeExplicitlyRegistered = _parent._isTypeExplicitlyRegistered;
176+
177+
BuildUpPipeline = _parent.BuildUpPipeline;
170178
GetRegistration = _parent.GetRegistration;
171179
Register = CreateAndSetOrUpdate;
172180
GetPolicy = parent.GetPolicy;
173181
SetPolicy = CreateAndSetPolicy;
174182
ClearPolicy = delegate { };
175-
176-
_get = _parent._get;
177-
_getGenericRegistration = _parent._getGenericRegistration;
183+
RegistrationExists = (type, name) => null != _get(type, name);
178184

179185
// Strategies
180186
_strategies = _parent._strategies;
@@ -233,16 +239,17 @@ private IPolicySet CreateAndSetOrUpdate(INamedType registration)
233239
private void SetupChildContainerBehaviors()
234240
{
235241
_registrations = new HashRegistry<Type, IRegistry<string, IPolicySet>>(ContainerInitialCapacity);
236-
IsTypeRegistered = IsTypeRegisteredLocally;
237242
Register = AddOrUpdate;
238243
GetPolicy = Get;
239244
SetPolicy = Set;
240245
ClearPolicy = Clear;
241246

242247
GetRegistration = GetDynamicRegistration;
243248

244-
_get = GetChained;
249+
_get = (type, name) => Get(type, name) ?? _parent._get(type, name);
245250
_getGenericRegistration = GetOrAddGeneric;
251+
_isTypeExplicitlyRegistered = IsTypeTypeExplicitlyRegisteredLocally;
252+
_isExplicitlyRegistered = IsExplicitlyRegisteredLocally;
246253
}
247254

248255
private void OnStrategiesChanged(object sender, EventArgs e)
@@ -294,6 +301,7 @@ private IList<BuilderStrategy> GetBuilders(InternalRegistration registration)
294301
return chain;
295302
}
296303

304+
[SuppressMessage("ReSharper", "InconsistentlySynchronizedField")]
297305
internal Type GetFinalType(Type argType)
298306
{
299307
Type next;
@@ -302,18 +310,18 @@ internal Type GetFinalType(Type argType)
302310
var info = type.GetTypeInfo();
303311
if (info.IsGenericType)
304312
{
305-
if (IsRegistered(type)) return type;
313+
if (_isTypeExplicitlyRegistered(type)) return type;
306314

307315
var definition = info.GetGenericTypeDefinition();
308-
if (IsRegistered(definition)) return definition;
316+
if (_isTypeExplicitlyRegistered(definition)) return definition;
309317

310318
next = info.GenericTypeArguments[0];
311-
if (IsRegistered(next)) return next;
319+
if (_isTypeExplicitlyRegistered(next)) return next;
312320
}
313321
else if (type.IsArray)
314322
{
315323
next = type.GetElementType();
316-
if (IsRegistered(next)) return next;
324+
if (_isTypeExplicitlyRegistered(next)) return next;
317325
}
318326
else
319327
{
@@ -336,6 +344,7 @@ internal Type GetFinalType(Type argType)
336344
/// This class doesn't have a finalizer, so <paramref name="disposing"/> will always be true.</remarks>
337345
/// <param name="disposing">True if being called typeFrom the IDisposable.Dispose
338346
/// method, false if being called typeFrom a finalizer.</param>
347+
[SuppressMessage("ReSharper", "InconsistentlySynchronizedField")]
339348
protected virtual void Dispose(bool disposing)
340349
{
341350
if (!disposing) return;

src/UnityContainer.Public.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public object Resolve(Type typeToBuild, string nameToBuild, params ResolverOverr
2929
var registration = GetRegistration(type, name);
3030
var context = new BuilderContext(this, (InternalRegistration)registration, null, resolverOverrides);
3131

32-
return BuilUpPipeline(context);
32+
return BuildUpPipeline(context);
3333
}
3434

3535
#endregion
@@ -62,7 +62,7 @@ public object BuildUp(Type typeToBuild, object existing, string nameToBuild, par
6262

6363
var context = new BuilderContext(this, (InternalRegistration)GetRegistration(type, name), existing, resolverOverrides);
6464

65-
return BuilUpPipeline(context);
65+
return BuildUpPipeline(context);
6666
}
6767

6868

src/UnityContainer.Registration.cs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
4+
using System.Linq;
45
using System.Reflection;
56
using Unity.Builder;
67
using Unity.Builder.Strategy;
@@ -171,11 +172,11 @@ public IUnityContainer RegisterInstance(Type registeredType, string name, object
171172

172173
#region Check Registration
173174

174-
public bool IsRegistered(Type type, string name) => ReferenceEquals(string.Empty, name)
175-
? IsRegistered(type)
176-
: IsTypeRegistered(type, name);
175+
public bool IsRegistered(Type type, string name) =>
176+
ReferenceEquals(string.Empty, name) ? _isTypeExplicitlyRegistered(type)
177+
: _isExplicitlyRegistered(type, name);
177178

178-
private bool IsTypeRegisteredLocally(Type type, string name)
179+
private bool IsExplicitlyRegisteredLocally(Type type, string name)
179180
{
180181
var hashCode = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;
181182
var targetBucket = hashCode % _registrations.Buckets.Length;
@@ -188,37 +189,32 @@ private bool IsTypeRegisteredLocally(Type type, string name)
188189
}
189190

190191
var registry = _registrations.Entries[i].Value;
191-
return null != registry?[name] as IContainerRegistration ||
192-
(_parent?.IsTypeRegistered(type, name) ?? false);
192+
return registry?[name] is IContainerRegistration ||
193+
(_parent?.IsRegistered(type, name) ?? false);
193194
}
194195

195-
return _parent?.IsTypeRegistered(type, name) ?? false;
196+
return _parent?.IsRegistered(type, name) ?? false;
196197
}
197198

198-
private bool IsRegistered(Type type)
199+
private bool IsTypeTypeExplicitlyRegisteredLocally(Type type)
199200
{
200-
if (null != _registrations)
201+
var hashCode = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;
202+
var targetBucket = hashCode % _registrations.Buckets.Length;
203+
for (var i = _registrations.Buckets[targetBucket]; i >= 0; i = _registrations.Entries[i].Next)
201204
{
202-
var hashCode = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;
203-
var targetBucket = hashCode % _registrations.Buckets.Length;
204-
for (var i = _registrations.Buckets[targetBucket]; i >= 0; i = _registrations.Entries[i].Next)
205+
if (_registrations.Entries[i].HashCode != hashCode ||
206+
_registrations.Entries[i].Key != type)
205207
{
206-
if (_registrations.Entries[i].HashCode != hashCode ||
207-
_registrations.Entries[i].Key != type)
208-
{
209-
continue;
210-
}
211-
212-
return _registrations.Entries[i]
213-
.Value
214-
.Values
215-
.Where(v => v is IContainerRegistration)
216-
.Any() ||
217-
(_parent?.IsRegistered(type) ?? false);
208+
continue;
218209
}
210+
211+
return _registrations.Entries[i].Value
212+
.Values
213+
.Any(v => v is IContainerRegistration) ||
214+
(_parent?._isTypeExplicitlyRegistered(type) ?? false);
219215
}
220216

221-
return _parent?.IsRegistered(type) ?? false;
217+
return _parent?._isTypeExplicitlyRegistered(type) ?? false;
222218
}
223219

224220
#endregion
@@ -471,25 +467,6 @@ private IPolicySet GetOrAddGeneric(Type type, string name, Type definition)
471467

472468
}
473469

474-
private IPolicySet GetChained(Type type, string name)
475-
{
476-
var hashCode = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;
477-
var targetBucket = hashCode % _registrations.Buckets.Length;
478-
for (var i = _registrations.Buckets[targetBucket]; i >= 0; i = _registrations.Entries[i].Next)
479-
{
480-
if (_registrations.Entries[i].HashCode != hashCode ||
481-
_registrations.Entries[i].Key != type)
482-
{
483-
continue;
484-
}
485-
486-
return _registrations.Entries[i].Value?[name] ??
487-
_parent?._get(type, name);
488-
}
489-
490-
return _parent?._get(type, name);
491-
}
492-
493470
private IPolicySet Get(Type type, string name)
494471
{
495472
var hashCode = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;

tests/Unity.Specification/Unity.Specification.csproj renamed to tests/Unity.Specification/Unity.Specification.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net47</TargetFramework>
4+
<TargetFramework>net46</TargetFramework>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0-preview-20180221-13" />
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0-preview-20180307-01" />
1010
<PackageReference Include="MSTest.TestAdapter" Version="1.3.0-beta2" />
1111
<PackageReference Include="MSTest.TestFramework" Version="1.3.0-beta2" />
1212
</ItemGroup>
1313

1414
<ItemGroup>
1515
<ProjectReference Include="..\..\..\Abstractions\src\Unity.Abstractions.csproj" />
16-
<ProjectReference Include="..\..\..\SpecificationTests\src\Unity.Specification.Tests.csproj" />
16+
<ProjectReference Include="..\..\..\SpecificationTests\src\Unity.Specification.csproj" />
1717
<ProjectReference Include="..\..\src\Unity.Container.csproj" />
1818
</ItemGroup>
1919

0 commit comments

Comments
 (0)