From 37076fa4eb27fa59d6176e928269234430a738fc Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 23 Jul 2015 18:26:56 +0200 Subject: [PATCH] Completes: U4-6871 CoreBootManager.CreateApplicationContext & CoreBootManager.CreateApplicationCache returns the instance instead of setting the singleton --- src/Umbraco.Core/CoreBootManager.cs | 61 +++++++++++-------- .../BootManagers/CoreBootManagerTests.cs | 10 +-- src/Umbraco.Web/WebBootManager.cs | 4 +- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index 61635a1c73..60aa21e726 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -48,8 +48,8 @@ namespace Umbraco.Core private bool _isComplete = false; private readonly IServiceProvider _serviceProvider = new ActivatorServiceProvider(); private readonly UmbracoApplicationBase _umbracoApplication; - protected ApplicationContext ApplicationContext { get; set; } - protected CacheHelper ApplicationCache { get; set; } + protected ApplicationContext ApplicationContext { get; private set; } + protected CacheHelper ApplicationCache { get; private set; } protected PluginManager PluginManager { get; private set; } protected UmbracoApplicationBase UmbracoApplication @@ -90,7 +90,7 @@ namespace Umbraco.Core string.Format("Umbraco {0} application starting on {1}", UmbracoVersion.GetSemanticVersion().ToSemanticString(), NetworkHelper.MachineName), "Umbraco application startup complete"); - CreateApplicationCache(); + ApplicationCache = CreateApplicationCache(); //create and set the plugin manager (I'd much prefer to not use this singleton anymore but many things are using it unfortunately and // the way that it is setup, there must only ever be one per app so without IoC it would be hard to make this not a singleton) @@ -112,23 +112,17 @@ namespace Umbraco.Core //initialize the DatabaseContext dbContext.Initialize(); - - var serviceContext = new ServiceContext( - new RepositoryFactory(ApplicationCache, ProfilingLogger.Logger, dbContext.SqlSyntax, UmbracoConfig.For.UmbracoSettings()), - new PetaPocoUnitOfWorkProvider(dbFactory), - new FileUnitOfWorkProvider(), - new PublishingStrategy(), - ApplicationCache, - ProfilingLogger.Logger); - CreateApplicationContext(dbContext, serviceContext); + //get the service context + var serviceContext = CreateServiceContext(dbContext, dbFactory); + + //set property and singleton from response + ApplicationContext.Current = ApplicationContext = CreateApplicationContext(dbContext, serviceContext); InitializeApplicationEventsResolver(); InitializeResolvers(); - - InitializeModelMappers(); using (ProfilingLogger.DebugDuration( @@ -157,28 +151,45 @@ namespace Umbraco.Core } /// - /// Creates and assigns the application context singleton + /// Creates and returns the service context for the app /// /// - /// - protected virtual void CreateApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext) + /// + /// + protected virtual ServiceContext CreateServiceContext(DatabaseContext dbContext, IDatabaseFactory dbFactory) { - //create the ApplicationContext - ApplicationContext = ApplicationContext.Current = new ApplicationContext(dbContext, serviceContext, ApplicationCache, ProfilingLogger); + return new ServiceContext( + new RepositoryFactory(ApplicationCache, ProfilingLogger.Logger, dbContext.SqlSyntax, UmbracoConfig.For.UmbracoSettings()), + new PetaPocoUnitOfWorkProvider(dbFactory), + new FileUnitOfWorkProvider(), + new PublishingStrategy(), + ApplicationCache, + ProfilingLogger.Logger); } /// - /// Creates and assigns the ApplicationCache based on a new instance of System.Web.Caching.Cache + /// Creates and returns the application context for the app /// - protected virtual void CreateApplicationCache() + /// + /// + protected virtual ApplicationContext CreateApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext) + { + //create the ApplicationContext + return new ApplicationContext(dbContext, serviceContext, ApplicationCache, ProfilingLogger); + } + + /// + /// Creates and returns the CacheHelper for the app + /// + protected virtual CacheHelper CreateApplicationCache() { var cacheHelper = new CacheHelper( - new ObjectCacheRuntimeCacheProvider(), - new StaticCacheProvider(), + new ObjectCacheRuntimeCacheProvider(), + new StaticCacheProvider(), //we have no request based cache when not running in web-based context - new NullCacheProvider()); + new NullCacheProvider()); - ApplicationCache = cacheHelper; + return cacheHelper; } /// diff --git a/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs b/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs index d2e5a9db12..7e8aa19880 100644 --- a/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs +++ b/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs @@ -63,17 +63,19 @@ namespace Umbraco.Tests.BootManagers } /// - /// Creates and assigns the application context singleton + /// Creates and returns the application context singleton /// /// /// - protected override void CreateApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext) + protected override ApplicationContext CreateApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext) { - base.CreateApplicationContext(dbContext, serviceContext); + var appContext = base.CreateApplicationContext(dbContext, serviceContext); var dbContextMock = new Mock(Mock.Of(), ProfilingLogger.Logger, Mock.Of(), "test"); dbContextMock.Setup(x => x.CanConnect).Returns(true); - ApplicationContext.DatabaseContext = dbContextMock.Object; + appContext.DatabaseContext = dbContextMock.Object; + + return appContext; } protected override void InitializeApplicationEventsResolver() diff --git a/src/Umbraco.Web/WebBootManager.cs b/src/Umbraco.Web/WebBootManager.cs index fa2a0e2fc7..628eba26ac 100644 --- a/src/Umbraco.Web/WebBootManager.cs +++ b/src/Umbraco.Web/WebBootManager.cs @@ -218,10 +218,10 @@ namespace Umbraco.Web /// /// Creates the application cache based on the HttpRuntime cache /// - protected override void CreateApplicationCache() + protected override CacheHelper CreateApplicationCache() { //create a web-based cache helper - ApplicationCache = new CacheHelper(); + return new CacheHelper(); } ///