mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-21 14:03:29 -07:00
Hello Autofac, Goodbye Ninject
This commit is contained in:
parent
17d9d0cc4f
commit
924d3d0c8e
139 changed files with 473 additions and 572 deletions
94
NzbDrone.Core/AutofacSignalrDependencyResolver.cs
Normal file
94
NzbDrone.Core/AutofacSignalrDependencyResolver.cs
Normal file
|
@ -0,0 +1,94 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Autofac;
|
||||
using Autofac.Builder;
|
||||
using Autofac.Core;
|
||||
using SignalR;
|
||||
|
||||
namespace NzbDrone.Core
|
||||
{
|
||||
public class AutofacSignalrDependencyResolver : DefaultDependencyResolver, IDependencyResolver, IRegistrationSource
|
||||
{
|
||||
private ILifetimeScope LifetimeScope { get; set; }
|
||||
|
||||
public AutofacSignalrDependencyResolver(ILifetimeScope lifetimeScope)
|
||||
{
|
||||
LifetimeScope = lifetimeScope;
|
||||
var currentRegistrationSource =
|
||||
LifetimeScope.ComponentRegistry.Sources.FirstOrDefault(s => s.GetType() == GetType());
|
||||
if (currentRegistrationSource != null)
|
||||
{
|
||||
((AutofacSignalrDependencyResolver)currentRegistrationSource).LifetimeScope = lifetimeScope;
|
||||
}
|
||||
else
|
||||
{
|
||||
LifetimeScope.ComponentRegistry.AddRegistrationSource(this);
|
||||
}
|
||||
}
|
||||
|
||||
public AutofacSignalrDependencyResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public override object GetService(Type serviceType)
|
||||
{
|
||||
object result;
|
||||
|
||||
if (LifetimeScope == null)
|
||||
{
|
||||
return base.GetService(serviceType);
|
||||
}
|
||||
|
||||
if (LifetimeScope.TryResolve(serviceType, out result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public override IEnumerable<object> GetServices(Type serviceType)
|
||||
{
|
||||
object result;
|
||||
|
||||
if (LifetimeScope == null)
|
||||
{
|
||||
return base.GetServices(serviceType);
|
||||
}
|
||||
|
||||
if (LifetimeScope.TryResolve(typeof(IEnumerable<>).MakeGenericType(serviceType), out result))
|
||||
{
|
||||
return (IEnumerable<object>)result;
|
||||
}
|
||||
|
||||
return Enumerable.Empty<object>();
|
||||
}
|
||||
|
||||
public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
|
||||
{
|
||||
var typedService = service as TypedService;
|
||||
if (typedService != null)
|
||||
{
|
||||
var instances = base.GetServices(typedService.ServiceType);
|
||||
|
||||
if (instances != null)
|
||||
{
|
||||
return instances
|
||||
.Select(i => RegistrationBuilder.ForDelegate(i.GetType(), (c, p) => i).As(typedService.ServiceType)
|
||||
.InstancePerLifetimeScope()
|
||||
.PreserveExistingDefaults()
|
||||
.CreateRegistration());
|
||||
}
|
||||
}
|
||||
|
||||
return Enumerable.Empty<IComponentRegistration>();
|
||||
}
|
||||
|
||||
bool IRegistrationSource.IsAdapterForIndividualComponents
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue