Core.Persistence - massive cleanup
This commit is contained in:
@@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using LightInject;
|
||||
using Umbraco.Core.Cache;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Configuration.UmbracoSettings;
|
||||
using Umbraco.Core.IO;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Persistence;
|
||||
@@ -19,24 +14,42 @@ namespace Umbraco.Core.DependencyInjection
|
||||
/// </summary>
|
||||
public sealed class RepositoryCompositionRoot : ICompositionRoot
|
||||
{
|
||||
public const string DisabledCache = "DisabledCache";
|
||||
|
||||
public void Compose(IServiceRegistry container)
|
||||
{
|
||||
container.RegisterSingleton<IDatabaseFactory>(factory => new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName, factory.GetInstance<ILogger>()));
|
||||
container.RegisterSingleton<DatabaseContext>(factory => GetDbContext(factory));
|
||||
// register syntax providers
|
||||
container.Register<ISqlSyntaxProvider, MySqlSyntaxProvider>("MySqlSyntaxProvider");
|
||||
container.Register<ISqlSyntaxProvider, SqlCeSyntaxProvider>("SqlCeSyntaxProvider");
|
||||
container.Register<ISqlSyntaxProvider, SqlServerSyntaxProvider>("SqlServerSyntaxProvider");
|
||||
|
||||
container.RegisterSingleton<SqlSyntaxProviders>(factory => SqlSyntaxProviders.CreateDefault(factory.GetInstance<ILogger>()));
|
||||
container.Register<ISqlSyntaxProvider>(factory => factory.GetInstance<DatabaseContext>().SqlSyntax);
|
||||
// register database factory
|
||||
// will be initialized with syntax providers and a logger, and will try to configure
|
||||
// from the default connection string name, if possible, else will remain non-configured
|
||||
// until the database context configures it properly (eg when installing)
|
||||
container.RegisterSingleton<IDatabaseFactory, DefaultDatabaseFactory>();
|
||||
|
||||
// register database context
|
||||
container.RegisterSingleton<DatabaseContext>();
|
||||
|
||||
// register IUnitOfWork providers
|
||||
// using a factory for NPocoUnitOfWorkProvider because it has another ctor accepting
|
||||
// one single parameter - we should get rid of it but it is used hundreds of times in
|
||||
// unit tests and I am lazy
|
||||
container.RegisterSingleton<IUnitOfWorkProvider, FileUnitOfWorkProvider>();
|
||||
container.RegisterSingleton<IDatabaseUnitOfWorkProvider>(factory => new NPocoUnitOfWorkProvider(factory.GetInstance<IDatabaseFactory>()));
|
||||
|
||||
// register mapping resover
|
||||
// using a factory because... no time to clean it up at the moment
|
||||
container.RegisterSingleton<IMappingResolver>(factory => new MappingResolver(
|
||||
factory.GetInstance<IServiceContainer>(),
|
||||
factory.GetInstance<ILogger>(),
|
||||
() => factory.GetInstance<PluginManager>().ResolveAssignedMapperTypes()));
|
||||
container.Register<RepositoryFactory>();
|
||||
container.RegisterSingleton<CacheHelper>(factory => CacheHelper.CreateDisabledCacheHelper(), "DisabledCache");
|
||||
|
||||
// register repository factory
|
||||
container.RegisterSingleton<RepositoryFactory>();
|
||||
|
||||
// register file systems
|
||||
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");
|
||||
@@ -44,47 +57,23 @@ namespace Umbraco.Core.DependencyInjection
|
||||
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
|
||||
// without having to manually specify the ctor for each one, have asked here: https://github.com/seesharper/LightInject/issues/237
|
||||
container.Register<IDatabaseUnitOfWork, INotificationsRepository>((factory, work) => new NotificationsRepository(work, factory.GetInstance<ISqlSyntaxProvider>()));
|
||||
|
||||
container.Register<IDatabaseUnitOfWork, IExternalLoginRepository>((factory, work) => new ExternalLoginRepository(
|
||||
work,
|
||||
factory.GetInstance<CacheHelper>(),
|
||||
factory.GetInstance<ILogger>(),
|
||||
factory.GetInstance<ISqlSyntaxProvider>(),
|
||||
factory.GetInstance<IMappingResolver>()));
|
||||
// register cache helpers
|
||||
// the main cache helper is registered by CoreBootManager and is used by most repositories
|
||||
// the disabled one is used by those repositories that have an annotated ctor parameter
|
||||
container.RegisterSingleton(factory => CacheHelper.CreateDisabledCacheHelper(), DisabledCache);
|
||||
|
||||
//here we are using some nice IoC magic:
|
||||
//https://github.com/seesharper/LightInject/issues/237
|
||||
//This tells the container that anytime there is a ctor dependency for IDatabaseUnitOfWork and it's available as the first
|
||||
//arg in the runtimeArgs, to use that. This means we donn't have to explicitly define all ctor's for all repositories which
|
||||
//saves us a lot of code.
|
||||
container.RegisterConstructorDependency<IDatabaseUnitOfWork>((factory, info, runtimeArguments) =>
|
||||
{
|
||||
var uow = runtimeArguments.Length > 0 ? runtimeArguments[0] as IDatabaseUnitOfWork : null;
|
||||
return uow;
|
||||
});
|
||||
//This ensures that the correct CacheHelper is returned for the right repos
|
||||
container.RegisterConstructorDependency<CacheHelper>((factory, info, runtimeArguments) =>
|
||||
{
|
||||
var declaringType = info.Member.DeclaringType;
|
||||
var disabledCacheRepos = new[]
|
||||
{
|
||||
typeof (ITaskRepository),
|
||||
typeof (ITaskTypeRepository),
|
||||
typeof (IAuditRepository),
|
||||
typeof (IRelationRepository),
|
||||
typeof (IRelationTypeRepository),
|
||||
typeof (IAuditRepository),
|
||||
typeof (IMigrationEntryRepository)
|
||||
};
|
||||
return disabledCacheRepos.Any(x => TypeHelper.IsTypeAssignableFrom(x, declaringType))
|
||||
? factory.GetInstance<CacheHelper>("DisabledCache")
|
||||
: factory.GetInstance<CacheHelper>();
|
||||
});
|
||||
// register IDatabaseUnitOfWork
|
||||
// resolve ctor dependency from GetInstance() runtimeArguments if possible
|
||||
container.RegisterConstructorDependency((factory, info, runtimeArguments) =>
|
||||
runtimeArguments.Length > 0 ? runtimeArguments[0] as IDatabaseUnitOfWork : null);
|
||||
|
||||
// register repositories
|
||||
// repos depend on various things, and a IDatabaseUnitOfWork (registered above)
|
||||
// some repositories have an annotated ctor parameter to pick the right cache helper
|
||||
|
||||
// repositories
|
||||
container.Register<INotificationsRepository, NotificationsRepository>();
|
||||
container.Register<IExternalLoginRepository, ExternalLoginRepository>();
|
||||
container.Register<IPublicAccessRepository, PublicAccessRepository>();
|
||||
container.Register<ITagRepository, TagRepository>();
|
||||
container.Register<IContentRepository, ContentRepository>();
|
||||
@@ -110,47 +99,14 @@ namespace Umbraco.Core.DependencyInjection
|
||||
container.Register<IRelationRepository, RelationRepository>();
|
||||
container.Register<IRelationTypeRepository, RelationTypeRepository>();
|
||||
container.Register<IMigrationEntryRepository, MigrationEntryRepository>();
|
||||
container.Register<IServerRegistrationRepository, ServerRegistrationRepository>();
|
||||
|
||||
//These repo registrations require custom injections so we need to define them:
|
||||
|
||||
container.Register<IDatabaseUnitOfWork, IServerRegistrationRepository>((factory, work) => new ServerRegistrationRepository(
|
||||
work,
|
||||
factory.GetInstance<CacheHelper>().StaticCache, //special static cache scenario
|
||||
factory.GetInstance<ILogger>(),
|
||||
factory.GetInstance<ISqlSyntaxProvider>(),
|
||||
factory.GetInstance<IMappingResolver>()));
|
||||
container.Register<IUnitOfWork, IScriptRepository>((factory, work) => new ScriptRepository(
|
||||
work,
|
||||
factory.GetInstance<IFileSystem>("ScriptFileSystem"),
|
||||
factory.GetInstance<IContentSection>()));
|
||||
container.Register<IUnitOfWork, IPartialViewRepository>((factory, work) => new PartialViewRepository(
|
||||
work,
|
||||
factory.GetInstance<IFileSystem>("PartialViewFileSystem")),
|
||||
serviceName: "PartialViewRepository");
|
||||
container.Register<IUnitOfWork, IPartialViewRepository>((factory, work) => new PartialViewMacroRepository(
|
||||
work,
|
||||
factory.GetInstance<IFileSystem>("PartialViewMacroFileSystem")),
|
||||
serviceName: "PartialViewMacroRepository");
|
||||
container.Register<IUnitOfWork, IStylesheetRepository>((factory, work) => new StylesheetRepository(
|
||||
work,
|
||||
factory.GetInstance<IFileSystem>("StylesheetFileSystem")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates and initializes the db context when IoC requests it
|
||||
/// </summary>
|
||||
/// <param name="container"></param>
|
||||
/// <returns></returns>
|
||||
private DatabaseContext GetDbContext(IServiceFactory container)
|
||||
{
|
||||
var dbCtx = new DatabaseContext(
|
||||
container.GetInstance<IDatabaseFactory>(),
|
||||
container.GetInstance<ILogger>(),
|
||||
container.GetInstance<SqlSyntaxProviders>());
|
||||
|
||||
//when it's first created we need to initialize it
|
||||
dbCtx.Initialize();
|
||||
return dbCtx;
|
||||
// repositories that depend on a filesystem
|
||||
// these have an annotated ctor parameter to pick the right file system
|
||||
container.Register<IScriptRepository, ScriptRepository>();
|
||||
container.Register<IPartialViewRepository, PartialViewRepository>("PartialViewRepository");
|
||||
container.Register<IPartialViewRepository, PartialViewMacroRepository>("PartialViewMacroRepository");
|
||||
container.Register<IStylesheetRepository, StylesheetRepository>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user