Sonarr/NzbDrone.Core/ContainerExtentions.cs

92 lines
3.4 KiB
C#
Raw Normal View History

using System.IO;
using System.Linq;
using System.Reflection;
using Autofac;
using Autofac.Core;
using NLog;
using NzbDrone.Common;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Instrumentation;
using NzbDrone.Core.Providers.ExternalNotification;
using NzbDrone.Core.Providers.Indexer;
using NzbDrone.Core.Providers.Metadata;
using NzbDrone.Core.Providers.Search;
using PetaPoco;
namespace NzbDrone.Core
{
public static class ContainerExtentions
{
2013-01-19 18:54:00 +00:00
private static readonly Logger logger = LogManager.GetLogger("ServiceRegistration");
public static void RegisterCoreServices(this ContainerBuilder containerBuilder)
{
var core = Assembly.Load("NzbDrone.Core");
var common = Assembly.Load("NzbDrone.Common");
containerBuilder.RegisterAssembly(core);
containerBuilder.RegisterAssembly(common);
containerBuilder.InitDatabase();
containerBuilder.RegisterModule<LogInjectionModule>();
}
private static void RegisterAssembly(this ContainerBuilder container, Assembly assembly)
{
2013-01-19 19:42:06 +00:00
logger.Info("Registering Services from {0}", assembly.FullName);
container.RegisterAssemblyTypes(assembly)
.AsSelf()
.SingleInstance();
container.RegisterAssemblyTypes(assembly)
.AsImplementedInterfaces()
.SingleInstance();
container.RegisterAssemblyTypes(assembly)
2013-01-19 18:54:00 +00:00
.Where(t => t.IsSubclassOf(typeof(IndexerBase)))
.As<IndexerBase>().SingleInstance();
container.RegisterAssemblyTypes(assembly)
2013-01-19 18:54:00 +00:00
.Where(t => t.IsSubclassOf(typeof(SearchBase)))
.As<SearchBase>().SingleInstance();
container.RegisterAssemblyTypes(assembly)
2013-01-19 18:54:00 +00:00
.Where(t => t.IsSubclassOf(typeof(ExternalNotificationBase)))
.As<ExternalNotificationBase>().SingleInstance();
container.RegisterAssemblyTypes(assembly)
2013-01-19 18:54:00 +00:00
.Where(t => t.IsSubclassOf(typeof(MetadataBase)))
.As<MetadataBase>().SingleInstance();
}
2013-01-19 19:42:06 +00:00
private static void InitDatabase(this ContainerBuilder container)
{
2013-01-19 18:54:00 +00:00
logger.Info("Registering Database...");
var appDataPath = new EnvironmentProvider().GetAppDataPath();
if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath);
2013-01-19 19:42:06 +00:00
container.Register(c => c.Resolve<ConnectionFactory>().GetMainPetaPocoDb())
.As<IDatabase>();
2013-01-19 19:42:06 +00:00
container.Register(c => c.Resolve<ConnectionFactory>().GetLogPetaPocoDb(false))
.SingleInstance()
.Named<IDatabase>("DatabaseTarget");
2013-01-19 19:42:06 +00:00
container.Register(c => c.Resolve<ConnectionFactory>().GetLogPetaPocoDb())
.Named<IDatabase>("LogProvider");
2013-02-04 01:27:34 +00:00
container.Register(c =>
{
2013-02-17 01:52:40 +00:00
return c.Resolve<IObjectDbFactory>().Create("");
}).As<IObjectDatabase>().SingleInstance();
2013-02-04 01:27:34 +00:00
container.RegisterType<DatabaseTarget>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("DatabaseTarget"));
container.RegisterType<LogProvider>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("LogProvider"));
}
}
}