From bda2dbfb8849775574651d3e54a25f00480b55e2 Mon Sep 17 00:00:00 2001 From: Stephan Date: Thu, 28 Jul 2016 19:29:30 +0200 Subject: [PATCH] Resvolution - Refactor --- src/Umbraco.Core/CoreBootManager.cs | 22 ++++----- src/Umbraco.Core/Current.cs | 17 ------- .../DependencyInjection/Current.cs | 27 +++++++++++ src/Umbraco.Core/Umbraco.Core.csproj | 2 +- src/Umbraco.Web/Current.cs | 31 +++++++----- .../Editors/ContentControllerBase.cs | 1 - .../Mvc/UmbracoViewPageOfTModel.cs | 13 +++-- .../Security/Identity/AppBuilderExtensions.cs | 5 -- .../UmbracoBackOfficeCookieAuthOptions.cs | 2 - src/Umbraco.Web/UmbracoContext.cs | 1 + src/Umbraco.Web/WebBootManager.cs | 3 -- .../umbraco.presentation/library.cs | 15 ++---- src/UmbracoExamine/UmbracoContentIndexer.cs | 48 ++++++++----------- 13 files changed, 91 insertions(+), 96 deletions(-) delete mode 100644 src/Umbraco.Core/Current.cs create mode 100644 src/Umbraco.Core/DependencyInjection/Current.cs diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index d31b7edeee..b7630013ed 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -115,7 +115,7 @@ namespace Umbraco.Core // completed at the end of the boot process to allow garbage collection _appStartupEvtContainer = Container.Clone(); _appStartupEvtContainer.BeginScope(); - _appStartupEvtContainer.RegisterBuilderCollection(PluginManager.ResolveApplicationStartupHandlers()); + _appStartupEvtContainer.RegisterBuilderCollection(PluginManager.ResolveApplicationStartupHandlers()); //build up standard IoC services ConfigureApplicationServices(Container); @@ -155,7 +155,7 @@ namespace Umbraco.Core internal virtual void ConfigureCoreServices(ServiceContainer container) { // configure the temp. Current - Current.Container = container; + Current.CurrentContainer = container; container.Register(factory => container); @@ -204,14 +204,14 @@ namespace Umbraco.Core protected virtual CacheHelper CreateApplicationCache() { var cacheHelper = new CacheHelper( - //we need to have the dep clone runtime cache provider to ensure + //we need to have the dep clone runtime cache provider to ensure //all entities are cached properly (cloned in and cloned out) new DeepCloneRuntimeCacheProvider(new ObjectCacheRuntimeCacheProvider()), new StaticCacheProvider(), //we have no request based cache when not running in web-based context new NullCacheProvider(), new IsolatedRuntimeCache(type => - //we need to have the dep clone runtime cache provider to ensure + //we need to have the dep clone runtime cache provider to ensure //all entities are cached properly (cloned in and cloned out) new DeepCloneRuntimeCacheProvider(new ObjectCacheRuntimeCacheProvider()))); @@ -220,7 +220,7 @@ namespace Umbraco.Core /// /// This method initializes all of the model mappers registered in the container - /// + /// protected void InitializeModelMappers() { Mapper.Initialize(configuration => @@ -323,7 +323,7 @@ namespace Umbraco.Core { using (ProfilingLogger.DebugDuration( $"Executing {x.GetType()} in ApplicationStarted", - $"Executed {x.GetType()} in ApplicationStarted", + $"Executed {x.GetType()} in ApplicationStarted", //only log if more than 150ms 150)) { @@ -340,8 +340,8 @@ namespace Umbraco.Core //end the current scope which was created to intantiate all of the startup handlers, //this will dispose them if they're IDisposable _appStartupEvtContainer.EndCurrentScope(); - //NOTE: DO NOT Dispose this cloned container since it will also dispose of any instances - // resolved from the parent container + //NOTE: DO NOT Dispose this cloned container since it will also dispose of any instances + // resolved from the parent container _appStartupEvtContainer = null; if (afterComplete != null) @@ -457,11 +457,11 @@ namespace Umbraco.Core CacheRefreshersResolver.Current = new CacheRefreshersResolver( Container, ProfilingLogger.Logger, () => PluginManager.ResolveCacheRefreshers()); - + PackageActionsResolver.Current = new PackageActionsResolver( ServiceProvider, ProfilingLogger.Logger, () => PluginManager.ResolvePackageActions()); - + //the database migration objects MigrationResolver.Current = new MigrationResolver( Container, ProfilingLogger.Logger, @@ -483,6 +483,6 @@ namespace Umbraco.Core // by default, no factory is activated PublishedContentModelFactoryResolver.Current = new PublishedContentModelFactoryResolver(Container); } - + } } diff --git a/src/Umbraco.Core/Current.cs b/src/Umbraco.Core/Current.cs deleted file mode 100644 index 2d43610b01..0000000000 --- a/src/Umbraco.Core/Current.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections.Generic; -using LightInject; -using Umbraco.Core.Strings; - -namespace Umbraco.Core -{ - // must remain internal - this class is here to support the transition from singletons - // and resolvers to injection - by providing a static access to singleton services - it - // is initialized once with a service container, in CoreBootManager. - internal class Current - { - public static IServiceContainer Container { get; set; } // ok to set - don't be stupid - - // cannot set - this is temp - public static IEnumerable UrlSegmentProviders => Container.GetInstance(); - } -} diff --git a/src/Umbraco.Core/DependencyInjection/Current.cs b/src/Umbraco.Core/DependencyInjection/Current.cs new file mode 100644 index 0000000000..95f67dd45b --- /dev/null +++ b/src/Umbraco.Core/DependencyInjection/Current.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using LightInject; +using Umbraco.Core.Strings; + +namespace Umbraco.Core.DependencyInjection +{ + // this class is here to support the transition from singletons and resolvers to injection, + // by providing a static access to singleton services - it is initialized once with a service + // container, in CoreBootManager. + public static class Current + { + internal static IServiceContainer CurrentContainer { get; set; } // ok to set - don't be stupid + + public static IServiceContainer Container + { + get + { + if (CurrentContainer == null) throw new Exception("oops:container"); + return CurrentContainer; + } + } + + public static IEnumerable UrlSegmentProviders + => Container.GetInstance(); + } +} diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 194402c805..1c8ebcd5f2 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -207,7 +207,7 @@ - + diff --git a/src/Umbraco.Web/Current.cs b/src/Umbraco.Web/Current.cs index c655f9fb57..8a5e38f24a 100644 --- a/src/Umbraco.Web/Current.cs +++ b/src/Umbraco.Web/Current.cs @@ -1,21 +1,19 @@ using System; using System.Collections.Generic; -using LightInject; using Umbraco.Core.Events; using Umbraco.Core.Strings; using Umbraco.Web.PublishedCache; +using CoreCurrent = Umbraco.Core.DependencyInjection.Current; namespace Umbraco.Web { // must remain internal - this class is here to support the transition from singletons // and resolvers to injection - by providing a static access to singleton services - it // is initialized once with a service container, in WebBootManager. - internal static class Current + public static class Current { private static readonly object Locker = new object(); - public static IServiceContainer Container { get; set; } // ok to set - don't be stupid - private static IUmbracoContextAccessor _umbracoContextAccessor; private static IFacadeAccessor _facadeAccessor; @@ -28,8 +26,7 @@ namespace Umbraco.Web get { if (_umbracoContextAccessor != null) return _umbracoContextAccessor; - if (Container == null) throw new Exception("oops:container"); - return (_umbracoContextAccessor = Container.GetInstance()); + return (_umbracoContextAccessor = CoreCurrent.Container.GetInstance()); } set { _umbracoContextAccessor = value; } // for tests } @@ -39,13 +36,13 @@ namespace Umbraco.Web get { if (_facadeAccessor != null) return _facadeAccessor; - if (Container == null) throw new Exception("oops:container"); - return (_facadeAccessor = Container.GetInstance()); + return (_facadeAccessor = CoreCurrent.Container.GetInstance()); } set { _facadeAccessor = value; } // for tests } - public static UmbracoContext UmbracoContext => UmbracoContextAccessor.UmbracoContext; + public static UmbracoContext UmbracoContext + => UmbracoContextAccessor.UmbracoContext; // have to support set for now, because of 'ensure umbraco context' which can create // contexts pretty much at any time and in an uncontrolled way - and when we do not have @@ -72,9 +69,21 @@ namespace Umbraco.Web } // cannot set - it's set by whatever creates the facade, which should have the accessor injected - public static IFacade Facade => FacadeAccessor.Facade; + public static IFacade Facade + => FacadeAccessor.Facade; // cannot set - this is temp - public static EventMessages EventMessages => Container.GetInstance().GetOrDefault(); + public static EventMessages EventMessages + => CoreCurrent.Container.GetInstance().GetOrDefault(); + + #region Core + + // just repeating Core for convenience + + public static IEnumerable UrlSegmentProviders + => CoreCurrent.Container.GetInstance(); + + + #endregion } } diff --git a/src/Umbraco.Web/Editors/ContentControllerBase.cs b/src/Umbraco.Web/Editors/ContentControllerBase.cs index 14e020b80c..398136fb06 100644 --- a/src/Umbraco.Web/Editors/ContentControllerBase.cs +++ b/src/Umbraco.Web/Editors/ContentControllerBase.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; -using System.Web.Mvc; using Umbraco.Core; using Umbraco.Core.Logging; using Umbraco.Core.Models; diff --git a/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs b/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs index eb564c3cd3..6ff1bf9ff8 100644 --- a/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs +++ b/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs @@ -1,5 +1,4 @@ using System; -using System.Globalization; using System.Text; using System.Web; using System.Web.Mvc; @@ -26,11 +25,11 @@ namespace Umbraco.Web.Mvc /// /// Gets the current . /// - // always try to return the context from the data tokens just in case its a custom context and not + // always try to return the context from the data tokens just in case its a custom context and not // using the Current.UmbracoContext. Fallback to that singleton if necessary, the only reason this // should ever happen is is someone is rendering a page that inherits from this class and are rendering // it outside of the normal Umbraco routing process. Very unlikely. - public UmbracoContext UmbracoContext => _umbracoContext ?? + public UmbracoContext UmbracoContext => _umbracoContext ?? (_umbracoContext = ViewContext.GetUmbracoContext() ?? Current.UmbracoContext); /// @@ -47,7 +46,7 @@ namespace Umbraco.Web.Mvc { const string token = Core.Constants.Web.PublishedDocumentRequestDataToken; - // we should always try to return the object from the data tokens just in case its a custom object and not + // we should always try to return the object from the data tokens just in case its a custom object and not // the one from UmbracoContext. Fallback to UmbracoContext if necessary. // try view context @@ -101,8 +100,8 @@ namespace Umbraco.Web.Mvc if (ViewContext.IsChildAction) return; - // this is used purely for partial view macros that contain forms and mostly - // just when rendered within the RTE - this should already be set with the + // this is used purely for partial view macros that contain forms and mostly + // just when rendered within the RTE - this should already be set with the // EnsurePartialViewMacroViewContextFilterAttribute if (ViewContext.RouteData.DataTokens.ContainsKey(Constants.DataTokenCurrentViewContext) == false) ViewContext.RouteData.DataTokens.Add(Constants.DataTokenCurrentViewContext, ViewContext); @@ -221,7 +220,7 @@ namespace Umbraco.Web.Mvc { return WebViewPageExtensions.RenderSection(this, name, defaultContents); } - + public HelperResult RenderSection(string name, IHtmlString defaultContents) { return WebViewPageExtensions.RenderSection(this, name, defaultContents); diff --git a/src/Umbraco.Web/Security/Identity/AppBuilderExtensions.cs b/src/Umbraco.Web/Security/Identity/AppBuilderExtensions.cs index be1135f16f..f1dab2ee63 100644 --- a/src/Umbraco.Web/Security/Identity/AppBuilderExtensions.cs +++ b/src/Umbraco.Web/Security/Identity/AppBuilderExtensions.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; using System.Threading; -using System.Web; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; using Microsoft.Owin; @@ -14,7 +10,6 @@ using Microsoft.Owin.Security.Cookies; using Owin; using Umbraco.Core; using Umbraco.Core.Configuration; -using Umbraco.Core.IO; using Umbraco.Core.Logging; using Umbraco.Core.Models.Identity; using Umbraco.Core.Security; diff --git a/src/Umbraco.Web/Security/Identity/UmbracoBackOfficeCookieAuthOptions.cs b/src/Umbraco.Web/Security/Identity/UmbracoBackOfficeCookieAuthOptions.cs index a18c37a453..f528bc09f1 100644 --- a/src/Umbraco.Web/Security/Identity/UmbracoBackOfficeCookieAuthOptions.cs +++ b/src/Umbraco.Web/Security/Identity/UmbracoBackOfficeCookieAuthOptions.cs @@ -1,6 +1,4 @@ using System; -using System.Security.Claims; -using System.Threading.Tasks; using Microsoft.Owin; using Microsoft.Owin.Security; using Microsoft.Owin.Security.Cookies; diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index 124410ad26..8f224edd8b 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -369,6 +369,7 @@ namespace Umbraco.Web Security.DisposeIfDisposable(); // reset - important when running outside of http context + // also takes care of the accessor Web.Current.SetUmbracoContext(null, true); // help caches release resources diff --git a/src/Umbraco.Web/WebBootManager.cs b/src/Umbraco.Web/WebBootManager.cs index 0d56ca0e77..331a20e873 100644 --- a/src/Umbraco.Web/WebBootManager.cs +++ b/src/Umbraco.Web/WebBootManager.cs @@ -322,9 +322,6 @@ namespace Umbraco.Web { base.ConfigureCoreServices(container); - // configure the temp. Current - Current.Container = container; - // register model mappers container.RegisterFrom(); diff --git a/src/Umbraco.Web/umbraco.presentation/library.cs b/src/Umbraco.Web/umbraco.presentation/library.cs index 058c05b123..13ad3c2b8d 100644 --- a/src/Umbraco.Web/umbraco.presentation/library.cs +++ b/src/Umbraco.Web/umbraco.presentation/library.cs @@ -15,24 +15,17 @@ using Newtonsoft.Json; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Configuration; -using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Services; -using Umbraco.Core.Strings; using Umbraco.Web; -using Umbraco.Web.Cache; using Umbraco.Web.Templates; -using umbraco.BusinessLogic; -using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.web; -using umbraco.DataLayer; using Umbraco.Core.IO; using Umbraco.Core.Xml; using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.XmlPublishedCache; using Language = umbraco.cms.businesslogic.language.Language; -using Media = umbraco.cms.businesslogic.media.Media; using Member = umbraco.cms.businesslogic.member.Member; using PropertyType = umbraco.cms.businesslogic.propertytype.PropertyType; @@ -363,7 +356,7 @@ namespace umbraco ApplicationContext.Current.Services.MediaService, ApplicationContext.Current.Services.DataTypeService, ApplicationContext.Current.Services.UserService, - Umbraco.Core.Current.UrlSegmentProviders, + Current.UrlSegmentProviders, media, deep); return Tuple.Create(serialized, media.Path); @@ -1263,7 +1256,7 @@ namespace umbraco } else { - var facade = Umbraco.Web.Current.Facade + var facade = Current.Facade ?? FacadeServiceResolver.Current.Service.CreateFacade(null); contentCache = facade.ContentCache as PublishedContentCache; } @@ -1678,7 +1671,7 @@ namespace umbraco ApplicationContext.Current.Services.ContentService, ApplicationContext.Current.Services.DataTypeService, ApplicationContext.Current.Services.UserService, - Umbraco.Core.Current.UrlSegmentProviders, parent).GetXmlNode(xd); + Current.UrlSegmentProviders, parent).GetXmlNode(xd); n.AppendChild(x); } } @@ -1691,7 +1684,7 @@ namespace umbraco ApplicationContext.Current.Services.ContentService, ApplicationContext.Current.Services.DataTypeService, ApplicationContext.Current.Services.UserService, - Umbraco.Core.Current.UrlSegmentProviders, child).GetXmlNode(xd); + Current.UrlSegmentProviders, child).GetXmlNode(xd); n.AppendChild(x); } } diff --git a/src/UmbracoExamine/UmbracoContentIndexer.cs b/src/UmbracoExamine/UmbracoContentIndexer.cs index ff2c72d372..8784733600 100644 --- a/src/UmbracoExamine/UmbracoContentIndexer.cs +++ b/src/UmbracoExamine/UmbracoContentIndexer.cs @@ -1,28 +1,22 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; -using System.Globalization; using System.Linq; -using System.Xml; -using System.Xml.Linq; using Examine; -using Lucene.Net.Documents; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Services; using Umbraco.Core.Strings; -using Examine.LuceneEngine; using Examine.LuceneEngine.Config; using Examine.LuceneEngine.Faceting; using Examine.LuceneEngine.Indexing; using Examine.LuceneEngine.Providers; using Lucene.Net.Analysis; using Lucene.Net.Store; -using Umbraco.Core.Xml; +using Umbraco.Core.DependencyInjection; using Umbraco.Core.Logging; using Umbraco.Core.Persistence.DatabaseModelDefinitions; using Umbraco.Core.Persistence.Querying; -using Umbraco.Core.Persistence.SqlSyntax; using IContentService = Umbraco.Core.Services.IContentService; using IMediaService = Umbraco.Core.Services.IMediaService; @@ -57,19 +51,19 @@ namespace UmbracoExamine } public UmbracoContentIndexer( - IEnumerable fieldDefinitions, - Directory luceneDirectory, + IEnumerable fieldDefinitions, + Directory luceneDirectory, Analyzer defaultAnalyzer, ProfilingLogger profilingLogger, - IContentService contentService, - IMediaService mediaService, - IUserService userService, - IEnumerable urlSegmentProviders, + IContentService contentService, + IMediaService mediaService, + IUserService userService, + IEnumerable urlSegmentProviders, IValueSetValidator validator, UmbracoContentIndexerOptions options, IQueryFactory queryFactory, - FacetConfiguration facetConfiguration = null, - IDictionary> indexValueTypes = null) + FacetConfiguration facetConfiguration = null, + IDictionary> indexValueTypes = null) : base(fieldDefinitions, luceneDirectory, defaultAnalyzer, profilingLogger, validator, facetConfiguration, indexValueTypes) { if (contentService == null) throw new ArgumentNullException("contentService"); @@ -84,7 +78,7 @@ namespace UmbracoExamine SupportUnpublishedContent = options.SupportUnpublishedContent; ParentId = options.ParentId; //backward compat hack: - IndexerData = new IndexCriteria(Enumerable.Empty(), Enumerable.Empty(), Enumerable.Empty(), Enumerable.Empty(), + IndexerData = new IndexCriteria(Enumerable.Empty(), Enumerable.Empty(), Enumerable.Empty(), Enumerable.Empty(), //hack to set the parent Id for backwards compat, when using this ctor the IndexerData will (should) always be null options.ParentId); @@ -94,10 +88,10 @@ namespace UmbracoExamine _urlSegmentProviders = urlSegmentProviders; _queryFactory = queryFactory; } - + #endregion - + #region Initialize /// @@ -117,7 +111,7 @@ namespace UmbracoExamine /// /// An attempt is made to call on a provider after the provider has already been initialized. /// - + public override void Initialize(string name, NameValueCollection config) { @@ -174,16 +168,16 @@ namespace UmbracoExamine #endregion - + #region Public methods - + /// - /// Deletes a node from the index. + /// Deletes a node from the index. /// /// - /// When a content node is deleted, we also need to delete it's children from the index so we need to perform a + /// When a content node is deleted, we also need to delete it's children from the index so we need to perform a /// custom Lucene search to find all decendents and create Delete item queues for them too. /// /// ID of the node to delete @@ -266,7 +260,7 @@ namespace UmbracoExamine } while (content.Length == pageSize); - + break; case IndexTypes.Media: @@ -276,7 +270,7 @@ namespace UmbracoExamine mediaParentId = ParentId.Value; } IMedia[] media; - + do { long total; @@ -294,7 +288,7 @@ namespace UmbracoExamine } IndexItems(GetValueSets(media)); - + pageIndex++; } while (media.Length == pageSize); @@ -324,7 +318,7 @@ namespace UmbracoExamine {"path", new object[] {c.Path}}, {"nodeType", new object[] {c.ContentType.Id}}, {"creatorName", new object[] {c.GetCreatorProfile(UserService).Name}}, - {"writerName", new object[] {c.GetWriterProfile(UserService).Name}}, + {"writerName", new object[] {c.GetWriterProfile(UserService).Name}}, {"writerID", new object[] {c.WriterId}}, {"version", new object[] {c.Version}}, {"template", new object[] {c.Template == null ? 0 : c.Template.Id}}