From f2886d89de2a7e7d4d5a6af863af91b86d72d9dc Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 30 Mar 2013 15:14:33 -0700 Subject: [PATCH] Autofac registrations are not singleton anymore. --- NzbDrone.Common/CommonContainerExtentions.cs | 6 ++-- NzbDrone.Core/ContainerExtensions.cs | 8 ++--- NzbDrone.Core/Datastore/DbFactory.cs | 7 ++-- .../Framework/MigrationController.cs | 35 ++++++++++++------- NzbDrone.Update/Program.cs | 1 - 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/NzbDrone.Common/CommonContainerExtentions.cs b/NzbDrone.Common/CommonContainerExtentions.cs index 10e2cf192..f75bb70f9 100644 --- a/NzbDrone.Common/CommonContainerExtentions.cs +++ b/NzbDrone.Common/CommonContainerExtentions.cs @@ -22,12 +22,10 @@ public static void RegisterAssemblyTypes(this ContainerBuilder containerBuilder, } containerBuilder.RegisterAssemblyTypes(apiAssembly) - .AsImplementedInterfaces() - .SingleInstance(); + .AsImplementedInterfaces(); containerBuilder.RegisterAssemblyTypes(apiAssembly) - .AsSelf() - .SingleInstance(); + .AsSelf(); } } } \ No newline at end of file diff --git a/NzbDrone.Core/ContainerExtensions.cs b/NzbDrone.Core/ContainerExtensions.cs index 45d8e4897..6e154e998 100644 --- a/NzbDrone.Core/ContainerExtensions.cs +++ b/NzbDrone.Core/ContainerExtensions.cs @@ -37,15 +37,15 @@ private static void RegisterAssembly(this ContainerBuilder container, string ass container.RegisterAssemblyTypes(assembly) .Where(t => t.IsSubclassOf(typeof(IndexerBase))) - .As().SingleInstance(); + .As(); container.RegisterAssemblyTypes(assembly) .Where(t => t.IsSubclassOf(typeof(IndexerSearchBase))) - .As().SingleInstance(); + .As(); container.RegisterAssemblyTypes(assembly) .Where(t => t.IsSubclassOf(typeof(ExternalNotificationBase))) - .As().SingleInstance(); + .As(); } private static void InitDatabase(this ContainerBuilder container) @@ -56,7 +56,7 @@ private static void InitDatabase(this ContainerBuilder container) var appDataPath = environmentProvider.GetAppDataPath(); if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath); - container.Register(c => c.Resolve().Create(environmentProvider.GetNzbDroneDatabase())).As().SingleInstance(); + container.Register(c => c.Resolve().Create(environmentProvider.GetNzbDroneDatabase())).As(); container.RegisterGeneric(typeof(BasicRepository<>)).As(typeof(IBasicRepository<>)); } diff --git a/NzbDrone.Core/Datastore/DbFactory.cs b/NzbDrone.Core/Datastore/DbFactory.cs index 256a4ad1a..aeff131a1 100644 --- a/NzbDrone.Core/Datastore/DbFactory.cs +++ b/NzbDrone.Core/Datastore/DbFactory.cs @@ -1,5 +1,4 @@ using System; -using System.Data; using System.Data.SQLite; using Marr.Data; using NzbDrone.Core.Datastore.Migration.Framework; @@ -16,9 +15,13 @@ public class DbFactory : IDbFactory { private readonly IMigrationController _migrationController; - public DbFactory(IMigrationController migrationController) + static DbFactory() { TableMapping.Map(); + } + + public DbFactory(IMigrationController migrationController) + { _migrationController = migrationController; } diff --git a/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs b/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs index 83b3c7b5d..18cea944f 100644 --- a/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs +++ b/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs @@ -1,4 +1,6 @@ -using System.Reflection; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Reflection; using FluentMigrator.Runner; using FluentMigrator.Runner.Initialization; using FluentMigrator.Runner.Processors.Sqlite; @@ -14,6 +16,8 @@ public class MigrationController : IMigrationController { private readonly IAnnouncer _announcer; + private static readonly HashSet MigrationCache = new HashSet(); + public MigrationController(IAnnouncer announcer) { _announcer = announcer; @@ -21,19 +25,26 @@ public MigrationController(IAnnouncer announcer) public void MigrateToLatest(string connectionString, MigrationType migrationType) { - var assembly = Assembly.GetExecutingAssembly(); - - var migrationContext = new RunnerContext(_announcer) + lock (MigrationCache) { - Namespace = "NzbDrone.Core.Datastore.Migration", - ApplicationContext = migrationType - }; + if (MigrationCache.Contains(connectionString.ToLower())) return; - var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 }; - var factory = new SqliteProcessorFactory(); - var processor = factory.Create(connectionString, _announcer, options); - var runner = new MigrationRunner(assembly, migrationContext, processor); - runner.MigrateUp(true); + var assembly = Assembly.GetExecutingAssembly(); + + var migrationContext = new RunnerContext(_announcer) + { + Namespace = "NzbDrone.Core.Datastore.Migration", + ApplicationContext = migrationType + }; + + var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 }; + var factory = new SqliteProcessorFactory(); + var processor = factory.Create(connectionString, _announcer, options); + var runner = new MigrationRunner(assembly, migrationContext, processor); + runner.MigrateUp(true); + + MigrationCache.Add(connectionString.ToLower()); + } } } } diff --git a/NzbDrone.Update/Program.cs b/NzbDrone.Update/Program.cs index 08d6ce2c8..8411a0249 100644 --- a/NzbDrone.Update/Program.cs +++ b/NzbDrone.Update/Program.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Linq; using Autofac; using NLog; using NzbDrone.Common;