2014-11-30 12:22:03 -08:00
|
|
|
|
using Microsoft.Practices.Unity;
|
|
|
|
|
using Stylet;
|
|
|
|
|
using System;
|
2015-01-14 04:23:43 -08:00
|
|
|
|
using System.Windows;
|
2014-11-30 12:22:03 -08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrappers
|
|
|
|
|
{
|
|
|
|
|
public class UnityBootstrapper<TRootViewModel> : BootstrapperBase<TRootViewModel> where TRootViewModel : class
|
|
|
|
|
{
|
|
|
|
|
private IUnityContainer container;
|
|
|
|
|
|
|
|
|
|
protected override void ConfigureBootstrapper()
|
|
|
|
|
{
|
|
|
|
|
this.Configure();
|
|
|
|
|
|
|
|
|
|
this.container = new UnityContainer();
|
|
|
|
|
this.DefaultConfigureIoC(this.container);
|
|
|
|
|
this.ConfigureIoC(this.container);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-01-04 05:23:29 -08:00
|
|
|
|
/// Override to configure anything that needs configuring
|
2014-11-30 12:22:03 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void Configure() { }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Carries out default configuration of the IoC container. Override if you don't want to do this
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void DefaultConfigureIoC(IUnityContainer container)
|
|
|
|
|
{
|
|
|
|
|
// For some reason using ContainerControlledLifetimeManager results in a transient registration....
|
|
|
|
|
// This is a workaround
|
2014-12-02 04:52:58 -08:00
|
|
|
|
var viewManager = new ViewManager(this);
|
|
|
|
|
container.RegisterInstance<IViewManager>(viewManager);
|
2014-11-30 12:22:03 -08:00
|
|
|
|
container.RegisterInstance<IWindowManager>(new WindowManager(viewManager, () => container.Resolve<IMessageBoxViewModel>()));
|
|
|
|
|
container.RegisterInstance<IEventAggregator>(new EventAggregator());
|
|
|
|
|
container.RegisterType<IMessageBoxViewModel, MessageBoxViewModel>(new PerResolveLifetimeManager());
|
|
|
|
|
container.RegisterTypes(AllClasses.FromAssemblies(this.Assemblies), WithMappings.None, WithName.Default, WithLifetime.PerResolve);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Override to add your own types to the IoC container.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void ConfigureIoC(IUnityContainer container) { }
|
|
|
|
|
|
2014-12-02 07:20:16 -08:00
|
|
|
|
public override object GetInstance(Type type)
|
2014-11-30 12:22:03 -08:00
|
|
|
|
{
|
2014-12-02 04:52:58 -08:00
|
|
|
|
return this.container.Resolve(type);
|
2014-11-30 12:22:03 -08:00
|
|
|
|
}
|
2015-01-14 04:23:43 -08:00
|
|
|
|
|
|
|
|
|
protected override void OnExitInternal(ExitEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.container.Dispose();
|
|
|
|
|
}
|
2014-11-30 12:22:03 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|