diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index f440626df1..50a9fbf847 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -162,16 +162,16 @@ namespace Umbraco.Core container.Register(factory => container); //Logging - container.Register(factory => _umbracoApplication.Logger, new PerContainerLifetime()); - container.Register(factory => ProfilingLogger.Profiler, new PerContainerLifetime()); - container.Register(factory => ProfilingLogger, new PerContainerLifetime()); + container.RegisterSingleton(factory => _umbracoApplication.Logger); + container.RegisterSingleton(factory => ProfilingLogger.Profiler); + container.RegisterSingleton(factory => ProfilingLogger); //Config container.RegisterFrom(); //Cache - container.Register(factory => ApplicationCache, new PerContainerLifetime()); - container.Register(factory => ApplicationCache.RuntimeCache, new PerContainerLifetime()); + container.RegisterSingleton(factory => ApplicationCache); + container.RegisterSingleton(factory => ApplicationCache.RuntimeCache); //Datalayer/Repositories/SQL/Database/etc... container.RegisterFrom(); @@ -179,10 +179,10 @@ namespace Umbraco.Core //Data Services/ServiceContext/etc... container.RegisterFrom(); - container.Register(); - container.Register(factory => PluginManager, new PerContainerLifetime()); + container.RegisterSingleton(); + container.RegisterSingleton(factory => PluginManager); - container.Register(new PerContainerLifetime()); + container.RegisterSingleton(); container.Register(factory => FileSystemProviderManager.Current.GetFileSystemProvider()); } diff --git a/src/Umbraco.Core/DependencyInjection/LightInjectExtensions.cs b/src/Umbraco.Core/DependencyInjection/LightInjectExtensions.cs index f80d5fdaf4..78679613f9 100644 --- a/src/Umbraco.Core/DependencyInjection/LightInjectExtensions.cs +++ b/src/Umbraco.Core/DependencyInjection/LightInjectExtensions.cs @@ -6,6 +6,47 @@ namespace Umbraco.Core.DependencyInjection { internal static class LightInjectExtensions { + /// + /// Extension method to register a singleton (syntax sugar) + /// + public static void RegisterSingleton(this IServiceRegistry container, Func factory, string serviceName) + { + container.Register(factory, new PerContainerLifetime()); + } + + /// + /// Extension method to register a singleton (syntax sugar) + /// + /// + /// + /// + public static void RegisterSingleton(this IServiceRegistry container) + where TImplementation : TContract + { + container.Register(new PerContainerLifetime()); + } + + /// + /// Extension method to register a singleton (syntax sugar) + /// + /// + /// + public static void RegisterSingleton(this IServiceRegistry container) + { + container.Register(new PerContainerLifetime()); + } + + /// + /// Extension method to register a singleton (syntax sugar) + /// + /// + /// + /// + public static void RegisterSingleton(this IServiceRegistry container, Func factory) + { + container.Register(factory); + } + /// /// In order for LightInject to deal with enumerables of the same type, each one needs to be named individually /// diff --git a/src/Umbraco.Core/DependencyInjection/RepositoryCompositionRoot.cs b/src/Umbraco.Core/DependencyInjection/RepositoryCompositionRoot.cs index 1ff50f8392..047728be5f 100644 --- a/src/Umbraco.Core/DependencyInjection/RepositoryCompositionRoot.cs +++ b/src/Umbraco.Core/DependencyInjection/RepositoryCompositionRoot.cs @@ -21,25 +21,24 @@ namespace Umbraco.Core.DependencyInjection { public void Compose(IServiceRegistry container) { - container.Register(factory => new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName, factory.GetInstance()), new PerContainerLifetime()); - container.Register(factory => GetDbContext(factory), new PerContainerLifetime()); - container.Register(factory => SqlSyntaxProviders.CreateDefault(factory.GetInstance()), new PerContainerLifetime()); - container.Register(new PerContainerLifetime()); - container.Register(factory => new PetaPocoUnitOfWorkProvider(factory.GetInstance()), new PerContainerLifetime()); - container.Register(factory => new MappingResolver( + container.RegisterSingleton(factory => new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName, factory.GetInstance())); + container.RegisterSingleton(factory => GetDbContext(factory)); + container.RegisterSingleton(factory => SqlSyntaxProviders.CreateDefault(factory.GetInstance())); + container.RegisterSingleton(); + container.RegisterSingleton(factory => new PetaPocoUnitOfWorkProvider(factory.GetInstance())); + container.RegisterSingleton(factory => new MappingResolver( factory.GetInstance(), factory.GetInstance(), - () => factory.GetInstance().ResolveAssignedMapperTypes()), - new PerContainerLifetime()); + () => factory.GetInstance().ResolveAssignedMapperTypes())); container.Register(); container.Register(factory => factory.GetInstance().SqlSyntax); - container.Register(factory => CacheHelper.CreateDisabledCacheHelper(), "DisabledCache", new PerContainerLifetime()); - container.Register(factory => new PhysicalFileSystem(SystemDirectories.Scripts), "ScriptFileSystem", new PerContainerLifetime()); - container.Register(factory => new PhysicalFileSystem(SystemDirectories.MvcViews + "/Partials/"), "PartialViewFileSystem", new PerContainerLifetime()); - container.Register(factory => new PhysicalFileSystem(SystemDirectories.MvcViews + "/MacroPartials/"), "PartialViewMacroFileSystem", new PerContainerLifetime()); - container.Register(factory => new PhysicalFileSystem(SystemDirectories.Css), "StylesheetFileSystem", new PerContainerLifetime()); - container.Register(factory => new PhysicalFileSystem(SystemDirectories.Masterpages), "MasterpageFileSystem", new PerContainerLifetime()); - container.Register(factory => new PhysicalFileSystem(SystemDirectories.MvcViews), "ViewFileSystem", new PerContainerLifetime()); + container.RegisterSingleton(factory => CacheHelper.CreateDisabledCacheHelper(), "DisabledCache"); + container.RegisterSingleton(factory => new PhysicalFileSystem(SystemDirectories.Scripts), "ScriptFileSystem"); + container.RegisterSingleton(factory => new PhysicalFileSystem(SystemDirectories.MvcViews + "/Partials/"), "PartialViewFileSystem"); + container.RegisterSingleton(factory => new PhysicalFileSystem(SystemDirectories.MvcViews + "/MacroPartials/"), "PartialViewMacroFileSystem"); + container.RegisterSingleton(factory => new PhysicalFileSystem(SystemDirectories.Css), "StylesheetFileSystem"); + container.RegisterSingleton(factory => new PhysicalFileSystem(SystemDirectories.Masterpages), "MasterpageFileSystem"); + container.RegisterSingleton(factory => new PhysicalFileSystem(SystemDirectories.MvcViews), "ViewFileSystem"); //Repository factories: //NOTE: Wondering if we can pass in parameters at resolution time with LightInject diff --git a/src/Umbraco.Core/DependencyInjection/ServicesCompositionRoot.cs b/src/Umbraco.Core/DependencyInjection/ServicesCompositionRoot.cs index 08ff442a0f..c0131541e7 100644 --- a/src/Umbraco.Core/DependencyInjection/ServicesCompositionRoot.cs +++ b/src/Umbraco.Core/DependencyInjection/ServicesCompositionRoot.cs @@ -14,22 +14,42 @@ namespace Umbraco.Core.DependencyInjection { public void Compose(IServiceRegistry container) { - container.Register(new PerContainerLifetime()); + container.RegisterSingleton(); //These will be replaced by the web boot manager when running in a web context container.Register(); - - container.Register(factory => new ServiceContext( - factory.GetInstance(), - factory.GetInstance(), - factory.GetInstance(), - factory.GetInstance(), - factory.GetInstance(), - factory.GetInstance(), - factory.GetInstance(), - factory.GetInstance>()), - new PerContainerLifetime()); - + + //the context + container.RegisterSingleton(); + + //now the services... + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + container.RegisterSingleton(); + //TODO: These are replaced in the web project - we need to declare them so that + // something is wired up, just not sure this is very nice but will work for now. + container.RegisterSingleton(); + container.RegisterSingleton(); } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/ServiceContext.cs b/src/Umbraco.Core/Services/ServiceContext.cs index c28c4e19e5..139e232abe 100644 --- a/src/Umbraco.Core/Services/ServiceContext.cs +++ b/src/Umbraco.Core/Services/ServiceContext.cs @@ -48,6 +48,39 @@ namespace Umbraco.Core.Services private Lazy _notificationService; private Lazy _externalLoginService; + /// + /// Constructor used for IoC + /// + public ServiceContext(Lazy migrationEntryService, Lazy publicAccessService, Lazy taskService, Lazy domainService, Lazy auditService, Lazy localizedTextService, Lazy tagService, Lazy contentService, Lazy userService, Lazy memberService, Lazy mediaService, Lazy contentTypeService, Lazy dataTypeService, Lazy fileService, Lazy localizationService, Lazy packagingService, Lazy serverRegistrationService, Lazy entityService, Lazy relationService, Lazy treeService, Lazy sectionService, Lazy macroService, Lazy memberTypeService, Lazy memberGroupService, Lazy notificationService, Lazy externalLoginService) + { + _migrationEntryService = migrationEntryService; + _publicAccessService = publicAccessService; + _taskService = taskService; + _domainService = domainService; + _auditService = auditService; + _localizedTextService = localizedTextService; + _tagService = tagService; + _contentService = contentService; + _userService = userService; + _memberService = memberService; + _mediaService = mediaService; + _contentTypeService = contentTypeService; + _dataTypeService = dataTypeService; + _fileService = fileService; + _localizationService = localizationService; + _packagingService = packagingService; + _serverRegistrationService = serverRegistrationService; + _entityService = entityService; + _relationService = relationService; + _treeService = treeService; + _sectionService = sectionService; + _macroService = macroService; + _memberTypeService = memberTypeService; + _memberGroupService = memberGroupService; + _notificationService = notificationService; + _externalLoginService = externalLoginService; + } + /// /// public ctor - will generally just be used for unit testing all items are optional and if not specified, the defaults will be used /// @@ -145,7 +178,7 @@ namespace Umbraco.Core.Services RepositoryFactory repositoryFactory, IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider, IUnitOfWorkProvider fileUnitOfWorkProvider, - BasePublishingStrategy publishingStrategy, + IPublishingStrategy publishingStrategy, CacheHelper cache, ILogger logger, IEventMessagesFactory eventMessagesFactory, @@ -171,7 +204,7 @@ namespace Umbraco.Core.Services private void BuildServiceCache( IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider, IUnitOfWorkProvider fileUnitOfWorkProvider, - BasePublishingStrategy publishingStrategy, + IPublishingStrategy publishingStrategy, CacheHelper cache, RepositoryFactory repositoryFactory, ILogger logger, diff --git a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs index 44c5a4d284..73a9bd1939 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs @@ -108,21 +108,21 @@ namespace Umbraco.Tests.TestHelpers Container.Register(factory => Logger); Container.Register(factory => CacheHelper); Container.Register(factory => ProfilingLogger); - Container.Register(factory => SettingsForTests.GetDefault(), new PerContainerLifetime()); - Container.Register(factory => settings.Content, new PerContainerLifetime()); - Container.Register(factory => settings.Templates, new PerContainerLifetime()); + Container.RegisterSingleton(factory => SettingsForTests.GetDefault()); + Container.RegisterSingleton(factory => settings.Content); + Container.RegisterSingleton(factory => settings.Templates); Container.Register(factory => CacheHelper.RuntimeCache); Container.Register(); Container.Register(factory => new MediaFileSystem(Mock.Of())); //replace some stuff Container.Register(factory => SqlSyntax); - Container.Register(factory => Mock.Of(), "ScriptFileSystem", new PerContainerLifetime()); - Container.Register(factory => Mock.Of(), "PartialViewFileSystem", new PerContainerLifetime()); - Container.Register(factory => Mock.Of(), "PartialViewMacroFileSystem", new PerContainerLifetime()); - Container.Register(factory => Mock.Of(), "StylesheetFileSystem", new PerContainerLifetime()); - Container.Register(factory => Mock.Of(), "MasterpageFileSystem", new PerContainerLifetime()); - Container.Register(factory => Mock.Of(), "ViewFileSystem", new PerContainerLifetime()); + Container.RegisterSingleton(factory => Mock.Of(), "ScriptFileSystem"); + Container.RegisterSingleton(factory => Mock.Of(), "PartialViewFileSystem"); + Container.RegisterSingleton(factory => Mock.Of(), "PartialViewMacroFileSystem"); + Container.RegisterSingleton(factory => Mock.Of(), "StylesheetFileSystem"); + Container.RegisterSingleton(factory => Mock.Of(), "MasterpageFileSystem"); + Container.RegisterSingleton(factory => Mock.Of(), "ViewFileSystem"); } private static readonly object Locker = new object(); diff --git a/src/Umbraco.Tests/TestHelpers/BaseUsingSqlCeSyntax.cs b/src/Umbraco.Tests/TestHelpers/BaseUsingSqlCeSyntax.cs index b31483484d..f1ff37bbb6 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseUsingSqlCeSyntax.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseUsingSqlCeSyntax.cs @@ -8,6 +8,7 @@ using Umbraco.Core.ObjectResolution; using Umbraco.Core.Persistence.Mappers; using Umbraco.Core.Persistence.SqlSyntax; using Umbraco.Core.Profiling; +using Umbraco.Core.DependencyInjection; namespace Umbraco.Tests.TestHelpers { @@ -29,9 +30,9 @@ namespace Umbraco.Tests.TestHelpers public virtual void Initialize() { var container = new ServiceContainer(); - container.Register(factory => SqlSyntax, new PerContainerLifetime()); - container.Register(factory => Mock.Of(), new PerContainerLifetime()); - container.Register(factory => Mock.Of(), new PerContainerLifetime()); + container.RegisterSingleton(factory => SqlSyntax); + container.RegisterSingleton(factory => Mock.Of()); + container.RegisterSingleton(factory => Mock.Of()); _mappingResolver = new MappingResolver(container, Mock.Of(), () => PluginManager.Current.ResolveAssignedMapperTypes()); diff --git a/src/Umbraco.Web/Models/Trees/IApplication.cs b/src/Umbraco.Web/Models/Trees/IApplication.cs new file mode 100644 index 0000000000..eb957761ff --- /dev/null +++ b/src/Umbraco.Web/Models/Trees/IApplication.cs @@ -0,0 +1,8 @@ +namespace Umbraco.Web.Models.Trees +{ + /// + /// Marker interface for created applications in the umbraco backoffice + /// + public interface IApplication + { } +} \ No newline at end of file diff --git a/src/Umbraco.Web/WebBootManager.cs b/src/Umbraco.Web/WebBootManager.cs index 0a47355a19..20cfc514fb 100644 --- a/src/Umbraco.Web/WebBootManager.cs +++ b/src/Umbraco.Web/WebBootManager.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.Configuration; using System.Linq; -using System.Reflection; using System.Web; using System.Web.Configuration; using System.Web.Http; @@ -14,39 +13,31 @@ using ClientDependency.Core.Config; using Examine; using Examine.Config; using LightInject; -using umbraco; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.Dictionary; using Umbraco.Core.Logging; using Umbraco.Core.Macros; -using Umbraco.Core.ObjectResolution; using Umbraco.Core.Profiling; using Umbraco.Core.PropertyEditors; using Umbraco.Core.PropertyEditors.ValueConverters; using Umbraco.Core.Sync; using Umbraco.Web.Dictionary; using Umbraco.Web.Install; -using Umbraco.Web.Macros; using Umbraco.Web.Media; using Umbraco.Web.Media.ThumbnailProviders; using Umbraco.Web.Models; using Umbraco.Web.Mvc; -using Umbraco.Web.PropertyEditors; -using Umbraco.Web.PropertyEditors.ValueConverters; using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.XmlPublishedCache; using Umbraco.Web.Routing; using Umbraco.Web.Security; -using Umbraco.Web.Scheduling; using Umbraco.Web.UI.JavaScript; using Umbraco.Web.WebApi; -using umbraco.BusinessLogic; using Umbraco.Core.Events; -using Umbraco.Core.Persistence; -using Umbraco.Core.Persistence.UnitOfWork; -using Umbraco.Core.Publishing; using Umbraco.Core.Services; +using Umbraco.Web.Services; +using Umbraco.Core.DependencyInjection; using GlobalSettings = Umbraco.Core.Configuration.GlobalSettings; using ProfilingViewEngine = Umbraco.Core.Profiling.ProfilingViewEngine; using TypeHelper = Umbraco.Core.TypeHelper; @@ -316,16 +307,18 @@ namespace Umbraco.Web container.EnablePerWebRequestScope(); - container.Register(new PerContainerLifetime()); - container.Register(factory => new PublishedContentCache(), new PerContainerLifetime()); - container.Register(new PerContainerLifetime()); + container.RegisterSingleton(); + container.RegisterSingleton(factory => new PublishedContentCache()); + container.RegisterSingleton(); //no need to declare as per request, currently we manage it's lifetime as the singleton container.Register(factory => UmbracoContext.Current); - container.Register(new PerRequestLifeTime()); + container.RegisterSingleton(); //Replace services: container.Register(); + container.RegisterSingleton(); + container.RegisterSingleton(); } ///