From 3f958109bbaba39d1fe7b844c7730014467f101e Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Fri, 19 Apr 2013 17:05:48 -0700 Subject: [PATCH] removed Autofac from update project. --- NzbDrone.Api/NzbDrone.Api.csproj | 3 + NzbDrone.Api/Series/SeriesModule.cs | 5 +- NzbDrone.Api/Series/SeriesResource.cs | 10 +- NzbDrone.Api/packages.config | 1 + NzbDrone.App.Test/ContainerFixture.cs | 6 +- NzbDrone.Common/ContainerBuilderBase.cs | 71 +++++++++++++ NzbDrone.Common/NzbDrone.Common.csproj | 3 +- NzbDrone.Core/packages.config | 2 +- NzbDrone.Integration.Test/SmokeTestBase.cs | 2 +- NzbDrone.Update/NzbDrone.Update.csproj | 8 +- NzbDrone.Update/Program.cs | 10 +- NzbDrone.Update/UpdateContainerBuilder.cs | 19 ++++ NzbDrone.Update/packages.config | 1 - NzbDrone.ncrunchsolution | 1 + NzbDrone/AppMain.cs | 2 +- NzbDrone/ContainerBuilder.cs | 118 --------------------- NzbDrone/MainAppContainerBuilder.cs | 61 +++++++++++ NzbDrone/NzbDrone.csproj | 2 +- 18 files changed, 174 insertions(+), 151 deletions(-) create mode 100644 NzbDrone.Common/ContainerBuilderBase.cs create mode 100644 NzbDrone.Update/UpdateContainerBuilder.cs delete mode 100644 NzbDrone/ContainerBuilder.cs create mode 100644 NzbDrone/MainAppContainerBuilder.cs diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index 5d3e88e44..7658d8bd1 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -70,6 +70,9 @@ False ..\packages\NLog.2.0.1.2\lib\net40\NLog.dll + + ..\packages\ValueInjecter.2.3.3\lib\net35\Omu.ValueInjecter.dll + diff --git a/NzbDrone.Api/Series/SeriesModule.cs b/NzbDrone.Api/Series/SeriesModule.cs index 166357fa1..43e40d9a2 100644 --- a/NzbDrone.Api/Series/SeriesModule.cs +++ b/NzbDrone.Api/Series/SeriesModule.cs @@ -6,16 +6,15 @@ using AutoMapper; using FluentValidation; using Nancy; using NzbDrone.Api.Extensions; +using NzbDrone.Api.REST; using NzbDrone.Common; -using NzbDrone.Core.Datastore; -using NzbDrone.Core.Jobs.Implementations; using NzbDrone.Core.Tv; using NzbDrone.Core.Jobs; using NzbDrone.Core.Model; namespace NzbDrone.Api.Series { - public class SeriesModule : NzbDroneApiModule + public class SeriesModule : RestModule { private readonly ISeriesService _seriesService; private readonly IJobController _jobProvider; diff --git a/NzbDrone.Api/Series/SeriesResource.cs b/NzbDrone.Api/Series/SeriesResource.cs index a83b0e8b6..478bc9a39 100644 --- a/NzbDrone.Api/Series/SeriesResource.cs +++ b/NzbDrone.Api/Series/SeriesResource.cs @@ -1,17 +1,11 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using NzbDrone.Api.QualityProfiles; -using NzbDrone.Core.Model; -using NzbDrone.Core.Qualities; +using NzbDrone.Api.REST; namespace NzbDrone.Api.Series { - public class SeriesResource + public class SeriesResource : RestResource { - public Int32 Id { get; set; } - //Todo: Sorters should be done completely on the client //Todo: Is there an easy way to keep IgnoreArticlesWhenSorting in sync between, Series, History, Missing? //Todo: We should get the entire QualityProfile instead of ID and Name separately diff --git a/NzbDrone.Api/packages.config b/NzbDrone.Api/packages.config index 3144ad997..3778d25db 100644 --- a/NzbDrone.Api/packages.config +++ b/NzbDrone.Api/packages.config @@ -4,4 +4,5 @@ + \ No newline at end of file diff --git a/NzbDrone.App.Test/ContainerFixture.cs b/NzbDrone.App.Test/ContainerFixture.cs index 16bb5e02e..bcf6a5c92 100644 --- a/NzbDrone.App.Test/ContainerFixture.cs +++ b/NzbDrone.App.Test/ContainerFixture.cs @@ -14,19 +14,19 @@ namespace NzbDrone.App.Test [Test] public void should_be_able_to_resolve_event_handlers() { - ContainerBuilder.BuildNzbDroneContainer().Resolve>().Should().NotBeEmpty(); + MainAppContainerBuilder.BuildContainer().Resolve>().Should().NotBeEmpty(); } [Test] public void should_be_able_to_resolve_indexers() { - ContainerBuilder.BuildNzbDroneContainer().Resolve>().Should().NotBeEmpty(); + MainAppContainerBuilder.BuildContainer().Resolve>().Should().NotBeEmpty(); } [Test] public void should_be_able_to_resolve_downlodclients() { - ContainerBuilder.BuildNzbDroneContainer().Resolve>().Should().NotBeEmpty(); + MainAppContainerBuilder.BuildContainer().Resolve>().Should().NotBeEmpty(); } } } \ No newline at end of file diff --git a/NzbDrone.Common/ContainerBuilderBase.cs b/NzbDrone.Common/ContainerBuilderBase.cs new file mode 100644 index 000000000..fefb85333 --- /dev/null +++ b/NzbDrone.Common/ContainerBuilderBase.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using TinyIoC; + +namespace NzbDrone.Common +{ + public abstract class ContainerBuilderBase + { + protected TinyIoCContainer Container; + private readonly List _loadedTypes; + + protected ContainerBuilderBase(params string[] assemblies) + { + Container = new TinyIoCContainer(); + + _loadedTypes = new List(); + + foreach (var assembly in assemblies) + { + _loadedTypes.AddRange(Assembly.Load(assembly).GetTypes()); + } + + AutoRegisterInterfaces(); + } + + private void AutoRegisterInterfaces() + { + var interfaces = _loadedTypes.Where(t => t.IsInterface); + + foreach (var contract in interfaces) + { + AutoRegisterImplementations(contract); + } + } + + protected void AutoRegisterImplementations() + { + AutoRegisterImplementations(typeof(TContract)); + } + + private void AutoRegisterImplementations(Type contractType) + { + var implementations = GetImplementations(contractType).ToList(); + + if (implementations.Count == 0) + { + return; + } + if (implementations.Count == 1) + { + Container.Register(contractType, implementations.Single()).AsMultiInstance(); + } + else + { + Container.RegisterMultiple(contractType, implementations).AsMultiInstance(); + } + } + + private IEnumerable GetImplementations(Type contractType) + { + return _loadedTypes + .Where(implementation => + contractType.IsAssignableFrom(implementation) && + !implementation.IsInterface && + !implementation.IsAbstract + ); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 2e8709719..3013d0545 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -1,4 +1,4 @@ - + Debug @@ -84,6 +84,7 @@ + diff --git a/NzbDrone.Core/packages.config b/NzbDrone.Core/packages.config index 5e6b29805..6afbdc990 100644 --- a/NzbDrone.Core/packages.config +++ b/NzbDrone.Core/packages.config @@ -1,4 +1,4 @@ - + diff --git a/NzbDrone.Integration.Test/SmokeTestBase.cs b/NzbDrone.Integration.Test/SmokeTestBase.cs index 6486015d6..a9d981672 100644 --- a/NzbDrone.Integration.Test/SmokeTestBase.cs +++ b/NzbDrone.Integration.Test/SmokeTestBase.cs @@ -69,7 +69,7 @@ namespace NzbDrone.Integration.Test [SetUp] public void SmokeTestSetup() { - Container = ContainerBuilder.BuildNzbDroneContainer(); + Container = MainAppContainerBuilder.BuildContainer(); InitDatabase(); diff --git a/NzbDrone.Update/NzbDrone.Update.csproj b/NzbDrone.Update/NzbDrone.Update.csproj index 1d65081f4..cb7ee1597 100644 --- a/NzbDrone.Update/NzbDrone.Update.csproj +++ b/NzbDrone.Update/NzbDrone.Update.csproj @@ -55,13 +55,6 @@ MinimumRecommendedRules.ruleset - - False - ..\packages\Autofac.3.0.1\lib\net40\Autofac.dll - - - ..\packages\Autofac.3.0.1\lib\net40\Autofac.Configuration.dll - False ..\packages\Exceptron.Client.1.0.20\lib\net20\Exceptron.Client.dll @@ -84,6 +77,7 @@ + diff --git a/NzbDrone.Update/Program.cs b/NzbDrone.Update/Program.cs index daf500e89..17311ac4b 100644 --- a/NzbDrone.Update/Program.cs +++ b/NzbDrone.Update/Program.cs @@ -1,9 +1,9 @@ using System; using System.IO; -using Autofac; using NLog; using NzbDrone.Common; using NzbDrone.Update.Providers; +using TinyIoC; namespace NzbDrone.Update { @@ -11,7 +11,7 @@ namespace NzbDrone.Update { private readonly UpdateProvider _updateProvider; private readonly ProcessProvider _processProvider; - private static IContainer _container; + private static TinyIoCContainer _container; private static readonly Logger logger = LogManager.GetCurrentClassLogger(); @@ -26,10 +26,8 @@ namespace NzbDrone.Update try { Console.WriteLine("Starting NzbDrone Update Client"); - var builder = new ContainerBuilder(); - builder.RegisterAssemblyTypes(typeof(UpdateProvider).Assembly).SingleInstance(); - builder.RegisterAssemblyTypes(typeof(RestProvider).Assembly).SingleInstance(); - _container = builder.Build(); + + _container = UpdateContainerBuilder.Build(); logger.Info("Updating NzbDrone to version {0}", _container.Resolve().Version); _container.Resolve().Start(args); diff --git a/NzbDrone.Update/UpdateContainerBuilder.cs b/NzbDrone.Update/UpdateContainerBuilder.cs new file mode 100644 index 000000000..102b32b22 --- /dev/null +++ b/NzbDrone.Update/UpdateContainerBuilder.cs @@ -0,0 +1,19 @@ +using NzbDrone.Common; +using TinyIoC; + +namespace NzbDrone.Update +{ + public class UpdateContainerBuilder : ContainerBuilderBase + { + public UpdateContainerBuilder() + : base("NzbDrone.Update", "NzbDrone.Common") + { + + } + + public static TinyIoCContainer Build() + { + return new UpdateContainerBuilder().Container; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Update/packages.config b/NzbDrone.Update/packages.config index 789e746ef..974363ad4 100644 --- a/NzbDrone.Update/packages.config +++ b/NzbDrone.Update/packages.config @@ -1,6 +1,5 @@  - diff --git a/NzbDrone.ncrunchsolution b/NzbDrone.ncrunchsolution index c725a019c..969da6dd4 100644 --- a/NzbDrone.ncrunchsolution +++ b/NzbDrone.ncrunchsolution @@ -2,6 +2,7 @@ 1 True true + true UseDynamicAnalysis Disabled Disabled diff --git a/NzbDrone/AppMain.cs b/NzbDrone/AppMain.cs index 11014e85c..19a94281c 100644 --- a/NzbDrone/AppMain.cs +++ b/NzbDrone/AppMain.cs @@ -41,7 +41,7 @@ namespace NzbDrone return; } - var container = ContainerBuilder.BuildNzbDroneContainer(); + var container = MainAppContainerBuilder.BuildContainer(); container.Resolve().Route(args); } diff --git a/NzbDrone/ContainerBuilder.cs b/NzbDrone/ContainerBuilder.cs deleted file mode 100644 index 2fa521394..000000000 --- a/NzbDrone/ContainerBuilder.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using FluentMigrator.Runner; -using NLog; -using Nancy.Bootstrapper; -using NzbDrone.Api; -using NzbDrone.Common; -using NzbDrone.Common.Eventing; -using NzbDrone.Core.Datastore; -using NzbDrone.Core.Datastore.Migration.Framework; -using NzbDrone.Core.ExternalNotification; -using NzbDrone.Core.Organizer; -using NzbDrone.Core.RootFolders; -using TinyIoC; - -namespace NzbDrone -{ - public static class ContainerBuilder - { - private static readonly Logger Logger = LogManager.GetLogger("ContainerBuilder"); - private static readonly List NzbDroneTypes; - - static ContainerBuilder() - { - NzbDroneTypes = new List(); - NzbDroneTypes.AddRange(Assembly.Load("NzbDrone").GetTypes()); - NzbDroneTypes.AddRange(Assembly.Load("NzbDrone.Common").GetTypes()); - NzbDroneTypes.AddRange(Assembly.Load("NzbDrone.Core").GetTypes()); - NzbDroneTypes.AddRange(Assembly.Load("NzbDrone.Api").GetTypes()); - } - - public static TinyIoCContainer BuildNzbDroneContainer() - { - var container = new TinyIoCContainer(); - - container.AutoRegisterInterfaces(); - - container.AutoRegisterImplementations(); - - container.Register().AsSingleton(); - container.Register().AsSingleton(); - container.Register().AsSingleton(); - container.Register().AsSingleton(); - - container.Register(typeof(IBasicRepository), typeof(BasicRepository)).AsMultiInstance(); - container.Register(typeof(IBasicRepository), typeof(BasicRepository)).AsMultiInstance(); - - container.InitDatabase(); - - ReportingService.RestProvider = container.Resolve(); - - return container; - } - - private static void InitDatabase(this TinyIoCContainer container) - { - Logger.Info("Registering Database..."); - - //TODO: move this to factory - var environmentProvider = new EnvironmentProvider(); - var appDataPath = environmentProvider.GetAppDataPath(); - - if (!Directory.Exists(appDataPath)) - { - Directory.CreateDirectory(appDataPath); - } - - container.Register((c, p) => c.Resolve().Create(environmentProvider.GetNzbDroneDatabase())); - } - - private static void AutoRegisterInterfaces(this TinyIoCContainer container) - { - var interfaces = NzbDroneTypes.Where(t => t.IsInterface); - - foreach (var contract in interfaces) - { - container.AutoRegisterImplementations(contract); - } - } - - - private static void AutoRegisterImplementations(this TinyIoCContainer container) - { - container.AutoRegisterImplementations(typeof(TContract)); - } - - private static void AutoRegisterImplementations(this TinyIoCContainer container, Type contractType) - { - var implementations = GetImplementations(contractType).ToList(); - - if (implementations.Count == 0) - { - return; - } - if (implementations.Count == 1) - { - container.Register(contractType, implementations.Single()).AsMultiInstance(); - } - else - { - container.RegisterMultiple(contractType, implementations).AsMultiInstance(); - } - } - - private static IEnumerable GetImplementations(Type contractType) - { - return NzbDroneTypes - .Where(implementation => - contractType.IsAssignableFrom(implementation) && - !implementation.IsInterface && - !implementation.IsAbstract - ); - } - } -} \ No newline at end of file diff --git a/NzbDrone/MainAppContainerBuilder.cs b/NzbDrone/MainAppContainerBuilder.cs new file mode 100644 index 000000000..4dbff5131 --- /dev/null +++ b/NzbDrone/MainAppContainerBuilder.cs @@ -0,0 +1,61 @@ +using System.IO; +using FluentMigrator.Runner; +using NLog; +using Nancy.Bootstrapper; +using NzbDrone.Api; +using NzbDrone.Common; +using NzbDrone.Common.Eventing; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Datastore.Migration.Framework; +using NzbDrone.Core.ExternalNotification; +using NzbDrone.Core.Organizer; +using NzbDrone.Core.RootFolders; +using TinyIoC; + +namespace NzbDrone +{ + public class MainAppContainerBuilder : ContainerBuilderBase + { + private static readonly Logger Logger = LogManager.GetLogger("ContainerBuilderBase"); + + public static TinyIoCContainer BuildContainer() + { + return new MainAppContainerBuilder().Container; + } + + + private MainAppContainerBuilder() + : base("NzbDrone", "NzbDrone.Common", "NzbDrone.Core", "NzbDrone.Api") + { + AutoRegisterImplementations(); + + Container.Register().AsSingleton(); + Container.Register().AsSingleton(); + Container.Register().AsSingleton(); + Container.Register().AsSingleton(); + + Container.Register(typeof(IBasicRepository), typeof(BasicRepository)).AsMultiInstance(); + Container.Register(typeof(IBasicRepository), typeof(BasicRepository)).AsMultiInstance(); + + InitDatabase(); + + ReportingService.RestProvider = Container.Resolve(); + } + + private void InitDatabase() + { + Logger.Info("Registering Database..."); + + //TODO: move this to factory + var environmentProvider = new EnvironmentProvider(); + var appDataPath = environmentProvider.GetAppDataPath(); + + if (!Directory.Exists(appDataPath)) + { + Directory.CreateDirectory(appDataPath); + } + + Container.Register((c, p) => c.Resolve().Create(environmentProvider.GetNzbDroneDatabase())); + } + } +} \ No newline at end of file diff --git a/NzbDrone/NzbDrone.csproj b/NzbDrone/NzbDrone.csproj index 538ad4313..02173c068 100644 --- a/NzbDrone/NzbDrone.csproj +++ b/NzbDrone/NzbDrone.csproj @@ -118,7 +118,7 @@ Component - +