2013-04-20 03:05:48 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
2013-08-16 06:32:23 +03:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-05-08 08:47:15 +03:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-05-11 02:53:50 +03:00
|
|
|
using TinyIoC;
|
2013-04-20 03:05:48 +03:00
|
|
|
|
2013-05-11 02:53:50 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Common.Composition
|
2013-04-20 03:05:48 +03:00
|
|
|
{
|
|
|
|
public abstract class ContainerBuilderBase
|
|
|
|
{
|
|
|
|
private readonly List<Type> _loadedTypes;
|
|
|
|
|
2013-05-11 02:53:50 +03:00
|
|
|
public IContainer Container { get; private set; }
|
|
|
|
|
2013-08-16 06:32:23 +03:00
|
|
|
protected ContainerBuilderBase(IStartupArguments args, params string[] assemblies)
|
2013-04-20 03:05:48 +03:00
|
|
|
{
|
2013-08-16 06:32:23 +03:00
|
|
|
|
|
|
|
|
2013-04-20 03:05:48 +03:00
|
|
|
_loadedTypes = new List<Type>();
|
|
|
|
|
|
|
|
foreach (var assembly in assemblies)
|
|
|
|
{
|
|
|
|
_loadedTypes.AddRange(Assembly.Load(assembly).GetTypes());
|
|
|
|
}
|
|
|
|
|
2013-05-13 03:36:23 +03:00
|
|
|
Container = new Container(new TinyIoCContainer(), _loadedTypes);
|
2013-04-20 03:05:48 +03:00
|
|
|
AutoRegisterInterfaces();
|
2013-08-16 06:32:23 +03:00
|
|
|
Container.Register(args);
|
2013-04-20 03:05:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void AutoRegisterInterfaces()
|
|
|
|
{
|
2013-05-11 02:53:50 +03:00
|
|
|
var loadedInterfaces = _loadedTypes.Where(t => t.IsInterface).ToList();
|
2013-05-11 23:06:57 +03:00
|
|
|
var implementedInterfaces = _loadedTypes.SelectMany(t => t.GetInterfaces());
|
2013-04-20 03:05:48 +03:00
|
|
|
|
2013-05-11 02:53:50 +03:00
|
|
|
var contracts = loadedInterfaces.Union(implementedInterfaces).Where(c => !c.IsGenericTypeDefinition && !string.IsNullOrWhiteSpace(c.FullName))
|
2013-05-11 23:06:57 +03:00
|
|
|
.Where(c => !c.FullName.StartsWith("System"))
|
2013-05-11 09:16:10 +03:00
|
|
|
.Except(new List<Type> { typeof(IMessage), typeof(IEvent), typeof(IContainer) }).Distinct().OrderBy(c => c.FullName);
|
2013-05-08 08:47:15 +03:00
|
|
|
|
2013-05-11 02:53:50 +03:00
|
|
|
foreach (var contract in contracts)
|
2013-04-20 03:05:48 +03:00
|
|
|
{
|
|
|
|
AutoRegisterImplementations(contract);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void AutoRegisterImplementations<TContract>()
|
|
|
|
{
|
|
|
|
AutoRegisterImplementations(typeof(TContract));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AutoRegisterImplementations(Type contractType)
|
|
|
|
{
|
2013-05-13 03:36:23 +03:00
|
|
|
var implementations = Container.GetImplementations(contractType).Where(c => !c.IsGenericTypeDefinition).ToList();
|
2013-05-11 02:53:50 +03:00
|
|
|
|
|
|
|
|
2013-04-20 03:05:48 +03:00
|
|
|
|
|
|
|
if (implementations.Count == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (implementations.Count == 1)
|
|
|
|
{
|
2013-05-11 02:53:50 +03:00
|
|
|
var impl = implementations.Single();
|
2013-05-30 04:33:20 +03:00
|
|
|
Container.RegisterSingleton(contractType, impl);
|
2013-04-20 03:05:48 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-30 04:33:20 +03:00
|
|
|
Container.RegisterAllAsSingleton(contractType, implementations);
|
2013-04-20 03:05:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|