mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-21 05:53:33 -07:00
back to tiny for now
This commit is contained in:
parent
2912561d0e
commit
4deecde092
84 changed files with 617 additions and 558 deletions
99
NzbDrone.Common/Composition/Container.cs
Normal file
99
NzbDrone.Common/Composition/Container.cs
Normal file
|
@ -0,0 +1,99 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TinyIoC;
|
||||
|
||||
namespace NzbDrone.Common.Composition
|
||||
{
|
||||
public class Container : IContainer
|
||||
{
|
||||
private readonly TinyIoCContainer _container;
|
||||
|
||||
public Container(TinyIoCContainer container)
|
||||
{
|
||||
_container = container;
|
||||
//_container.Options.AllowOverridingRegistrations = true;
|
||||
|
||||
|
||||
//_container.RegisterSingle(LogManager.GetCurrentClassLogger());
|
||||
|
||||
_container.Register<IContainer>(this);
|
||||
//container.RegisterWithContext(dependencyContext => LogManager.GetLogger(dependencyContext.ImplementationType.Name));
|
||||
}
|
||||
|
||||
public void Register<TService, TImplementation>()
|
||||
where TImplementation : class, TService
|
||||
where TService : class
|
||||
{
|
||||
_container.Register<TService, TImplementation>();
|
||||
}
|
||||
|
||||
public TinyIoCContainer TinyContainer { get { return _container; } }
|
||||
|
||||
public void Register<T>(T instance) where T : class
|
||||
{
|
||||
_container.Register<T>(instance);
|
||||
}
|
||||
|
||||
public T Resolve<T>() where T : class
|
||||
{
|
||||
return _container.Resolve<T>();
|
||||
}
|
||||
|
||||
public object Resolve(Type type)
|
||||
{
|
||||
return _container.Resolve(type);
|
||||
}
|
||||
|
||||
public void Register(Type serviceType, Type implementationType)
|
||||
{
|
||||
_container.Register(serviceType, implementationType);
|
||||
}
|
||||
|
||||
public void Register<TService>(Func<IContainer, TService> factory) where TService : class
|
||||
{
|
||||
_container.Register((c, n) => factory(this));
|
||||
}
|
||||
|
||||
public void RegisterSingleton<TService, TImplementation>()
|
||||
where TImplementation : class, TService
|
||||
where TService : class
|
||||
{
|
||||
_container.Register<TService, TImplementation>().AsSingleton();
|
||||
}
|
||||
|
||||
public void RegisterSingleton<T>() where T : class
|
||||
{
|
||||
_container.Register<T, T>().AsSingleton();
|
||||
}
|
||||
|
||||
public void RegisterSingleton(Type service, Type implementation)
|
||||
{
|
||||
_container.Register(service, implementation).AsSingleton();
|
||||
}
|
||||
|
||||
public IEnumerable<T> ResolveAll<T>() where T : class
|
||||
{
|
||||
return _container.ResolveAll<T>();
|
||||
}
|
||||
|
||||
public IEnumerable<object> ResolveAll(Type type)
|
||||
{
|
||||
return _container.ResolveAll(type);
|
||||
}
|
||||
|
||||
public void Register(Type registrationType, object instance)
|
||||
{
|
||||
_container.Register(registrationType, instance);
|
||||
}
|
||||
|
||||
public void RegisterAll(Type registrationType, IEnumerable<Type> implementationList)
|
||||
{
|
||||
_container.RegisterMultiple(registrationType, implementationList);
|
||||
}
|
||||
|
||||
public bool IsTypeRegistered(Type type)
|
||||
{
|
||||
return _container.CanResolve(type);
|
||||
}
|
||||
}
|
||||
}
|
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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
30
NzbDrone.Common/Composition/IContainer.cs
Normal file
30
NzbDrone.Common/Composition/IContainer.cs
Normal file
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TinyIoC;
|
||||
|
||||
namespace NzbDrone.Common.Composition
|
||||
{
|
||||
public interface IContainer
|
||||
{
|
||||
TinyIoCContainer TinyContainer { get; }
|
||||
void Register<T>(T instance) where T : class;
|
||||
|
||||
void Register<TService, TImplementation>()
|
||||
where TImplementation : class, TService
|
||||
where TService : class;
|
||||
T Resolve<T>() where T : class;
|
||||
object Resolve(Type type);
|
||||
void Register(Type serviceType, Type implementationType);
|
||||
void Register<TService>(Func<IContainer, TService> factory) where TService : class;
|
||||
void RegisterSingleton<TService, TImplementation>()
|
||||
where TImplementation : class, TService
|
||||
where TService : class;
|
||||
void RegisterSingleton<T>() where T : class;
|
||||
void RegisterSingleton(Type service, Type implementation);
|
||||
IEnumerable<T> ResolveAll<T>() where T : class;
|
||||
IEnumerable<object> ResolveAll(Type type);
|
||||
void Register(Type registrationType, object instance);
|
||||
void RegisterAll(Type registrationType, IEnumerable<Type> implementationList);
|
||||
bool IsTypeRegistered(Type type);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue