From ca7deedfb9d8265bdfad220f1c7b1742cc772c63 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 8 Nov 2011 12:12:54 -0800 Subject: [PATCH] CentralDispatch is no longer static. --- NzbDrone.Core.Test/CentralDispatchFixture.cs | 29 +++-- .../InventoryProvider_IsMonitoredTest.cs | 2 +- NzbDrone.Core/CentralDispatch.cs | 116 ++++++++---------- .../Instrumentation/LogConfiguration.cs | 6 +- .../Providers/ExternalNotification/Growl.cs | 6 +- NzbDrone.Web/Global.asax.cs | 9 +- 6 files changed, 82 insertions(+), 86 deletions(-) diff --git a/NzbDrone.Core.Test/CentralDispatchFixture.cs b/NzbDrone.Core.Test/CentralDispatchFixture.cs index 937b25378..cc1895813 100644 --- a/NzbDrone.Core.Test/CentralDispatchFixture.cs +++ b/NzbDrone.Core.Test/CentralDispatchFixture.cs @@ -1,11 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using FluentAssertions; -using NLog; using NUnit.Framework; -using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.Indexer; using NzbDrone.Core.Providers.Jobs; @@ -21,10 +18,18 @@ namespace NzbDrone.Core.Test readonly IList indexers = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(IndexerBase))).ToList(); readonly IList jobs = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob))).ToList(); + private CentralDispatch centralDispatch; + + [SetUp] + public void Setup() + { + centralDispatch = new CentralDispatch(); + } + [Test] public void InitAppTest() { - CentralDispatch.NinjectKernel.Should().NotBeNull(); + centralDispatch.Kernel.Should().NotBeNull(); } [Test] @@ -37,7 +42,7 @@ namespace NzbDrone.Core.Test foreach (var provider in providers) { Console.WriteLine("Resolving " + provider.Name); - CentralDispatch.NinjectKernel.Get(provider).Should().NotBeNull(); + centralDispatch.Kernel.Get(provider).Should().NotBeNull(); } } @@ -47,7 +52,7 @@ namespace NzbDrone.Core.Test { //Assert - var registeredJobs = CentralDispatch.NinjectKernel.GetAll(); + var registeredJobs = centralDispatch.Kernel.GetAll(); jobs.Should().NotBeEmpty(); @@ -60,7 +65,7 @@ namespace NzbDrone.Core.Test { //Assert - var registeredIndexers = CentralDispatch.NinjectKernel.GetAll(); + var registeredIndexers = centralDispatch.Kernel.GetAll(); indexers.Should().NotBeEmpty(); @@ -71,26 +76,26 @@ namespace NzbDrone.Core.Test [Test] public void jobs_are_initialized() { - CentralDispatch.NinjectKernel.Get().All().Should().HaveSameCount(jobs); + centralDispatch.Kernel.Get().All().Should().HaveSameCount(jobs); } [Test] public void indexers_are_initialized() { - CentralDispatch.NinjectKernel.Get().All().Should().HaveSameCount(indexers); + centralDispatch.Kernel.Get().All().Should().HaveSameCount(indexers); } [Test] public void quality_profile_initialized() { - CentralDispatch.NinjectKernel.Get().All().Should().HaveCount(2); + centralDispatch.Kernel.Get().All().Should().HaveCount(2); } [Test] public void JobProvider_should_be_singletone() { - var first = CentralDispatch.NinjectKernel.Get(); - var second = CentralDispatch.NinjectKernel.Get(); + var first = centralDispatch.Kernel.Get(); + var second = centralDispatch.Kernel.Get(); first.Should().BeSameAs(second); } diff --git a/NzbDrone.Core.Test/ProviderTests/InventoryProvider_IsMonitoredTest.cs b/NzbDrone.Core.Test/ProviderTests/InventoryProvider_IsMonitoredTest.cs index d1ece9501..333c1b36a 100644 --- a/NzbDrone.Core.Test/ProviderTests/InventoryProvider_IsMonitoredTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/InventoryProvider_IsMonitoredTest.cs @@ -26,7 +26,7 @@ namespace NzbDrone.Core.Test.ProviderTests private EpisodeParseResult parseResultSingle; [SetUp] - public new void Setup() + public void Setup() { parseResultMulti = new EpisodeParseResult() { diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index a10440fcc..341bd0284 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -1,9 +1,6 @@ using System; using System.Diagnostics; -using System.IO; using System.Linq; -using System.Reflection; -using System.Web.Hosting; using Ninject; using NLog; using NzbDrone.Core.Datastore; @@ -17,111 +14,104 @@ using PetaPoco; namespace NzbDrone.Core { - public static class CentralDispatch + public class CentralDispatch { - private static StandardKernel _kernel; - private static readonly Object KernelLock = new object(); + private readonly Object KernelLock = new object(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public static StandardKernel NinjectKernel + public CentralDispatch() { - get - { - if (_kernel == null) - { - InitializeApp(); - } - return _kernel; - } + InitializeApp(); } - public static void InitializeApp() + public StandardKernel Kernel { get; private set; } + + private void InitializeApp() { BindKernel(); - _kernel.Get().Setup(); + Kernel.Get().Setup(); - var mainConnectionString = _kernel.Get().MainConnectionString; + var mainConnectionString = Kernel.Get().MainConnectionString; MigrationsHelper.Run(mainConnectionString, true); - LogConfiguration.RegisterDatabaseLogger(_kernel.Get()); + LogConfiguration.RegisterDatabaseLogger(Kernel.Get()); - _kernel.Get().SetupDefaultProfiles(); - _kernel.Get().SetupDefault(); - _kernel.Get().CreateDefaultConfigFile(); + Kernel.Get().SetupDefaultProfiles(); + Kernel.Get().SetupDefault(); + Kernel.Get().CreateDefaultConfigFile(); BindExternalNotifications(); BindIndexers(); BindJobs(); } - private static void BindKernel() + private void BindKernel() { lock (KernelLock) { Logger.Debug("Binding Ninject's Kernel"); - _kernel = new StandardKernel(); + Kernel = new StandardKernel(); - var connection = _kernel.Get(); + var connection = Kernel.Get(); - _kernel.Bind().ToMethod(c => connection.GetMainPetaPocoDb()).InTransientScope(); - _kernel.Bind().ToMethod(c => connection.GetLogPetaPocoDb(false)).WhenInjectedInto().InSingletonScope(); - _kernel.Bind().ToMethod(c => connection.GetLogPetaPocoDb()).WhenInjectedInto().InSingletonScope(); + Kernel.Bind().ToMethod(c => connection.GetMainPetaPocoDb()).InTransientScope(); + Kernel.Bind().ToMethod(c => connection.GetLogPetaPocoDb(false)).WhenInjectedInto().InSingletonScope(); + Kernel.Bind().ToMethod(c => connection.GetLogPetaPocoDb()).WhenInjectedInto().InSingletonScope(); - _kernel.Bind().ToSelf().InSingletonScope(); + Kernel.Bind().ToSelf().InSingletonScope(); } } - private static void BindIndexers() + private void BindIndexers() { - _kernel.Bind().To(); - _kernel.Bind().To(); - _kernel.Bind().To(); - _kernel.Bind().To(); + Kernel.Bind().To(); + Kernel.Bind().To(); + Kernel.Bind().To(); + Kernel.Bind().To(); - var indexers = _kernel.GetAll(); - _kernel.Get().InitializeIndexers(indexers.ToList()); + var indexers = Kernel.GetAll(); + Kernel.Get().InitializeIndexers(indexers.ToList()); } - private static void BindJobs() + private void BindJobs() { - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); - _kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); - _kernel.Get().Initialize(); - _kernel.Get().StartTimer(30); + Kernel.Get().Initialize(); + Kernel.Get().StartTimer(30); } - private static void BindExternalNotifications() + private void BindExternalNotifications() { - _kernel.Bind().To(); - _kernel.Bind().To(); - _kernel.Bind().To(); - _kernel.Bind().To(); + Kernel.Bind().To(); + Kernel.Bind().To(); + Kernel.Bind().To(); - var notifiers = _kernel.GetAll(); - _kernel.Get().InitializeNotifiers(notifiers.ToList()); + var notifiers = Kernel.GetAll(); + Kernel.Get().InitializeNotifiers(notifiers.ToList()); } /// /// Forces IISExpress process to exit with the host application /// - public static void DedicateToHost() + public void DedicateToHost() { try { diff --git a/NzbDrone.Core/Instrumentation/LogConfiguration.cs b/NzbDrone.Core/Instrumentation/LogConfiguration.cs index 8ce6037e7..0baba5855 100644 --- a/NzbDrone.Core/Instrumentation/LogConfiguration.cs +++ b/NzbDrone.Core/Instrumentation/LogConfiguration.cs @@ -10,10 +10,12 @@ namespace NzbDrone.Core.Instrumentation public class LogConfiguration { private readonly PathProvider _pathProvider; + private readonly DatabaseTarget _databaseTarget; - public LogConfiguration(PathProvider pathProvider) + public LogConfiguration(PathProvider pathProvider, DatabaseTarget databaseTarget) { _pathProvider = pathProvider; + _databaseTarget = databaseTarget; } public void Setup() @@ -28,7 +30,7 @@ namespace NzbDrone.Core.Instrumentation Common.LogConfiguration.RegisterConsoleLogger(LogLevel.Info, "NzbDrone.Web.MvcApplication"); Common.LogConfiguration.RegisterConsoleLogger(LogLevel.Info, "NzbDrone.Core.CentralDispatch"); - LogManager.ConfigurationReloaded += ((s, e) => RegisterDatabaseLogger(CentralDispatch.NinjectKernel.Get())); + LogManager.ConfigurationReloaded += ((s, e) => RegisterDatabaseLogger(_databaseTarget)); } public static void RegisterDatabaseLogger(DatabaseTarget databaseTarget) diff --git a/NzbDrone.Core/Providers/ExternalNotification/Growl.cs b/NzbDrone.Core/Providers/ExternalNotification/Growl.cs index d4e887dd2..79cdc23c5 100644 --- a/NzbDrone.Core/Providers/ExternalNotification/Growl.cs +++ b/NzbDrone.Core/Providers/ExternalNotification/Growl.cs @@ -9,8 +9,6 @@ namespace NzbDrone.Core.Providers.ExternalNotification { private readonly GrowlProvider _growlProvider; - private readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public Growl(ConfigProvider configProvider, GrowlProvider growlProvider) : base(configProvider) { @@ -41,7 +39,7 @@ namespace NzbDrone.Core.Providers.ExternalNotification catch (Exception ex) { - Logger.WarnException(ex.Message, ex); + _logger.WarnException(ex.Message, ex); throw; } } @@ -65,7 +63,7 @@ namespace NzbDrone.Core.Providers.ExternalNotification catch (Exception ex) { - Logger.WarnException(ex.Message, ex); + _logger.WarnException(ex.Message, ex); throw; } } diff --git a/NzbDrone.Web/Global.asax.cs b/NzbDrone.Web/Global.asax.cs index ef314499a..690913b65 100644 --- a/NzbDrone.Web/Global.asax.cs +++ b/NzbDrone.Web/Global.asax.cs @@ -54,13 +54,14 @@ namespace NzbDrone.Web protected override IKernel CreateKernel() { - var kernel = CentralDispatch.NinjectKernel; + + var dispatch = new CentralDispatch(); Logger.Info("NzbDrone Starting up."); - CentralDispatch.DedicateToHost(); + dispatch.DedicateToHost(); - kernel.Load(Assembly.GetExecutingAssembly()); - return kernel; + dispatch.Kernel.Load(Assembly.GetExecutingAssembly()); + return dispatch.Kernel; }