From c165f37bb96ad40d863a436795c21b9085f3fde9 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Fri, 22 Feb 2013 22:49:00 -0500 Subject: [PATCH] more dependancy injection. still just beginning --- MediaBrowser.Common/Kernel/BaseKernel.cs | 52 ++--------- .../Kernel/IApplicationHost.cs | 21 +++++ .../MediaBrowser.Common.csproj | 3 - MediaBrowser.Common/packages.config | 1 - MediaBrowser.Controller/Kernel.cs | 75 ++++------------ .../MediaBrowser.Controller.csproj | 4 - .../MediaInfo/FFMpegManager.cs | 90 +------------------ .../MediaInfo/FFMpegVideoImageProvider.cs | 19 +++- .../MediaInfo/FFProbeVideoInfoProvider.cs | 15 +++- MediaBrowser.Controller/packages.config | 1 - MediaBrowser.ServerApplication/App.xaml.cs | 67 +++++++++++++- .../MediaBrowser.ServerApplication.csproj | 4 + .../packages.config | 1 + 13 files changed, 147 insertions(+), 206 deletions(-) diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index 202bd3cab..85954cb82 100644 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ b/MediaBrowser.Common/Kernel/BaseKernel.cs @@ -18,7 +18,6 @@ using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; -using SimpleInjector; namespace MediaBrowser.Common.Kernel { @@ -223,12 +222,6 @@ namespace MediaBrowser.Common.Kernel /// The task manager. public TaskManager TaskManager { get; private set; } - /// - /// Gets the iso manager. - /// - /// The iso manager. - public IIsoManager IsoManager { get; private set; } - /// /// Gets the rest services. /// @@ -347,20 +340,14 @@ namespace MediaBrowser.Common.Kernel /// Initializes a new instance of the class. /// /// The app host. - /// The iso manager. /// The logger. /// isoManager - protected BaseKernel(IApplicationHost appHost, IIsoManager isoManager, ILogger logger) + protected BaseKernel(IApplicationHost appHost, ILogger logger) { if (appHost == null) { throw new ArgumentNullException("appHost"); } - - if (isoManager == null) - { - throw new ArgumentNullException("isoManager"); - } if (logger == null) { @@ -368,7 +355,6 @@ namespace MediaBrowser.Common.Kernel } ApplicationHost = appHost; - IsoManager = isoManager; Logger = logger; } @@ -471,11 +457,6 @@ namespace MediaBrowser.Common.Kernel CompositionContainer.Catalog.Dispose(); } - /// - /// The ioc container - /// - private readonly Container _iocContainer = new Container(); - /// /// Composes the parts. /// @@ -486,19 +467,18 @@ namespace MediaBrowser.Common.Kernel CompositionContainer = GetSafeCompositionContainer(concreteTypes.Select(i => new TypeCatalog(i))); - ComposeExportedValues(CompositionContainer, _iocContainer); + RegisterExportedValues(CompositionContainer); CompositionContainer.ComposeParts(this); - ComposePartsWithIocContainer(concreteTypes, _iocContainer); + FindParts(concreteTypes); } /// /// Composes the parts with ioc container. /// /// All types. - /// The container. - protected virtual void ComposePartsWithIocContainer(Type[] allTypes, Container container) + protected virtual void FindParts(Type[] allTypes) { RestServices = GetExports(allTypes); WebSocketListeners = GetExports(allTypes); @@ -530,21 +510,20 @@ namespace MediaBrowser.Common.Kernel /// System.Object. private object Instantiate(Type type) { - return _iocContainer.GetInstance(type); + return ApplicationHost.CreateInstance(type); } /// /// Composes the exported values. /// /// The container. - /// - protected virtual void ComposeExportedValues(CompositionContainer container, Container iocContainer) + protected virtual void RegisterExportedValues(CompositionContainer container) { + ApplicationHost.Register(this); + container.ComposeExportedValue("logger", Logger); container.ComposeExportedValue("appHost", ApplicationHost); - - iocContainer.RegisterSingle(Logger); - iocContainer.RegisterSingle(ApplicationHost); + container.ComposeExportedValue("isoManager", ApplicationHost.Resolve()); } /// @@ -739,7 +718,6 @@ namespace MediaBrowser.Common.Kernel { DisposeTcpManager(); DisposeTaskManager(); - DisposeIsoManager(); DisposeHttpManager(); DisposeComposableParts(); @@ -753,18 +731,6 @@ namespace MediaBrowser.Common.Kernel } } - /// - /// Disposes the iso manager. - /// - private void DisposeIsoManager() - { - if (IsoManager != null) - { - IsoManager.Dispose(); - IsoManager = null; - } - } - /// /// Disposes the TCP manager. /// diff --git a/MediaBrowser.Common/Kernel/IApplicationHost.cs b/MediaBrowser.Common/Kernel/IApplicationHost.cs index 63c63eb3d..d2b1ab7a2 100644 --- a/MediaBrowser.Common/Kernel/IApplicationHost.cs +++ b/MediaBrowser.Common/Kernel/IApplicationHost.cs @@ -43,5 +43,26 @@ namespace MediaBrowser.Common.Kernel /// /// Task. Task UpdateApplication(CancellationToken cancellationToken, IProgress progress); + + /// + /// Creates an instance of type and resolves all constructor dependancies + /// + /// The type. + /// System.Object. + object CreateInstance(Type type); + + /// + /// Registers a service that other classes can use as a dependancy. + /// + /// + /// The obj. + void Register(T obj) where T : class; + + /// + /// Resolves this instance. + /// + /// + /// ``0. + T Resolve() where T : class; } } diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index ea388e33b..cae26b3df 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -88,9 +88,6 @@ False ..\packages\ServiceStack.Text.3.9.37\lib\net35\ServiceStack.Text.dll - - ..\packages\SimpleInjector.2.0.0-beta5\lib\net40-client\SimpleInjector.dll - diff --git a/MediaBrowser.Common/packages.config b/MediaBrowser.Common/packages.config index 96fdb618b..536640094 100644 --- a/MediaBrowser.Common/packages.config +++ b/MediaBrowser.Common/packages.config @@ -13,5 +13,4 @@ - \ No newline at end of file diff --git a/MediaBrowser.Controller/Kernel.cs b/MediaBrowser.Controller/Kernel.cs index 6bd48a502..ebf3e724d 100644 --- a/MediaBrowser.Controller/Kernel.cs +++ b/MediaBrowser.Controller/Kernel.cs @@ -28,7 +28,6 @@ using System.ComponentModel.Composition.Hosting; using System.Linq; using System.Threading; using System.Threading.Tasks; -using SimpleInjector; namespace MediaBrowser.Controller { @@ -301,48 +300,16 @@ namespace MediaBrowser.Controller get { return 7359; } } - /// - /// Gets or sets the zip client. - /// - /// The zip client. - private IZipClient ZipClient { get; set; } - - /// - /// Gets or sets the bluray examiner. - /// - /// The bluray examiner. - private IBlurayExaminer BlurayExaminer { get; set; } - /// /// Creates a kernel based on a Data path, which is akin to our current programdata path /// /// The app host. - /// The iso manager. - /// The zip client. - /// The bluray examiner. /// The logger. /// isoManager - public Kernel(IApplicationHost appHost, IIsoManager isoManager, IZipClient zipClient, IBlurayExaminer blurayExaminer, ILogger logger) - : base(appHost, isoManager, logger) + public Kernel(IApplicationHost appHost, ILogger logger) + : base(appHost, logger) { - if (isoManager == null) - { - throw new ArgumentNullException("isoManager"); - } - - if (zipClient == null) - { - throw new ArgumentNullException("zipClient"); - } - - if (blurayExaminer == null) - { - throw new ArgumentNullException("blurayExaminer"); - } - Instance = this; - ZipClient = zipClient; - BlurayExaminer = blurayExaminer; // For now there's no real way to inject this properly BaseItem.Logger = logger; @@ -356,26 +323,22 @@ namespace MediaBrowser.Controller /// Composes the exported values. /// /// The container. - /// The _ioc container. - protected override void ComposeExportedValues(CompositionContainer container, Container iocContainer) + protected override void RegisterExportedValues(CompositionContainer container) { - base.ComposeExportedValues(container, iocContainer); - container.ComposeExportedValue("kernel", this); - container.ComposeExportedValue("blurayExaminer", BlurayExaminer); - iocContainer.RegisterSingle(this); - iocContainer.RegisterSingle(BlurayExaminer); + ApplicationHost.Register(this); + + base.RegisterExportedValues(container); } /// /// Composes the parts with ioc container. /// /// All types. - /// The container. - protected override void ComposePartsWithIocContainer(Type[] allTypes, Container container) + protected override void FindParts(Type[] allTypes) { - base.ComposePartsWithIocContainer(allTypes, container); + base.FindParts(allTypes); EntityResolutionIgnoreRules = GetExports(allTypes); UserDataRepositories = GetExports(allTypes); @@ -395,24 +358,22 @@ namespace MediaBrowser.Controller /// Task. protected override async Task ReloadInternal() { - Logger.Info("Extracting tools"); - // Reset these so that they can be lazy loaded again Users = null; RootFolder = null; - ReloadResourcePools(); - InstallationManager = new InstallationManager(this, ZipClient, Logger); - LibraryManager = new LibraryManager(this, Logger); - UserManager = new UserManager(this, Logger); - FFMpegManager = new FFMpegManager(this, ZipClient, Logger); - ImageManager = new ImageManager(this, Logger); - ProviderManager = new ProviderManager(this, Logger); - UserDataManager = new UserDataManager(this, Logger); - PluginSecurityManager = new PluginSecurityManager(this); - await base.ReloadInternal().ConfigureAwait(false); + ReloadResourcePools(); + InstallationManager = (InstallationManager)ApplicationHost.CreateInstance(typeof(InstallationManager)); + FFMpegManager = (FFMpegManager)ApplicationHost.CreateInstance(typeof(FFMpegManager)); + LibraryManager = (LibraryManager)ApplicationHost.CreateInstance(typeof(LibraryManager)); + UserManager = (UserManager)ApplicationHost.CreateInstance(typeof(UserManager)); + ImageManager = (ImageManager)ApplicationHost.CreateInstance(typeof(ImageManager)); + ProviderManager = (ProviderManager)ApplicationHost.CreateInstance(typeof(ProviderManager)); + UserDataManager = (UserDataManager)ApplicationHost.CreateInstance(typeof(UserDataManager)); + PluginSecurityManager = (PluginSecurityManager)ApplicationHost.CreateInstance(typeof(PluginSecurityManager)); + ReloadFileSystemManager(); await UserManager.RefreshUsersMetadata(CancellationToken.None).ConfigureAwait(false); diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index cfd14f2b7..f9765d852 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -63,10 +63,6 @@ False ..\packages\protobuf-net.2.0.0.621\lib\net40\protobuf-net.dll - - False - ..\packages\SimpleInjector.2.0.0-beta5\lib\net40-client\SimpleInjector.dll - diff --git a/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs b/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs index 9bc1ebaa5..7ef70ea42 100644 --- a/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs +++ b/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs @@ -4,9 +4,9 @@ using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Serialization; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; -using MediaBrowser.Controller.Library; using MediaBrowser.Model.Entities; using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; using System; using System.Collections.Generic; using System.ComponentModel; @@ -17,7 +17,6 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; -using MediaBrowser.Model.Logging; namespace MediaBrowser.Controller.MediaInfo { @@ -26,22 +25,6 @@ namespace MediaBrowser.Controller.MediaInfo /// public class FFMpegManager : BaseManager { - /// - /// Holds the list of new items to generate chapter image for when the NewItemTimer expires - /// - private readonly List + + False + ..\packages\SimpleInjector.2.0.0-beta5\lib\net40-client\SimpleInjector.dll + diff --git a/MediaBrowser.ServerApplication/packages.config b/MediaBrowser.ServerApplication/packages.config index c9cf3ee00..e1beae6da 100644 --- a/MediaBrowser.ServerApplication/packages.config +++ b/MediaBrowser.ServerApplication/packages.config @@ -3,5 +3,6 @@ + \ No newline at end of file