Lidarr/NzbDrone/ContainerBuilder.cs

114 lines
4.0 KiB
C#
Raw Normal View History

2013-04-03 02:20:05 +00:00
using System;
2013-04-10 23:44:48 +00:00
using System.Collections.Generic;
2013-04-03 02:20:05 +00:00
using System.IO;
using System.Linq;
using System.Reflection;
using FluentMigrator.Runner;
using NLog;
using Nancy.Bootstrapper;
using NzbDrone.Api;
using NzbDrone.Common;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Migration.Framework;
using NzbDrone.Core.ExternalNotification;
2013-04-10 23:44:48 +00:00
using NzbDrone.Core.Organizer;
2013-04-03 02:20:05 +00:00
using NzbDrone.Core.RootFolders;
using TinyIoC;
namespace NzbDrone
{
public static class ContainerBuilder
{
2013-04-10 23:44:48 +00:00
private static readonly Logger Logger = LogManager.GetLogger("ContainerBuilder");
2013-04-03 02:20:05 +00:00
public static TinyIoCContainer Instance { get; private set; }
2013-04-10 23:44:48 +00:00
private static readonly List<Type> NzbDroneTypes;
2013-04-03 02:20:05 +00:00
static ContainerBuilder()
{
var container = new TinyIoCContainer();
2013-04-10 23:44:48 +00:00
NzbDroneTypes = new List<Type>();
NzbDroneTypes.AddRange(Assembly.Load("NzbDrone").GetTypes());
NzbDroneTypes.AddRange(Assembly.Load("NzbDrone.Common").GetTypes());
NzbDroneTypes.AddRange(Assembly.Load("NzbDrone.Core").GetTypes());
NzbDroneTypes.AddRange(Assembly.Load("NzbDrone.Api").GetTypes());
container.AutoRegisterInterfaces();
2013-04-03 02:20:05 +00:00
container.AutoRegisterImplementations<ExternalNotificationBase>();
container.Register<IEventAggregator, EventAggregator>().AsSingleton();
container.Register<INancyBootstrapper, TinyNancyBootstrapper>().AsSingleton();
container.Register<IAnnouncer, MigrationLogger>().AsSingleton();
2013-04-10 23:44:48 +00:00
container.Register<Router>().AsSingleton();
2013-04-03 02:20:05 +00:00
container.Register(typeof(IBasicRepository<RootFolder>), typeof(BasicRepository<RootFolder>)).AsMultiInstance();
2013-04-10 23:44:48 +00:00
container.Register(typeof(IBasicRepository<NameSpecification>), typeof(BasicRepository<NameSpecification>)).AsMultiInstance();
2013-04-03 02:20:05 +00:00
container.InitDatabase();
Instance = container;
}
private static void InitDatabase(this TinyIoCContainer container)
{
2013-04-10 23:44:48 +00:00
Logger.Info("Registering Database...");
2013-04-03 02:20:05 +00:00
//TODO: move this to factory
var environmentProvider = new EnvironmentProvider();
var appDataPath = environmentProvider.GetAppDataPath();
2013-04-10 23:44:48 +00:00
if (!Directory.Exists(appDataPath))
{
Directory.CreateDirectory(appDataPath);
}
2013-04-03 02:20:05 +00:00
2013-04-10 23:44:48 +00:00
container.Register((c, p) => c.Resolve<IDbFactory>().Create(environmentProvider.GetNzbDroneDatabase()));
2013-04-03 02:20:05 +00:00
}
2013-04-10 23:44:48 +00:00
private static void AutoRegisterInterfaces(this TinyIoCContainer container)
2013-04-03 02:20:05 +00:00
{
2013-04-10 23:44:48 +00:00
var interfaces = NzbDroneTypes.Where(t => t.IsInterface);
2013-04-03 02:20:05 +00:00
foreach (var contract in interfaces)
{
container.AutoRegisterImplementations(contract);
}
}
2013-04-10 23:44:48 +00:00
2013-04-03 02:20:05 +00:00
private static void AutoRegisterImplementations<TContract>(this TinyIoCContainer container)
{
container.AutoRegisterImplementations(typeof(TContract));
}
private static void AutoRegisterImplementations(this TinyIoCContainer container, Type contractType)
{
2013-04-10 23:44:48 +00:00
var implementations = GetImplementations(contractType).ToList();
2013-04-03 02:20:05 +00:00
2013-04-10 23:44:48 +00:00
if (implementations.Count == 0)
2013-04-03 02:20:05 +00:00
{
2013-04-10 23:44:48 +00:00
return;
}
if (implementations.Count == 1)
{
container.Register(contractType, implementations.Single()).AsMultiInstance();
}
else
{
container.RegisterMultiple(contractType, implementations).AsMultiInstance();
2013-04-03 02:20:05 +00:00
}
}
2013-04-10 23:44:48 +00:00
private static IEnumerable<Type> GetImplementations(Type contractType)
{
2013-04-10 23:44:48 +00:00
return NzbDroneTypes
.Where(implementation =>
contractType.IsAssignableFrom(implementation) &&
!implementation.IsInterface &&
!implementation.IsAbstract
);
}
2013-04-03 02:20:05 +00:00
}
}