From b8d5c718429f1325111834b8b95698fc9c9ba47d Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Sat, 23 Feb 2013 02:57:11 -0500 Subject: type discovery without attributes --- MediaBrowser.UI/App.xaml.cs | 96 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 11 deletions(-) (limited to 'MediaBrowser.UI/App.xaml.cs') diff --git a/MediaBrowser.UI/App.xaml.cs b/MediaBrowser.UI/App.xaml.cs index b98ce8e6c..bcaa0529e 100644 --- a/MediaBrowser.UI/App.xaml.cs +++ b/MediaBrowser.UI/App.xaml.cs @@ -117,6 +117,17 @@ namespace MediaBrowser.UI get { return "MediaBrowser.UI.Uninstall.exe"; } } + /// + /// The container + /// + private SimpleInjector.Container _container = new SimpleInjector.Container(); + + /// + /// Gets or sets the iso manager. + /// + /// The iso manager. + private IIsoManager IsoManager { get; set; } + /// /// Gets the instance. /// @@ -290,15 +301,6 @@ namespace MediaBrowser.UI InitializeComponent(); } - - /// - /// Instantiates the kernel. - /// - /// IKernel. - protected IKernel InstantiateKernel() - { - return new UIKernel(this, new PismoIsoManager(Logger), Logger); - } /// /// Instantiates the main window. @@ -405,7 +407,9 @@ namespace MediaBrowser.UI // Without this the app will shutdown after the splash screen closes ShutdownMode = ShutdownMode.OnExplicitShutdown; - Kernel = InstantiateKernel(); + RegisterResources(); + + Kernel = new UIKernel(this, Logger); try { @@ -614,7 +618,8 @@ namespace MediaBrowser.UI { try { - ServerConfiguration = await ApiClient.GetServerConfigurationAsync(); + var b = Kernel; + //ServerConfiguration = await ApiClient.GetServerConfigurationAsync(); } catch (HttpException ex) { @@ -1010,6 +1015,19 @@ namespace MediaBrowser.UI return new ApplicationUpdateCheck().CheckForApplicationUpdate(cancellationToken, progress); } + /// + /// Registers resources that classes will depend on + /// + private void RegisterResources() + { + Register(this); + Register(Logger); + + IsoManager = new PismoIsoManager(Logger); + + Register(IsoManager); + } + /// /// Updates the application. /// @@ -1020,5 +1038,61 @@ namespace MediaBrowser.UI { return new ApplicationUpdater().UpdateApplication(cancellationToken, progress); } + + /// + /// Creates an instance of type and resolves all constructor dependancies + /// + /// The type. + /// System.Object. + public object CreateInstance(Type type) + { + try + { + return _container.GetInstance(type); + } + catch + { + Logger.Error("Error creating {0}", type.Name); + + throw; + } + } + + /// + /// Registers the specified obj. + /// + /// + /// The obj. + public void Register(T obj) + where T : class + { + _container.RegisterSingle(obj); + } + + /// + /// Resolves this instance. + /// + /// + /// ``0. + public T Resolve() + { + return (T)_container.GetRegistration(typeof(T), true).GetInstance(); + } + + /// + /// Resolves this instance. + /// + /// + /// ``0. + public T TryResolve() + { + var result = _container.GetRegistration(typeof(T), false); + + if (result == null) + { + return default(T); + } + return (T)result.GetInstance(); + } } } -- cgit v1.2.3