mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-29 13:25:13 +02:00
back to tiny for now
This commit is contained in:
95
NzbDrone.Common/Composition/ContainerBuilderBase.cs
Normal file
95
NzbDrone.Common/Composition/ContainerBuilderBase.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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)
|
||||
{
|
||||
Container = new Container(new TinyIoCContainer());
|
||||
|
||||
_loadedTypes = new List<Type>();
|
||||
|
||||
foreach (var assembly in assemblies)
|
||||
{
|
||||
_loadedTypes.AddRange(Assembly.Load(assembly).GetTypes());
|
||||
}
|
||||
|
||||
AutoRegisterInterfaces();
|
||||
}
|
||||
|
||||
private void AutoRegisterInterfaces()
|
||||
{
|
||||
var loadedInterfaces = _loadedTypes.Where(t => t.IsInterface).ToList();
|
||||
var implementedInterfaces = _loadedTypes.SelectMany(t => t.GetInterfaces()).Where(i => !i.Assembly.FullName.StartsWith("System")).ToList();
|
||||
|
||||
var contracts = loadedInterfaces.Union(implementedInterfaces).Where(c => !c.IsGenericTypeDefinition && !string.IsNullOrWhiteSpace(c.FullName))
|
||||
.Except(new List<Type> { typeof(IMessage), typeof(ICommand), 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)
|
||||
{
|
||||
if (contractType.Name.Contains("oots"))
|
||||
{
|
||||
int adawd = 12;
|
||||
}
|
||||
|
||||
var implementations = GetImplementations(contractType).Where(c => !c.IsGenericTypeDefinition).ToList();
|
||||
|
||||
|
||||
|
||||
if (implementations.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (implementations.Count == 1)
|
||||
{
|
||||
var impl = implementations.Single();
|
||||
|
||||
if (impl.HasAttribute<SingletonAttribute>())
|
||||
{
|
||||
Container.RegisterSingleton(contractType, impl);
|
||||
}
|
||||
else
|
||||
{
|
||||
Container.Register(contractType, impl);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Container.RegisterAll(contractType, implementations);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<Type> GetImplementations(Type contractType)
|
||||
{
|
||||
return _loadedTypes
|
||||
.Where(implementation =>
|
||||
contractType.IsAssignableFrom(implementation) &&
|
||||
!implementation.IsInterface &&
|
||||
!implementation.IsAbstract
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user