1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-27 01:57:21 +00:00
Lidarr/NzbDrone.Common/Composition/ContainerBuilderBase.cs
2013-05-29 18:33:20 -07:00

77 lines
No EOL
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using NzbDrone.Common.Messaging;
using NzbDrone.Common.Reflection;
using TinyIoC;
namespace NzbDrone.Common.Composition
{
public abstract class ContainerBuilderBase
{
private readonly List<Type> _loadedTypes;
public IContainer Container { get; private set; }
protected ContainerBuilderBase(params string[] assemblies)
{
_loadedTypes = new List<Type>();
foreach (var assembly in assemblies)
{
_loadedTypes.AddRange(Assembly.Load(assembly).GetTypes());
}
Container = new Container(new TinyIoCContainer(), _loadedTypes);
AutoRegisterInterfaces();
}
private void AutoRegisterInterfaces()
{
var loadedInterfaces = _loadedTypes.Where(t => t.IsInterface).ToList();
var implementedInterfaces = _loadedTypes.SelectMany(t => t.GetInterfaces());
var contracts = loadedInterfaces.Union(implementedInterfaces).Where(c => !c.IsGenericTypeDefinition && !string.IsNullOrWhiteSpace(c.FullName))
.Where(c => !c.FullName.StartsWith("System"))
.Except(new List<Type> { typeof(IMessage), typeof(IEvent), typeof(IContainer) }).Distinct().OrderBy(c => c.FullName);
foreach (var contract in contracts)
{
AutoRegisterImplementations(contract);
}
}
protected void AutoRegisterImplementations<TContract>()
{
AutoRegisterImplementations(typeof(TContract));
}
private void AutoRegisterImplementations(Type contractType)
{
var implementations = Container.GetImplementations(contractType).Where(c => !c.IsGenericTypeDefinition).ToList();
if (implementations.Count == 0)
{
return;
}
if (implementations.Count == 1)
{
var impl = implementations.Single();
Trace.WriteLine(string.Format("Registering {0} -> {1}", contractType.FullName, impl.Name));
Container.RegisterSingleton(contractType, impl);
}
else
{
Trace.WriteLine(string.Format("Registering {0} -> {1}", contractType.FullName, implementations.Count));
Container.RegisterAllAsSingleton(contractType, implementations);
}
}
}
}