2013-03-28 22:07:09 +00:00
|
|
|
|
|
2011-11-14 00:22:18 +00:00
|
|
|
|
|
|
|
|
|
using System.Linq;
|
2011-10-07 06:36:04 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Microsoft.Practices.Unity;
|
|
|
|
|
using Microsoft.Practices.Unity.ObjectBuilder;
|
|
|
|
|
|
2011-11-14 00:22:18 +00:00
|
|
|
|
namespace NzbDrone.Test.Common.AutoMoq.Unity
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2013-04-07 22:40:13 +00:00
|
|
|
|
public class AutoMockingContainerExtension : UnityContainerExtension
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|