Re-arrange bootloaders

Bootloader is now abstract, and called BootstrapperBase. The default
implmentations of the IoC functions have been removed - we include an IoC
container, no point in providing a Bootstrapper implementation which
doesn't use it.

IoCBootstrapper has been renamed Bootstrapper, to make it more obvious
that it's the primary bootstrapper.
This commit is contained in:
Antony Male 2014-02-23 20:08:35 +00:00
parent e37068838a
commit b13908e762
6 changed files with 98 additions and 107 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Stylet.Samples.Hello namespace Stylet.Samples.Hello
{ {
class HelloBootstrapper : IoCBootstrapper<ShellViewModel> class HelloBootstrapper : Bootstrapper<ShellViewModel>
{ {
} }
} }

View File

@ -7,7 +7,7 @@ using System.Windows;
namespace Stylet.Samples.TabNavigation namespace Stylet.Samples.TabNavigation
{ {
class Bootstrapper : IoCBootstrapper<ShellViewModel> class Bootstrapper : Bootstrapper<ShellViewModel>
{ {
} }
} }

View File

@ -1,84 +1,47 @@
using System; using StyletIoC;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Threading;
namespace Stylet namespace Stylet
{ {
// We pretend to be a ResourceDictionary so the user can do: public class Bootstrapper<TRootViewModel> : BootstrapperBase<TRootViewModel>
// <Application.Resources><ResourceDictionary>
// <ResourceDictionary.MergedDictionaries>
// <local:Bootstrapper/>
// </ResourceDictionary.MergedDictionaries>
// </ResourceDictionary></Application.Resources>
// rather than:
// <Application.Resources><ResourceDictionary>
// <ResourceDictionary.MergedDictionaries>
// <ResourceDictionary>
// <local:Bootstrapper x:Key="bootstrapper"/>
// </ResourceDictionary>
// </ResourceDictionary.MergedDictionaries>
// </ResourceDictionary></Application.Resources>
// And also so that we can load the Stylet resources
public class Bootstrapper<TRootViewModel> : ResourceDictionary
{ {
protected Application Application { get; private set; } protected IContainer container;
public Bootstrapper() protected override void Start()
{ {
var rc = new ResourceDictionary() { Source = new Uri("/Stylet;component/StyletResourceDictionary.xaml", UriKind.Relative) }; base.Start();
this.MergedDictionaries.Add(rc);
this.Start(); var builder = new StyletIoCBuilder();
this.ConfigureIoC(builder);
this.container = builder.BuildContainer();
} }
protected virtual void Start() protected virtual void ConfigureIoC(IStyletIoCBuilder builder)
{ {
this.Application = Application.Current; builder.Autobind(AssemblySource.Assemblies);
Execute.SynchronizationContext = SynchronizationContext.Current; builder.Bind<IWindowManager>().To<WindowManager>().InSingletonScope();
builder.Bind<IViewManager>().To<ViewManager>().InSingletonScope();
}
this.Application.Startup += OnStartup; protected override object GetInstance(Type service, string key = null)
this.Application.Exit += OnExit;
this.Application.DispatcherUnhandledException += OnUnhandledExecption;
this.Application.Startup += (o, e) =>
{ {
IoC.Get<IWindowManager>().ShowWindow(IoC.Get<TRootViewModel>()); return this.container.Get(service, key);
};
AssemblySource.Assemblies.Clear();
AssemblySource.Assemblies.AddRange(this.SelectAssemblies());
IoC.GetInstance = this.GetInstance;
IoC.GetAllInstances = this.GetAllInstances;
IoC.BuildUp = this.BuildUp;
} }
protected virtual object GetInstance(Type service, string key = null) protected override IEnumerable<object> GetAllInstances(Type service)
{ {
if (service == typeof(IWindowManager)) service = typeof(WindowManager); return this.container.GetAll(service);
return Activator.CreateInstance(service);
} }
protected virtual IEnumerable<object> GetAllInstances(Type service) protected override void BuildUp(object instance)
{ {
return new[] { Activator.CreateInstance(service) }; this.container.BuildUp(instance);
} }
protected virtual void BuildUp(object instance) { }
protected IEnumerable<Assembly> SelectAssemblies()
{
return new[] { Assembly.GetEntryAssembly() };
}
protected virtual void OnStartup(object sender, StartupEventArgs e) { }
protected virtual void OnExit(object sender, EventArgs e) { }
protected virtual void OnUnhandledExecption(object sender, DispatcherUnhandledExceptionEventArgs e) { }
} }
} }

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace Stylet
{
// We pretend to be a ResourceDictionary so the user can do:
// <Application.Resources><ResourceDictionary>
// <ResourceDictionary.MergedDictionaries>
// <local:Bootstrapper/>
// </ResourceDictionary.MergedDictionaries>
// </ResourceDictionary></Application.Resources>
// rather than:
// <Application.Resources><ResourceDictionary>
// <ResourceDictionary.MergedDictionaries>
// <ResourceDictionary>
// <local:Bootstrapper x:Key="bootstrapper"/>
// </ResourceDictionary>
// </ResourceDictionary.MergedDictionaries>
// </ResourceDictionary></Application.Resources>
// And also so that we can load the Stylet resources
public abstract class BootstrapperBase<TRootViewModel> : ResourceDictionary
{
protected Application Application { get; private set; }
public BootstrapperBase()
{
var rc = new ResourceDictionary() { Source = new Uri("/Stylet;component/StyletResourceDictionary.xaml", UriKind.Relative) };
this.MergedDictionaries.Add(rc);
this.Start();
}
protected virtual void Start()
{
this.Application = Application.Current;
Execute.SynchronizationContext = SynchronizationContext.Current;
this.Application.Startup += OnStartup;
this.Application.Exit += OnExit;
this.Application.DispatcherUnhandledException += OnUnhandledExecption;
this.Application.Startup += (o, e) =>
{
IoC.Get<IWindowManager>().ShowWindow(IoC.Get<TRootViewModel>());
};
AssemblySource.Assemblies.Clear();
AssemblySource.Assemblies.AddRange(this.SelectAssemblies());
IoC.GetInstance = this.GetInstance;
IoC.GetAllInstances = this.GetAllInstances;
IoC.BuildUp = this.BuildUp;
}
protected abstract object GetInstance(Type service, string key = null);
protected abstract IEnumerable<object> GetAllInstances(Type service);
protected abstract void BuildUp(object instance);
protected IEnumerable<Assembly> SelectAssemblies()
{
return new[] { Assembly.GetEntryAssembly() };
}
protected virtual void OnStartup(object sender, StartupEventArgs e) { }
protected virtual void OnExit(object sender, EventArgs e) { }
protected virtual void OnUnhandledExecption(object sender, DispatcherUnhandledExceptionEventArgs e) { }
}
}

View File

@ -1,47 +0,0 @@
using StyletIoC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Stylet
{
public class IoCBootstrapper<TRootViewModel> : Bootstrapper<TRootViewModel>
{
protected IContainer container;
protected override void Start()
{
base.Start();
var builder = new StyletIoCBuilder();
this.ConfigureIoC(builder);
this.container = builder.BuildContainer();
}
protected virtual void ConfigureIoC(IStyletIoCBuilder builder)
{
builder.Autobind(AssemblySource.Assemblies);
builder.Bind<IWindowManager>().To<WindowManager>().InSingletonScope();
builder.Bind<IViewManager>().To<ViewManager>().InSingletonScope();
}
protected override object GetInstance(Type service, string key = null)
{
return this.container.Get(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return this.container.GetAll(service);
}
protected override void BuildUp(object instance)
{
this.container.BuildUp(instance);
}
}
}

View File

@ -47,7 +47,7 @@
<Compile Include="ActionExtension.cs" /> <Compile Include="ActionExtension.cs" />
<Compile Include="AssemblySource.cs" /> <Compile Include="AssemblySource.cs" />
<Compile Include="BindableCollection.cs" /> <Compile Include="BindableCollection.cs" />
<Compile Include="Bootstrapper.cs" /> <Compile Include="BootstrapperBase.cs" />
<Compile Include="Conductor.cs" /> <Compile Include="Conductor.cs" />
<Compile Include="ConductorAllActive.cs" /> <Compile Include="ConductorAllActive.cs" />
<Compile Include="ConductorBase.cs" /> <Compile Include="ConductorBase.cs" />
@ -57,7 +57,7 @@
<Compile Include="Execute.cs" /> <Compile Include="Execute.cs" />
<Compile Include="IConductor.cs" /> <Compile Include="IConductor.cs" />
<Compile Include="IoC.cs" /> <Compile Include="IoC.cs" />
<Compile Include="IoCBootstrapper.cs" /> <Compile Include="Bootstrapper.cs" />
<Compile Include="IScreen.cs" /> <Compile Include="IScreen.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PropertyChangedBase.cs" /> <Compile Include="PropertyChangedBase.cs" />