2013-04-20 03:05:48 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
2013-05-09 09:38:20 +03:00
|
|
|
using NzbDrone.Common.Composition;
|
2013-05-08 08:47:15 +03:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-04-20 03:05:48 +03:00
|
|
|
using TinyIoC;
|
2013-05-09 09:38:20 +03:00
|
|
|
using NzbDrone.Common.Reflection;
|
2013-04-20 03:05:48 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Common
|
|
|
|
{
|
|
|
|
public abstract class ContainerBuilderBase
|
|
|
|
{
|
2013-05-06 00:24:33 +03:00
|
|
|
protected readonly TinyIoCContainer Container;
|
2013-04-20 03:05:48 +03:00
|
|
|
private readonly List<Type> _loadedTypes;
|
|
|
|
|
|
|
|
protected ContainerBuilderBase(params string[] assemblies)
|
|
|
|
{
|
|
|
|
Container = new TinyIoCContainer();
|
|
|
|
|
|
|
|
_loadedTypes = new List<Type>();
|
|
|
|
|
|
|
|
foreach (var assembly in assemblies)
|
|
|
|
{
|
|
|
|
_loadedTypes.AddRange(Assembly.Load(assembly).GetTypes());
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoRegisterInterfaces();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AutoRegisterInterfaces()
|
|
|
|
{
|
2013-05-08 08:47:15 +03:00
|
|
|
var simpleInterfaces = _loadedTypes.Where(t => t.IsInterface).ToList();
|
|
|
|
var appliedInterfaces = _loadedTypes.SelectMany(t => t.GetInterfaces()).Where(i => i.Assembly.FullName.Contains("NzbDrone")).ToList();
|
2013-04-20 03:05:48 +03:00
|
|
|
|
2013-05-08 08:47:15 +03:00
|
|
|
var contracts = simpleInterfaces.Union(appliedInterfaces)
|
|
|
|
.Except(new List<Type> { typeof(IMessage), typeof(ICommand), typeof(IEvent) });
|
|
|
|
|
|
|
|
|
|
|
|
var count = contracts.Count();
|
|
|
|
|
|
|
|
foreach (var contract in simpleInterfaces.Union(contracts))
|
2013-04-20 03:05:48 +03:00
|
|
|
{
|
|
|
|
AutoRegisterImplementations(contract);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void AutoRegisterImplementations<TContract>()
|
|
|
|
{
|
|
|
|
AutoRegisterImplementations(typeof(TContract));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AutoRegisterImplementations(Type contractType)
|
|
|
|
{
|
|
|
|
var implementations = GetImplementations(contractType).ToList();
|
|
|
|
|
|
|
|
if (implementations.Count == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (implementations.Count == 1)
|
|
|
|
{
|
2013-05-09 09:38:20 +03:00
|
|
|
if (implementations.Single().HasAttribute<SingletonAttribute>())
|
|
|
|
{
|
|
|
|
Container.Register(contractType, implementations.Single()).AsSingleton();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Container.Register(contractType, implementations.Single()).AsMultiInstance();
|
|
|
|
}
|
|
|
|
|
2013-04-27 05:03:34 +03:00
|
|
|
Container.RegisterMultiple(contractType, implementations).AsMultiInstance();
|
2013-04-20 03:05:48 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Container.RegisterMultiple(contractType, implementations).AsMultiInstance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<Type> GetImplementations(Type contractType)
|
|
|
|
{
|
|
|
|
return _loadedTypes
|
|
|
|
.Where(implementation =>
|
|
|
|
contractType.IsAssignableFrom(implementation) &&
|
|
|
|
!implementation.IsInterface &&
|
|
|
|
!implementation.IsAbstract
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|