Refactoring Stage engine
This commit is contained in:
Родитель
65c80dbf70
Коммит
73f75f5367
|
@ -10,6 +10,7 @@ using Unity.Builder;
|
|||
using Unity.Extension;
|
||||
using Unity.Policy;
|
||||
using Guard = Microsoft.Practices.Unity.Utility.Guard;
|
||||
using Unity.Strategy;
|
||||
|
||||
namespace Microsoft.Practices.Unity.InterceptionExtension
|
||||
{
|
||||
|
|
|
@ -19,7 +19,7 @@ namespace Microsoft.Practices.Unity.TestSupport
|
|||
private NamedTypeBuildKey originalBuildKey = null;
|
||||
private IPolicyList persistentPolicies;
|
||||
private IPolicyList policies;
|
||||
private StrategyChain strategies = new StrategyChain();
|
||||
private MockStrategyChain strategies = new MockStrategyChain();
|
||||
private CompositeResolverOverride resolverOverrides = new CompositeResolverOverride();
|
||||
|
||||
private NamedTypeBuildKey buildKey = null;
|
||||
|
@ -57,7 +57,7 @@ namespace Microsoft.Practices.Unity.TestSupport
|
|||
get { return recoveryStack; }
|
||||
}
|
||||
|
||||
public StrategyChain Strategies
|
||||
public MockStrategyChain Strategies
|
||||
{
|
||||
get { return strategies; }
|
||||
}
|
||||
|
|
|
@ -0,0 +1,170 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Builder;
|
||||
using Unity.Strategy;
|
||||
|
||||
namespace Microsoft.Practices.Unity.TestSupport
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a chain of responsibility for builder strategies.
|
||||
/// </summary>
|
||||
public class MockStrategyChain : IStrategyChain
|
||||
{
|
||||
private readonly List<IBuilderStrategy> _strategies = new List<IBuilderStrategy>();
|
||||
|
||||
/// <summary>
|
||||
/// Initialize a new instance of the <see cref="MockStrategyChain"/> class.
|
||||
/// </summary>
|
||||
public MockStrategyChain() { }
|
||||
|
||||
/// <summary>
|
||||
/// Initialize a new instance of the <see cref="MockStrategyChain"/> class with a collection of strategies.
|
||||
/// </summary>
|
||||
/// <param name="strategies">A collection of strategies to initialize the chain.</param>
|
||||
public MockStrategyChain(IEnumerable strategies)
|
||||
{
|
||||
AddRange(strategies);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a strategy to the chain.
|
||||
/// </summary>
|
||||
/// <param name="strategy">The strategy to add to the chain.</param>
|
||||
public void Add(IBuilderStrategy strategy)
|
||||
{
|
||||
_strategies.Add(strategy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds strategies to the chain.
|
||||
/// </summary>
|
||||
/// <param name="strategyEnumerable">The strategies to add to the chain.</param>
|
||||
public void AddRange(IEnumerable strategyEnumerable)
|
||||
{
|
||||
foreach (IBuilderStrategy strategy in strategyEnumerable
|
||||
?? throw new ArgumentNullException(nameof(strategyEnumerable)))
|
||||
{
|
||||
Add(strategy);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverse the order of the strategy chain.
|
||||
/// </summary>
|
||||
/// <returns>The reversed strategy chain.</returns>
|
||||
public IStrategyChain Reverse()
|
||||
{
|
||||
List<IBuilderStrategy> reverseList = new List<IBuilderStrategy>(_strategies);
|
||||
reverseList.Reverse();
|
||||
return new MockStrategyChain(reverseList);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute this strategy chain against the given context to build up.
|
||||
/// </summary>
|
||||
/// <param name="context">Context for the build processes.</param>
|
||||
/// <returns>The build up object</returns>
|
||||
public object ExecuteBuildUp(IBuilderContext builderContext)
|
||||
{
|
||||
var context = builderContext ?? throw new ArgumentNullException(nameof(builderContext));
|
||||
int i = 0;
|
||||
|
||||
try
|
||||
{
|
||||
for (; i < _strategies.Count; ++i)
|
||||
{
|
||||
if (context.BuildComplete)
|
||||
{
|
||||
break;
|
||||
}
|
||||
_strategies[i].PreBuildUp(context);
|
||||
}
|
||||
|
||||
if (context.BuildComplete)
|
||||
{
|
||||
--i; // skip shortcutting strategy's post
|
||||
}
|
||||
|
||||
for (--i; i >= 0; --i)
|
||||
{
|
||||
_strategies[i].PostBuildUp(context);
|
||||
}
|
||||
return context.Existing;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
context.RecoveryStack.ExecuteRecovery();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute this strategy chain against the given context,
|
||||
/// calling the TearDown methods on the strategies.
|
||||
/// </summary>
|
||||
/// <param name="context">Context for the teardown process.</param>
|
||||
public void ExecuteTearDown(IBuilderContext builderContext)
|
||||
{
|
||||
var context = builderContext ?? throw new ArgumentNullException(nameof(builderContext));
|
||||
int i = 0;
|
||||
|
||||
try
|
||||
{
|
||||
for (; i < _strategies.Count; ++i)
|
||||
{
|
||||
if (context.BuildComplete)
|
||||
{
|
||||
--i; // Skip current strategy's post
|
||||
break;
|
||||
}
|
||||
_strategies[i].PreTearDown(context);
|
||||
}
|
||||
|
||||
for (--i; i >= 0; --i)
|
||||
{
|
||||
_strategies[i].PostTearDown(context);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
context.RecoveryStack.ExecuteRecovery();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
#region IEnumerable<IBuilderStrategy> Members
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator that iterates through the collection.
|
||||
/// </summary>
|
||||
///
|
||||
/// <returns>
|
||||
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
|
||||
/// </returns>
|
||||
IEnumerator<IBuilderStrategy> IEnumerable<IBuilderStrategy>.GetEnumerator()
|
||||
{
|
||||
return _strategies.GetEnumerator();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable Members
|
||||
|
||||
/// <summary>
|
||||
/// Returns an enumerator that iterates through a collection.
|
||||
/// </summary>
|
||||
///
|
||||
/// <returns>
|
||||
/// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
|
||||
/// </returns>
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
return _strategies.GetEnumerator();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче