Skip to content

Commit 9d3d598

Browse files
committed
Merge branch 'master' of https://github.com/unitycontainer/container into v5.x
2 parents 4850232 + f55ee30 commit 9d3d598

27 files changed

+195
-212
lines changed

src/Extension/Diagnostic.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Extensions/DiagnosticExtensions.cs renamed to src/Extensions/Diagnostic.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,52 @@
1-
using System;
1+
using System;
22
using System.Diagnostics;
3+
using Unity.Extension;
34

45
namespace Unity
56
{
7+
/// <summary>
8+
/// Diagnostic extension implements validating when calling <see cref="IUnityContainer.RegisterType"/>,
9+
/// <see cref="IUnityContainer.Resolve"/>, and <see cref="IUnityContainer.BuildUp"/> methods. When executed
10+
/// these methods provide extra layer of verification and validation as well
11+
/// as more detailed reporting of error conditions.
12+
/// </summary>
13+
/// <remarks>
14+
/// <para>
15+
/// Unity uses reflection to gather information about types, members, and parameters.
16+
/// It is quite obvious that it takes significant amount of time during execution. So,
17+
/// to optimize performance all these verifications where moved to the Diagnostic
18+
/// extension. It is recommended to include this extension only during
19+
/// development cycle and refrain from executing it in production
20+
/// environment.
21+
/// </para>
22+
/// <para>
23+
/// This extension can be registered in two ways: by adding an extension or by calling
24+
/// <c>EnableDiagnostic()</c> extension method on container.
25+
/// Adding extension to container will work in any build, where <c>EnableDiagnostic()</c>
26+
/// will only enable it in DEBUG mode.
27+
/// </para>
28+
/// </remarks>
29+
/// <example>
30+
/// <code>
31+
/// var container = new UnityContainer();
32+
/// #if DEBUG
33+
/// container.AddExtension(new Diagnostic());
34+
/// #endif
35+
/// </code>
36+
/// <code>
37+
/// var container = new UnityContainer();
38+
/// container.EnableDiagnostic();
39+
/// </code>
40+
/// </example>
41+
public class Diagnostic : UnityContainerExtension
42+
{
43+
protected override void Initialize()
44+
{
45+
((UnityContainer)Container).SetDefaultPolicies = UnityContainer.SetDiagnosticPolicies;
46+
((UnityContainer)Container).SetDefaultPolicies((UnityContainer)Container);
47+
}
48+
}
49+
650
public static class DiagnosticExtensions
751
{
852
/// <summary>
@@ -54,9 +98,10 @@ public static void EnableDebugDiagnostic(this UnityContainer container)
5498
public static UnityContainer EnableDiagnostic(this UnityContainer container)
5599
{
56100
if (null == container) throw new ArgumentNullException(nameof(container));
57-
101+
58102
container.AddExtension(new Diagnostic());
59103
return container;
60104
}
61105
}
106+
62107
}

src/Extension/Legacy.cs renamed to src/Extensions/Legacy.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Linq;
42
using Unity.Builder;
53
using Unity.Processors;
64
using Unity.Storage;

src/Extensions/Strategies.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Unity.Extension;
2+
using Unity.Policy;
3+
4+
namespace Unity
5+
{
6+
/// <summary>
7+
/// This extension forces the container to only use activated strategies during resolution
8+
/// </summary>
9+
/// <remarks>
10+
/// This extension forces compatibility with systems without support for runtime compilers.
11+
/// One of such systems is iOS.
12+
/// </remarks>
13+
public class ForceActivation : UnityContainerExtension
14+
{
15+
protected override void Initialize()
16+
{
17+
var unity = (UnityContainer)Container;
18+
19+
unity._buildStrategy = unity.ResolvingFactory;
20+
unity.Defaults.Set(typeof(ResolveDelegateFactory), unity._buildStrategy);
21+
}
22+
}
23+
24+
/// <summary>
25+
/// This extension forces the container to only use compiled strategies during resolution
26+
/// </summary>
27+
public class ForceCompillation : UnityContainerExtension
28+
{
29+
protected override void Initialize()
30+
{
31+
var unity = (UnityContainer)Container;
32+
33+
unity._buildStrategy = unity.CompilingFactory;
34+
unity.Defaults.Set(typeof(ResolveDelegateFactory), unity._buildStrategy);
35+
36+
}
37+
}
38+
39+
}

src/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
using System;
2-
using System.Runtime.CompilerServices;
32
using System.Security;
43

5-
[assembly:CLSCompliant(true)]
4+
[assembly: CLSCompliant(true)]
65
[assembly: AllowPartiallyTrustedCallers]
7-
8-
[assembly: InternalsVisibleTo("Performance, PublicKey=" +
9-
"002400000480000094000000060200000024000052534131000400000100010037b16015885a7a" +
10-
"c3c63f3c10b23972ec0dfd6db643eaef45ea2297bdfdc53b1945017fc76fd038dc6e7bf9190024" +
11-
"d5435fa49630fdfd143e3149a1506b895fbcce017df1d4f0eac6f05f6d257be45c7be9a8aa8d3d" +
12-
"4164892dc75e7c379a22da0d986db393fbd09e4ba42398c80a305361553ef90eb3484d9cf12df9" +
13-
"0fc0e6e3")]
14-
[assembly: InternalsVisibleTo("Unity.Specification.Tests, PublicKey=" +
15-
"002400000480000094000000060200000024000052534131000400000100010037b16015885a7a" +
16-
"c3c63f3c10b23972ec0dfd6db643eaef45ea2297bdfdc53b1945017fc76fd038dc6e7bf9190024" +
17-
"d5435fa49630fdfd143e3149a1506b895fbcce017df1d4f0eac6f05f6d257be45c7be9a8aa8d3d" +
18-
"4164892dc75e7c379a22da0d986db393fbd09e4ba42398c80a305361553ef90eb3484d9cf12df9" +
19-
"0fc0e6e3")]
20-
[assembly: InternalsVisibleTo("Unity.Specification.Tests.Diagnostic, PublicKey=" +
21-
"002400000480000094000000060200000024000052534131000400000100010037b16015885a7a" +
22-
"c3c63f3c10b23972ec0dfd6db643eaef45ea2297bdfdc53b1945017fc76fd038dc6e7bf9190024" +
23-
"d5435fa49630fdfd143e3149a1506b895fbcce017df1d4f0eac6f05f6d257be45c7be9a8aa8d3d" +
24-
"4164892dc75e7c379a22da0d986db393fbd09e4ba42398c80a305361553ef90eb3484d9cf12df9" +
25-
"0fc0e6e3")]

src/UnityContainer.Diagnostic.cs

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5-
using System.Reflection;
6-
using Unity.Builder;
7-
using Unity.Injection;
85
using Unity.Policy;
9-
using Unity.Processors;
106
using Unity.Registration;
11-
using Unity.Storage;
127

138
namespace Unity
149
{
@@ -18,40 +13,12 @@ public partial class UnityContainer
1813
{
1914
#region Fields
2015

21-
private readonly ResolveDelegateFactory _buildStrategy = OptimizingFactory;
16+
internal ResolveDelegateFactory _buildStrategy = OptimizingFactory;
2217

2318
#endregion
2419

25-
internal enum BuildStrategy
26-
{
27-
Compiled,
28-
29-
Resolved,
30-
31-
Optimized
32-
}
3320

34-
#region Diagnostic Constructor
35-
36-
internal UnityContainer(BuildStrategy strategy)
37-
: this()
38-
{
39-
switch (strategy)
40-
{
41-
case BuildStrategy.Compiled:
42-
_buildStrategy = CompilingFactory;
43-
Defaults.Set(typeof(ResolveDelegateFactory), _buildStrategy);
44-
break;
45-
46-
case BuildStrategy.Resolved:
47-
_buildStrategy = ResolvingFactory;
48-
Defaults.Set(typeof(ResolveDelegateFactory), _buildStrategy);
49-
break;
50-
}
51-
}
52-
53-
54-
#endregion
21+
#region Debug Support
5522

5623
private string DebugName()
5724
{
@@ -80,5 +47,7 @@ public UnityContainerDebugProxy(IUnityContainer container)
8047
public IEnumerable<IContainerRegistration> Registrations => _container.Registrations;
8148

8249
}
50+
51+
#endregion
8352
}
8453
}

src/UnityContainer.Resolution.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private static ResolveDelegate<BuilderContext> OptimizingFactory(ref BuilderCont
208208
};
209209
}
210210

211-
private ResolveDelegate<BuilderContext> CompilingFactory(ref BuilderContext context)
211+
internal ResolveDelegate<BuilderContext> CompilingFactory(ref BuilderContext context)
212212
{
213213
var expressions = new List<Expression>();
214214
var type = context.Type;
@@ -228,7 +228,7 @@ private ResolveDelegate<BuilderContext> CompilingFactory(ref BuilderContext cont
228228
return lambda.Compile();
229229
}
230230

231-
private ResolveDelegate<BuilderContext> ResolvingFactory(ref BuilderContext context)
231+
internal ResolveDelegate<BuilderContext> ResolvingFactory(ref BuilderContext context)
232232
{
233233
ResolveDelegate<BuilderContext> seed = null;
234234
var type = context.Type;

tests/Performance/Tests/Compiled.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Runner.Setup;
33
using System.Collections.Generic;
44
using Unity;
5-
using Unity.Builder;
65

76
namespace Runner.Tests
87
{
@@ -16,7 +15,7 @@ public class Compiled
1615
[IterationSetup]
1716
public virtual void SetupContainer()
1817
{
19-
_container = new UnityContainer(Unity.UnityContainer.BuildStrategy.Compiled);
18+
_container = new UnityContainer().AddExtension(new ForceCompillation());
2019

2120
_container.RegisterType<Poco>();
2221
_container.RegisterType<IFoo, Foo>();

tests/Performance/Tests/PreCompiled.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class PreCompiled
1616
[IterationSetup]
1717
public virtual void SetupContainer()
1818
{
19-
_container = new UnityContainer(Unity.UnityContainer.BuildStrategy.Compiled);
19+
_container = new UnityContainer().AddExtension(new ForceCompillation());
2020

2121
_container.RegisterType<Poco>();
2222
_container.RegisterType<IFoo, Foo>();

tests/Performance/Tests/PreResolved.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class PreResolved
1616
[IterationSetup]
1717
public virtual void SetupContainer()
1818
{
19-
_container = new UnityContainer(Unity.UnityContainer.BuildStrategy.Resolved);
19+
_container = new UnityContainer().AddExtension(new ForceActivation());
2020

2121
_container.RegisterType<Poco>();
2222
_container.RegisterType<IFoo, Foo>();

0 commit comments

Comments
 (0)