From 7ea92e36735390c747f7154c450388415c837ec0 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Tue, 4 Feb 2014 18:21:52 +0000 Subject: [PATCH] Add super-simple IoC container --- Stylet/Bootstrapper.cs | 19 ++++++++++++++++++- Stylet/IoC.cs | 25 +++++++++++++++++++++++++ Stylet/Stylet.csproj | 1 + Stylet/ViewLocator.cs | 2 +- Stylet/WindowManager.cs | 2 +- 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 Stylet/IoC.cs diff --git a/Stylet/Bootstrapper.cs b/Stylet/Bootstrapper.cs index c2e9a1c..9dc93a3 100644 --- a/Stylet/Bootstrapper.cs +++ b/Stylet/Bootstrapper.cs @@ -27,10 +27,27 @@ namespace Stylet this.Application.Startup += (o, e) => { - new WindowManager().ShowWindow(Activator.CreateInstance(typeof(TRootViewModel))); + IoC.Get().ShowWindow(IoC.Get()); }; + + IoC.GetInstance = this.GetInstance; + IoC.GetAllInstances = this.GetAllInstances; + IoC.BuildUp = this.BuildUp; } + protected virtual object GetInstance(Type service, string key = null) + { + if (service == typeof(IWindowManager)) service = typeof(WindowManager); + return Activator.CreateInstance(service); + } + + protected virtual IEnumerable GetAllInstances(Type service) + { + return new[] { Activator.CreateInstance(service) }; + } + + protected virtual void BuildUp(object 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/IoC.cs b/Stylet/IoC.cs new file mode 100644 index 0000000..4bda045 --- /dev/null +++ b/Stylet/IoC.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stylet +{ + public static class IoC + { + public static Func GetInstance = (service, key) => { throw new InvalidOperationException("IoC is not initialized"); }; + public static Func> GetAllInstances = service => { throw new InvalidOperationException("IoC is not initialized"); }; + public static Action BuildUp = instance => { throw new InvalidOperationException("IoC is not initialized"); }; + + public static T Get(string key = null) + { + return (T)GetInstance(typeof(T), key); + } + + public static IEnumerable GetAll() + { + return GetAllInstances(typeof(T)).Cast(); + } + } +} diff --git a/Stylet/Stylet.csproj b/Stylet/Stylet.csproj index 8117ebc..cb60be0 100644 --- a/Stylet/Stylet.csproj +++ b/Stylet/Stylet.csproj @@ -54,6 +54,7 @@ Designer + SubView.xaml diff --git a/Stylet/ViewLocator.cs b/Stylet/ViewLocator.cs index 318a8c7..192ec15 100644 --- a/Stylet/ViewLocator.cs +++ b/Stylet/ViewLocator.cs @@ -23,7 +23,7 @@ namespace Stylet if (viewType.IsInterface || viewType.IsAbstract || !typeof(UIElement).IsAssignableFrom(viewType)) throw new Exception(String.Format("Found type for view : {0}, but it wasn't a class derived from UIElement", viewType.Name)); - var view = (UIElement)Activator.CreateInstance(viewType); + var view = (UIElement)IoC.GetInstance(viewType, null); // If it doesn't have a code-behind, this won't be called var initializer = viewType.GetMethod("InitializeComponent", BindingFlags.Public | BindingFlags.Instance); diff --git a/Stylet/WindowManager.cs b/Stylet/WindowManager.cs index 13455bc..1fb897a 100644 --- a/Stylet/WindowManager.cs +++ b/Stylet/WindowManager.cs @@ -10,7 +10,7 @@ namespace Stylet { public interface IWindowManager { - + void ShowWindow(object viewModel, IDictionary settings = null); } public class WindowManager : IWindowManager