Creates a RegisterSingleton ext method for easier registration

This commit is contained in:
Shannon
2016-02-17 17:14:54 +01:00
parent 4fa1ce42dc
commit 172c4a59d7
9 changed files with 160 additions and 65 deletions

View File

@@ -162,16 +162,16 @@ namespace Umbraco.Core
container.Register<IServiceContainer>(factory => container);
//Logging
container.Register<ILogger>(factory => _umbracoApplication.Logger, new PerContainerLifetime());
container.Register<IProfiler>(factory => ProfilingLogger.Profiler, new PerContainerLifetime());
container.Register<ProfilingLogger>(factory => ProfilingLogger, new PerContainerLifetime());
container.RegisterSingleton<ILogger>(factory => _umbracoApplication.Logger);
container.RegisterSingleton<IProfiler>(factory => ProfilingLogger.Profiler);
container.RegisterSingleton<ProfilingLogger>(factory => ProfilingLogger);
//Config
container.RegisterFrom<ConfigurationCompositionRoot>();
//Cache
container.Register<CacheHelper>(factory => ApplicationCache, new PerContainerLifetime());
container.Register<IRuntimeCacheProvider>(factory => ApplicationCache.RuntimeCache, new PerContainerLifetime());
container.RegisterSingleton<CacheHelper>(factory => ApplicationCache);
container.RegisterSingleton<IRuntimeCacheProvider>(factory => ApplicationCache.RuntimeCache);
//Datalayer/Repositories/SQL/Database/etc...
container.RegisterFrom<RepositoryCompositionRoot>();
@@ -179,10 +179,10 @@ namespace Umbraco.Core
//Data Services/ServiceContext/etc...
container.RegisterFrom<ServicesCompositionRoot>();
container.Register<IServiceProvider, ActivatorServiceProvider>();
container.Register<PluginManager>(factory => PluginManager, new PerContainerLifetime());
container.RegisterSingleton<IServiceProvider, ActivatorServiceProvider>();
container.RegisterSingleton<PluginManager>(factory => PluginManager);
container.Register<ApplicationContext>(new PerContainerLifetime());
container.RegisterSingleton<ApplicationContext>();
container.Register<MediaFileSystem>(factory => FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>());
}

View File

@@ -6,6 +6,47 @@ namespace Umbraco.Core.DependencyInjection
{
internal static class LightInjectExtensions
{
/// <summary>
/// Extension method to register a singleton (syntax sugar)
/// </summary>
public static void RegisterSingleton<TService>(this IServiceRegistry container, Func<IServiceFactory, TService> factory, string serviceName)
{
container.Register<TService>(factory, new PerContainerLifetime());
}
/// <summary>
/// Extension method to register a singleton (syntax sugar)
/// </summary>
/// <typeparam name="TContract"></typeparam>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="container"></param>
public static void RegisterSingleton<TContract, TImplementation>(this IServiceRegistry container)
where TImplementation : TContract
{
container.Register<TContract, TImplementation>(new PerContainerLifetime());
}
/// <summary>
/// Extension method to register a singleton (syntax sugar)
/// </summary>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="container"></param>
public static void RegisterSingleton<TImplementation>(this IServiceRegistry container)
{
container.Register<TImplementation>(new PerContainerLifetime());
}
/// <summary>
/// Extension method to register a singleton (syntax sugar)
/// </summary>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="container"></param>
/// <param name="factory"></param>
public static void RegisterSingleton<TImplementation>(this IServiceRegistry container, Func<IServiceFactory, TImplementation> factory)
{
container.Register<TImplementation>(factory);
}
/// <summary>
/// In order for LightInject to deal with enumerables of the same type, each one needs to be named individually
/// </summary>

View File

@@ -21,25 +21,24 @@ namespace Umbraco.Core.DependencyInjection
{
public void Compose(IServiceRegistry container)
{
container.Register<IDatabaseFactory>(factory => new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName, factory.GetInstance<ILogger>()), new PerContainerLifetime());
container.Register<DatabaseContext>(factory => GetDbContext(factory), new PerContainerLifetime());
container.Register<SqlSyntaxProviders>(factory => SqlSyntaxProviders.CreateDefault(factory.GetInstance<ILogger>()), new PerContainerLifetime());
container.Register<IUnitOfWorkProvider, FileUnitOfWorkProvider>(new PerContainerLifetime());
container.Register<IDatabaseUnitOfWorkProvider>(factory => new PetaPocoUnitOfWorkProvider(factory.GetInstance<ILogger>()), new PerContainerLifetime());
container.Register<IMappingResolver>(factory => new MappingResolver(
container.RegisterSingleton<IDatabaseFactory>(factory => new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName, factory.GetInstance<ILogger>()));
container.RegisterSingleton<DatabaseContext>(factory => GetDbContext(factory));
container.RegisterSingleton<SqlSyntaxProviders>(factory => SqlSyntaxProviders.CreateDefault(factory.GetInstance<ILogger>()));
container.RegisterSingleton<IUnitOfWorkProvider, FileUnitOfWorkProvider>();
container.RegisterSingleton<IDatabaseUnitOfWorkProvider>(factory => new PetaPocoUnitOfWorkProvider(factory.GetInstance<ILogger>()));
container.RegisterSingleton<IMappingResolver>(factory => new MappingResolver(
factory.GetInstance<IServiceContainer>(),
factory.GetInstance<ILogger>(),
() => factory.GetInstance<PluginManager>().ResolveAssignedMapperTypes()),
new PerContainerLifetime());
() => factory.GetInstance<PluginManager>().ResolveAssignedMapperTypes()));
container.Register<RepositoryFactory>();
container.Register<ISqlSyntaxProvider>(factory => factory.GetInstance<DatabaseContext>().SqlSyntax);
container.Register<CacheHelper>(factory => CacheHelper.CreateDisabledCacheHelper(), "DisabledCache", new PerContainerLifetime());
container.Register<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.Scripts), "ScriptFileSystem", new PerContainerLifetime());
container.Register<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.MvcViews + "/Partials/"), "PartialViewFileSystem", new PerContainerLifetime());
container.Register<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.MvcViews + "/MacroPartials/"), "PartialViewMacroFileSystem", new PerContainerLifetime());
container.Register<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.Css), "StylesheetFileSystem", new PerContainerLifetime());
container.Register<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.Masterpages), "MasterpageFileSystem", new PerContainerLifetime());
container.Register<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.MvcViews), "ViewFileSystem", new PerContainerLifetime());
container.RegisterSingleton<CacheHelper>(factory => CacheHelper.CreateDisabledCacheHelper(), "DisabledCache");
container.RegisterSingleton<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.Scripts), "ScriptFileSystem");
container.RegisterSingleton<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.MvcViews + "/Partials/"), "PartialViewFileSystem");
container.RegisterSingleton<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.MvcViews + "/MacroPartials/"), "PartialViewMacroFileSystem");
container.RegisterSingleton<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.Css), "StylesheetFileSystem");
container.RegisterSingleton<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.Masterpages), "MasterpageFileSystem");
container.RegisterSingleton<IFileSystem>(factory => new PhysicalFileSystem(SystemDirectories.MvcViews), "ViewFileSystem");
//Repository factories:
//NOTE: Wondering if we can pass in parameters at resolution time with LightInject

View File

@@ -14,22 +14,42 @@ namespace Umbraco.Core.DependencyInjection
{
public void Compose(IServiceRegistry container)
{
container.Register<BasePublishingStrategy, PublishingStrategy>(new PerContainerLifetime());
container.RegisterSingleton<IPublishingStrategy, PublishingStrategy>();
//These will be replaced by the web boot manager when running in a web context
container.Register<IEventMessagesFactory, TransientMessagesFactory>();
container.Register<ServiceContext>(factory => new ServiceContext(
factory.GetInstance<RepositoryFactory>(),
factory.GetInstance<IDatabaseUnitOfWorkProvider>(),
factory.GetInstance<IUnitOfWorkProvider>(),
factory.GetInstance<BasePublishingStrategy>(),
factory.GetInstance<CacheHelper>(),
factory.GetInstance<ILogger>(),
factory.GetInstance<IEventMessagesFactory>(),
factory.GetInstance<IEnumerable<IUrlSegmentProvider>>()),
new PerContainerLifetime());
//the context
container.RegisterSingleton<ServiceContext>();
//now the services...
container.RegisterSingleton<IMigrationEntryService, MigrationEntryService>();
container.RegisterSingleton<IPublicAccessService, PublicAccessService>();
container.RegisterSingleton<ITaskService, TaskService>();
container.RegisterSingleton<IDomainService, DomainService>();
container.RegisterSingleton<IAuditService, AuditService>();
container.RegisterSingleton<ILocalizedTextService, LocalizedTextService>();
container.RegisterSingleton<ITagService, TagService>();
container.RegisterSingleton<IContentService, ContentService>();
container.RegisterSingleton<IUserService, UserService>();
container.RegisterSingleton<IMemberService, MemberService>();
container.RegisterSingleton<IMediaService, MediaService>();
container.RegisterSingleton<IContentTypeService, ContentTypeService>();
container.RegisterSingleton<IDataTypeService, DataTypeService>();
container.RegisterSingleton<IFileService, FileService>();
container.RegisterSingleton<ILocalizationService, LocalizationService>();
container.RegisterSingleton<IPackagingService, PackagingService>();
container.RegisterSingleton<IServerRegistrationService, ServerRegistrationService>();
container.RegisterSingleton<IEntityService, EntityService>();
container.RegisterSingleton<IRelationService, RelationService>();
container.RegisterSingleton<IMacroService, MacroService>();
container.RegisterSingleton<IMemberTypeService, MemberTypeService>();
container.RegisterSingleton<INotificationService, NotificationService>();
container.RegisterSingleton<IExternalLoginService, ExternalLoginService>();
//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<IApplicationTreeService, EmptyApplicationTreeService>();
container.RegisterSingleton<ISectionService, EmptySectionService>();
}
}
}

View File

@@ -48,6 +48,39 @@ namespace Umbraco.Core.Services
private Lazy<INotificationService> _notificationService;
private Lazy<IExternalLoginService> _externalLoginService;
/// <summary>
/// Constructor used for IoC
/// </summary>
public ServiceContext(Lazy<IMigrationEntryService> migrationEntryService, Lazy<IPublicAccessService> publicAccessService, Lazy<ITaskService> taskService, Lazy<IDomainService> domainService, Lazy<IAuditService> auditService, Lazy<ILocalizedTextService> localizedTextService, Lazy<ITagService> tagService, Lazy<IContentService> contentService, Lazy<IUserService> userService, Lazy<IMemberService> memberService, Lazy<IMediaService> mediaService, Lazy<IContentTypeService> contentTypeService, Lazy<IDataTypeService> dataTypeService, Lazy<IFileService> fileService, Lazy<ILocalizationService> localizationService, Lazy<IPackagingService> packagingService, Lazy<IServerRegistrationService> serverRegistrationService, Lazy<IEntityService> entityService, Lazy<IRelationService> relationService, Lazy<IApplicationTreeService> treeService, Lazy<ISectionService> sectionService, Lazy<IMacroService> macroService, Lazy<IMemberTypeService> memberTypeService, Lazy<IMemberGroupService> memberGroupService, Lazy<INotificationService> notificationService, Lazy<IExternalLoginService> 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;
}
/// <summary>
/// public ctor - will generally just be used for unit testing all items are optional and if not specified, the defaults will be used
/// </summary>
@@ -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,

View File

@@ -108,21 +108,21 @@ namespace Umbraco.Tests.TestHelpers
Container.Register<ILogger>(factory => Logger);
Container.Register<CacheHelper>(factory => CacheHelper);
Container.Register<ProfilingLogger>(factory => ProfilingLogger);
Container.Register<IUmbracoSettingsSection>(factory => SettingsForTests.GetDefault(), new PerContainerLifetime());
Container.Register<IContentSection>(factory => settings.Content, new PerContainerLifetime());
Container.Register<ITemplatesSection>(factory => settings.Templates, new PerContainerLifetime());
Container.RegisterSingleton<IUmbracoSettingsSection>(factory => SettingsForTests.GetDefault());
Container.RegisterSingleton<IContentSection>(factory => settings.Content);
Container.RegisterSingleton<ITemplatesSection>(factory => settings.Templates);
Container.Register<IRuntimeCacheProvider>(factory => CacheHelper.RuntimeCache);
Container.Register<IServiceProvider, ActivatorServiceProvider>();
Container.Register<MediaFileSystem>(factory => new MediaFileSystem(Mock.Of<IFileSystem>()));
//replace some stuff
Container.Register<ISqlSyntaxProvider>(factory => SqlSyntax);
Container.Register<IFileSystem>(factory => Mock.Of<IFileSystem>(), "ScriptFileSystem", new PerContainerLifetime());
Container.Register<IFileSystem>(factory => Mock.Of<IFileSystem>(), "PartialViewFileSystem", new PerContainerLifetime());
Container.Register<IFileSystem>(factory => Mock.Of<IFileSystem>(), "PartialViewMacroFileSystem", new PerContainerLifetime());
Container.Register<IFileSystem>(factory => Mock.Of<IFileSystem>(), "StylesheetFileSystem", new PerContainerLifetime());
Container.Register<IFileSystem>(factory => Mock.Of<IFileSystem>(), "MasterpageFileSystem", new PerContainerLifetime());
Container.Register<IFileSystem>(factory => Mock.Of<IFileSystem>(), "ViewFileSystem", new PerContainerLifetime());
Container.RegisterSingleton<IFileSystem>(factory => Mock.Of<IFileSystem>(), "ScriptFileSystem");
Container.RegisterSingleton<IFileSystem>(factory => Mock.Of<IFileSystem>(), "PartialViewFileSystem");
Container.RegisterSingleton<IFileSystem>(factory => Mock.Of<IFileSystem>(), "PartialViewMacroFileSystem");
Container.RegisterSingleton<IFileSystem>(factory => Mock.Of<IFileSystem>(), "StylesheetFileSystem");
Container.RegisterSingleton<IFileSystem>(factory => Mock.Of<IFileSystem>(), "MasterpageFileSystem");
Container.RegisterSingleton<IFileSystem>(factory => Mock.Of<IFileSystem>(), "ViewFileSystem");
}
private static readonly object Locker = new object();

View File

@@ -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<ISqlSyntaxProvider>(factory => SqlSyntax, new PerContainerLifetime());
container.Register<ILogger>(factory => Mock.Of<ILogger>(), new PerContainerLifetime());
container.Register<IProfiler>(factory => Mock.Of<IProfiler>(), new PerContainerLifetime());
container.RegisterSingleton<ISqlSyntaxProvider>(factory => SqlSyntax);
container.RegisterSingleton<ILogger>(factory => Mock.Of<ILogger>());
container.RegisterSingleton<IProfiler>(factory => Mock.Of<IProfiler>());
_mappingResolver = new MappingResolver(container, Mock.Of<ILogger>(),
() => PluginManager.Current.ResolveAssignedMapperTypes());

View File

@@ -0,0 +1,8 @@
namespace Umbraco.Web.Models.Trees
{
/// <summary>
/// Marker interface for created applications in the umbraco backoffice
/// </summary>
public interface IApplication
{ }
}

View File

@@ -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<IUmbracoContextAccessor, DefaultUmbracoContextAccessor>(new PerContainerLifetime());
container.Register<IPublishedContentCache>(factory => new PublishedContentCache(), new PerContainerLifetime());
container.Register<IPublishedMediaCache, PublishedMediaCache>(new PerContainerLifetime());
container.RegisterSingleton<IUmbracoContextAccessor, DefaultUmbracoContextAccessor>();
container.RegisterSingleton<IPublishedContentCache>(factory => new PublishedContentCache());
container.RegisterSingleton<IPublishedMediaCache, PublishedMediaCache>();
//no need to declare as per request, currently we manage it's lifetime as the singleton
container.Register<UmbracoContext>(factory => UmbracoContext.Current);
container.Register<UmbracoHelper>(new PerRequestLifeTime());
container.RegisterSingleton<UmbracoHelper>();
//Replace services:
container.Register<IEventMessagesFactory, RequestLifespanMessagesFactory>();
container.RegisterSingleton<IApplicationTreeService, ApplicationTreeService>();
container.RegisterSingleton<ISectionService, SectionService>();
}
/// <summary>