Add super-simple IoC container

This commit is contained in:
Antony Male 2014-02-04 18:21:52 +00:00
parent 9543bb28c1
commit 7ea92e3673
5 changed files with 46 additions and 3 deletions

View File

@ -27,10 +27,27 @@ namespace Stylet
this.Application.Startup += (o, e) => this.Application.Startup += (o, e) =>
{ {
new WindowManager().ShowWindow(Activator.CreateInstance(typeof(TRootViewModel))); IoC.Get<IWindowManager>().ShowWindow(IoC.Get<TRootViewModel>());
}; };
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<object> 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 OnStartup(object sender, StartupEventArgs e) { }
protected virtual void OnExit(object sender, EventArgs e) { } protected virtual void OnExit(object sender, EventArgs e) { }
protected virtual void OnUnhandledExecption(object sender, DispatcherUnhandledExceptionEventArgs e) { } protected virtual void OnUnhandledExecption(object sender, DispatcherUnhandledExceptionEventArgs e) { }

25
Stylet/IoC.cs Normal file
View File

@ -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<Type, string, object> GetInstance = (service, key) => { throw new InvalidOperationException("IoC is not initialized"); };
public static Func<Type, IEnumerable<object>> GetAllInstances = service => { throw new InvalidOperationException("IoC is not initialized"); };
public static Action<object> BuildUp = instance => { throw new InvalidOperationException("IoC is not initialized"); };
public static T Get<T>(string key = null)
{
return (T)GetInstance(typeof(T), key);
}
public static IEnumerable<T> GetAll<T>()
{
return GetAllInstances(typeof(T)).Cast<T>();
}
}
}

View File

@ -54,6 +54,7 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="Bootstrapper.cs" /> <Compile Include="Bootstrapper.cs" />
<Compile Include="IoC.cs" />
<Compile Include="SubView.xaml.cs"> <Compile Include="SubView.xaml.cs">
<DependentUpon>SubView.xaml</DependentUpon> <DependentUpon>SubView.xaml</DependentUpon>
</Compile> </Compile>

View File

@ -23,7 +23,7 @@ namespace Stylet
if (viewType.IsInterface || viewType.IsAbstract || !typeof(UIElement).IsAssignableFrom(viewType)) 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)); 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 // If it doesn't have a code-behind, this won't be called
var initializer = viewType.GetMethod("InitializeComponent", BindingFlags.Public | BindingFlags.Instance); var initializer = viewType.GetMethod("InitializeComponent", BindingFlags.Public | BindingFlags.Instance);

View File

@ -10,7 +10,7 @@ namespace Stylet
{ {
public interface IWindowManager public interface IWindowManager
{ {
void ShowWindow(object viewModel, IDictionary<string, object> settings = null);
} }
public class WindowManager : IWindowManager public class WindowManager : IWindowManager