using LightInject;
using Umbraco.Core.Cache;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Mappers;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Plugins;
namespace Umbraco.Core.DependencyInjection
{
///
/// Sets the IoC container for the umbraco data layer/repositories/sql/database/etc...
///
public sealed class RepositoryCompositionRoot : ICompositionRoot
{
public const string DisabledCache = "DisabledCache";
public void Compose(IServiceRegistry container)
{
// register syntax providers
container.Register("MySqlSyntaxProvider");
container.Register("SqlCeSyntaxProvider");
container.Register("SqlServerSyntaxProvider");
// 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();
// register a database accessor - will be replaced
container.RegisterSingleton();
// register database context
container.RegisterSingleton();
// register IUnitOfWork providers
container.RegisterSingleton();
container.RegisterSingleton();
// register mapping resover
// using a factory because... no time to clean it up at the moment
container.RegisterSingleton(factory => new MappingResolver(
factory.GetInstance(),
factory.GetInstance(),
() => factory.GetInstance().ResolveAssignedMapperTypes()));
// register repository factory
container.RegisterSingleton();
// register file systems
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");
// 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);
// resolve ctor dependency from GetInstance() runtimeArguments, if possible - 'factory' is
// the container, 'info' describes the ctor argument, and 'args' contains the args that
// were passed to GetInstance() - use first arg if it is the right type,
//
// for IDatabaseUnitOfWork
container.RegisterConstructorDependency((factory, info, args) => args.Length > 0 ? args[0] as IDatabaseUnitOfWork : null);
// for IUnitOfWork
container.RegisterConstructorDependency((factory, info, args) => args.Length > 0 ? args[0] as IUnitOfWork : 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();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
// repositories that depend on a filesystem
// these have an annotated ctor parameter to pick the right file system
container.Register();
container.Register();
container.Register();
container.Register();
}
}
}