Added Legacy extension
This commit is contained in:
Eugene Sadovoi 2019-03-01 14:29:25 -05:00
Родитель c32f3726bf
Коммит c8a39a663f
4 изменённых файлов: 133 добавлений и 8 удалений

Просмотреть файл

@ -2,6 +2,40 @@
namespace Unity
{
/// <summary>
/// Diagnostic extension implements validating when calling <see cref="IUnityContainer.RegisterType"/>,
/// <see cref="IUnityContainer.Resolve"/>, and <see cref="IUnityContainer.BuildUp"/> methods. When executed
/// these methods provide extra layer of verification and validation as well
/// as more detailed reporting of error conditions.
/// </summary>
/// <remarks>
/// <para>
/// Unity uses reflection to gather information about types, members, and parameters.
/// It is quite abvious that it takes significant amount of time during execution. So,
/// to optimize performance all these verifications where mooved to the Diagnostic
/// extension. It is recommended to include this extension only during
/// development cycle and refrain from executing it in production
/// environment.
/// </para>
/// <para>
/// This extenson can be registered in two ways: by adding an extention or by calling
/// <c>EnableDiagnostic()</c> extension method on container.
/// Adding extension to container will work in any build, where <c>EnableDiagnostic()</c>
/// will only enable it in DEBUG mode.
/// </para>
/// </remarks>
/// <example>
/// <code>
/// var container = new UnityContainer();
/// #if DEBUG
/// container.AddExtension(new Diagnostic());
/// #endif
/// </code>
/// <code>
/// var container = new UnityContainer();
/// container.EnableDiagnostic();
/// </code>
/// </example>
public class Diagnostic : UnityContainerExtension
{
protected override void Initialize()

20
src/Extension/Legacy.cs Normal file
Просмотреть файл

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Builder;
using Unity.Processors;
using Unity.Storage;
namespace Unity.Extension
{
public class Legacy : UnityContainerExtension
{
protected override void Initialize()
{
var strategies = (StagedStrategyChain<MemberProcessor, BuilderStage>)Context.BuildPlanStrategies;
var processor = (ConstructorProcessor)strategies.First(s => s is ConstructorProcessor);
processor.SelectMethod = processor.LegacySelector;
}
}
}

Просмотреть файл

@ -1,6 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Unity.Extension;
namespace Unity.Tests.v5
{
@ -37,12 +36,5 @@ namespace Unity.Tests.v5
var message = ex.Message;
}
}
}
#region Test Data
#endregion
}

Просмотреть файл

@ -0,0 +1,79 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Unity.Extension;
namespace Unity.Tests.v5
{
[TestClass]
public class LegacyExtensionFixture
{
[TestMethod]
public void Register()
{
// Setup
var container = new UnityContainer();
container.AddNewExtension<Legacy>();
// Act
var config = container.Configure<Legacy>();
// Validate
Assert.IsNotNull(config);
}
[TestMethod]
public void SmartByDefault()
{
// Setup
var container = new UnityContainer();
// Act
var result = container.Resolve<ObjectWithMultipleConstructors>();
// Validate
Assert.IsNotNull(result);
}
[TestMethod]
[ExpectedException(typeof(ResolutionFailedException))]
public void LegacySelection()
{
// Setup
var container = new UnityContainer();
container.AddNewExtension<Legacy>();
// Act
container.Resolve<ObjectWithMultipleConstructors>();
}
}
#region Test Data
public class ObjectWithMultipleConstructors
{
public const string One = "1";
public const string Two = "2";
public const string Three = "3";
public const string Four = "4";
public const string Five = "5";
public string Signature { get; }
public ObjectWithMultipleConstructors(int first)
{
Signature = One;
}
public ObjectWithMultipleConstructors(object first, IUnityContainer second)
{
Signature = Two;
}
public ObjectWithMultipleConstructors(object first, string second, IUnityContainer third)
{
Signature = Three;
}
}
#endregion
}