Skip to content

Commit 1a9b69c

Browse files
committed
Merge branch 'master' of https://github.com/unitycontainer/container into v5.x
2 parents 4d4f588 + c8a39a6 commit 1a9b69c

File tree

4 files changed

+133
-8
lines changed

4 files changed

+133
-8
lines changed

src/Extension/Diagnostic.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,40 @@
22

33
namespace Unity
44
{
5+
/// <summary>
6+
/// Diagnostic extension implements validating when calling <see cref="IUnityContainer.RegisterType"/>,
7+
/// <see cref="IUnityContainer.Resolve"/>, and <see cref="IUnityContainer.BuildUp"/> methods. When executed
8+
/// these methods provide extra layer of verification and validation as well
9+
/// as more detailed reporting of error conditions.
10+
/// </summary>
11+
/// <remarks>
12+
/// <para>
13+
/// Unity uses reflection to gather information about types, members, and parameters.
14+
/// It is quite abvious that it takes significant amount of time during execution. So,
15+
/// to optimize performance all these verifications where mooved to the Diagnostic
16+
/// extension. It is recommended to include this extension only during
17+
/// development cycle and refrain from executing it in production
18+
/// environment.
19+
/// </para>
20+
/// <para>
21+
/// This extenson can be registered in two ways: by adding an extention or by calling
22+
/// <c>EnableDiagnostic()</c> extension method on container.
23+
/// Adding extension to container will work in any build, where <c>EnableDiagnostic()</c>
24+
/// will only enable it in DEBUG mode.
25+
/// </para>
26+
/// </remarks>
27+
/// <example>
28+
/// <code>
29+
/// var container = new UnityContainer();
30+
/// #if DEBUG
31+
/// container.AddExtension(new Diagnostic());
32+
/// #endif
33+
/// </code>
34+
/// <code>
35+
/// var container = new UnityContainer();
36+
/// container.EnableDiagnostic();
37+
/// </code>
38+
/// </example>
539
public class Diagnostic : UnityContainerExtension
640
{
741
protected override void Initialize()

src/Extension/Legacy.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Unity.Builder;
5+
using Unity.Processors;
6+
using Unity.Storage;
7+
8+
namespace Unity.Extension
9+
{
10+
public class Legacy : UnityContainerExtension
11+
{
12+
protected override void Initialize()
13+
{
14+
var strategies = (StagedStrategyChain<MemberProcessor, BuilderStage>)Context.BuildPlanStrategies;
15+
var processor = (ConstructorProcessor)strategies.First(s => s is ConstructorProcessor);
16+
17+
processor.SelectMethod = processor.LegacySelector;
18+
}
19+
}
20+
}

tests/Unity.Tests/DiagnosticExtensionFixture.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using System;
3-
using Unity.Extension;
43

54
namespace Unity.Tests.v5
65
{
@@ -37,12 +36,5 @@ public void ErrorMessage()
3736
var message = ex.Message;
3837
}
3938
}
40-
41-
42-
4339
}
44-
45-
#region Test Data
46-
47-
#endregion
4840
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using Unity.Extension;
4+
5+
namespace Unity.Tests.v5
6+
{
7+
[TestClass]
8+
public class LegacyExtensionFixture
9+
{
10+
[TestMethod]
11+
public void Register()
12+
{
13+
// Setup
14+
var container = new UnityContainer();
15+
container.AddNewExtension<Legacy>();
16+
17+
// Act
18+
var config = container.Configure<Legacy>();
19+
20+
// Validate
21+
Assert.IsNotNull(config);
22+
}
23+
24+
[TestMethod]
25+
public void SmartByDefault()
26+
{
27+
// Setup
28+
var container = new UnityContainer();
29+
30+
// Act
31+
var result = container.Resolve<ObjectWithMultipleConstructors>();
32+
33+
// Validate
34+
Assert.IsNotNull(result);
35+
}
36+
37+
[TestMethod]
38+
[ExpectedException(typeof(ResolutionFailedException))]
39+
public void LegacySelection()
40+
{
41+
// Setup
42+
var container = new UnityContainer();
43+
container.AddNewExtension<Legacy>();
44+
45+
// Act
46+
container.Resolve<ObjectWithMultipleConstructors>();
47+
}
48+
}
49+
50+
#region Test Data
51+
52+
public class ObjectWithMultipleConstructors
53+
{
54+
public const string One = "1";
55+
public const string Two = "2";
56+
public const string Three = "3";
57+
public const string Four = "4";
58+
public const string Five = "5";
59+
60+
public string Signature { get; }
61+
62+
public ObjectWithMultipleConstructors(int first)
63+
{
64+
Signature = One;
65+
}
66+
67+
public ObjectWithMultipleConstructors(object first, IUnityContainer second)
68+
{
69+
Signature = Two;
70+
}
71+
72+
public ObjectWithMultipleConstructors(object first, string second, IUnityContainer third)
73+
{
74+
Signature = Three;
75+
}
76+
}
77+
78+
#endregion
79+
}

0 commit comments

Comments
 (0)