diff --git a/Samples/Stylet.Samples.Hello/HelloBootstrapper.cs b/Samples/Stylet.Samples.Hello/HelloBootstrapper.cs index 45f5c0c..ca0c352 100644 --- a/Samples/Stylet.Samples.Hello/HelloBootstrapper.cs +++ b/Samples/Stylet.Samples.Hello/HelloBootstrapper.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Stylet.Samples.Hello { - class HelloBootstrapper : IoCBootstrapper + class HelloBootstrapper : Bootstrapper { } } diff --git a/Samples/Stylet.Samples.TabNavigation/Bootstrapper.cs b/Samples/Stylet.Samples.TabNavigation/Bootstrapper.cs index 5b3b69f..d8255b4 100644 --- a/Samples/Stylet.Samples.TabNavigation/Bootstrapper.cs +++ b/Samples/Stylet.Samples.TabNavigation/Bootstrapper.cs @@ -7,7 +7,7 @@ using System.Windows; namespace Stylet.Samples.TabNavigation { - class Bootstrapper : IoCBootstrapper + class Bootstrapper : Bootstrapper { } } diff --git a/Stylet/Bootstrapper.cs b/Stylet/Bootstrapper.cs index 181f00b..b37c2b7 100644 --- a/Stylet/Bootstrapper.cs +++ b/Stylet/Bootstrapper.cs @@ -1,84 +1,47 @@ -using System; +using StyletIoC; +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: - // - // - // - // - // - // rather than: - // - // - // - // - // - // - // - // And also so that we can load the Stylet resources - public class Bootstrapper : ResourceDictionary + public class Bootstrapper : BootstrapperBase { - 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) }; - this.MergedDictionaries.Add(rc); + base.Start(); - 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; - Execute.SynchronizationContext = SynchronizationContext.Current; - - this.Application.Startup += OnStartup; - this.Application.Exit += OnExit; - this.Application.DispatcherUnhandledException += OnUnhandledExecption; - - this.Application.Startup += (o, e) => - { - IoC.Get().ShowWindow(IoC.Get()); - }; - - AssemblySource.Assemblies.Clear(); - AssemblySource.Assemblies.AddRange(this.SelectAssemblies()); - - IoC.GetInstance = this.GetInstance; - IoC.GetAllInstances = this.GetAllInstances; - IoC.BuildUp = this.BuildUp; + builder.Autobind(AssemblySource.Assemblies); + builder.Bind().To().InSingletonScope(); + builder.Bind().To().InSingletonScope(); } - protected virtual object GetInstance(Type service, string key = null) + protected override object GetInstance(Type service, string key = null) { - if (service == typeof(IWindowManager)) service = typeof(WindowManager); - return Activator.CreateInstance(service); + return this.container.Get(service, key); } - protected virtual IEnumerable GetAllInstances(Type service) + protected override IEnumerable GetAllInstances(Type service) { - return new[] { Activator.CreateInstance(service) }; + return this.container.GetAll(service); } - protected virtual void BuildUp(object instance) { } - - protected IEnumerable SelectAssemblies() + protected override void BuildUp(object instance) { - return new[] { Assembly.GetEntryAssembly() }; + this.container.BuildUp(instance); } - - protected virtual void OnStartup(object sender, StartupEventArgs e) { } - protected virtual void OnExit(object sender, EventArgs e) { } - protected virtual void OnUnhandledExecption(object sender, DispatcherUnhandledExceptionEventArgs e) { } } } diff --git a/Stylet/BootstrapperBase.cs b/Stylet/BootstrapperBase.cs new file mode 100644 index 0000000..3d2719a --- /dev/null +++ b/Stylet/BootstrapperBase.cs @@ -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: + // + // + // + // + // + // rather than: + // + // + // + // + // + // + // + // And also so that we can load the Stylet resources + public abstract class BootstrapperBase : 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().ShowWindow(IoC.Get()); + }; + + 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 GetAllInstances(Type service); + protected abstract void BuildUp(object instance); + + protected IEnumerable 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) { } + } +} diff --git a/Stylet/IoCBootstrapper.cs b/Stylet/IoCBootstrapper.cs deleted file mode 100644 index bfb4cba..0000000 --- a/Stylet/IoCBootstrapper.cs +++ /dev/null @@ -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 : Bootstrapper - { - 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().To().InSingletonScope(); - builder.Bind().To().InSingletonScope(); - } - - protected override object GetInstance(Type service, string key = null) - { - return this.container.Get(service, key); - } - - protected override IEnumerable GetAllInstances(Type service) - { - return this.container.GetAll(service); - } - - protected override void BuildUp(object instance) - { - this.container.BuildUp(instance); - } - } -} diff --git a/Stylet/Stylet.csproj b/Stylet/Stylet.csproj index ba220e5..ec3eebd 100644 --- a/Stylet/Stylet.csproj +++ b/Stylet/Stylet.csproj @@ -47,7 +47,7 @@ - + @@ -57,7 +57,7 @@ - +