2011-05-22 16:53:21 +00:00
|
|
|
|
// ReSharper disable RedundantUsingDirective
|
|
|
|
|
using System;
|
2011-04-07 03:34:48 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Microsoft.Practices.Unity;
|
|
|
|
|
using Microsoft.Practices.Unity.ObjectBuilder;
|
|
|
|
|
|
|
|
|
|
namespace AutoMoq.Unity
|
|
|
|
|
{
|
|
|
|
|
internal class AutoMockingContainerExtension : UnityContainerExtension
|
|
|
|
|
{
|
|
|
|
|
private readonly IList<Type> registeredTypes = new List<Type>();
|
|
|
|
|
|
|
|
|
|
protected override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
SetEventsOnContainerToTrackAllRegisteredTypes();
|
|
|
|
|
SetBuildingStrategyForBuildingUnregisteredTypes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region private methods
|
|
|
|
|
|
|
|
|
|
private void SetEventsOnContainerToTrackAllRegisteredTypes()
|
|
|
|
|
{
|
|
|
|
|
Context.Registering += ((sender, e) => RegisterType(e.TypeFrom));
|
|
|
|
|
Context.RegisteringInstance += ((sender, e) => RegisterType(e.RegisteredType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RegisterType(Type typeToRegister)
|
|
|
|
|
{
|
|
|
|
|
registeredTypes.Add(typeToRegister);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetBuildingStrategyForBuildingUnregisteredTypes()
|
|
|
|
|
{
|
|
|
|
|
var strategy = new AutoMockingBuilderStrategy(registeredTypes, Container);
|
|
|
|
|
Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|