mirror of
https://github.com/lidarr/Lidarr
synced 2025-01-30 19:02:06 +00:00
Updated Container to handle Singleton Implementations instead of Singleton Interfaces.
This commit is contained in:
parent
d0bf539a73
commit
cf9391a7a3
2 changed files with 38 additions and 4 deletions
|
@ -14,6 +14,7 @@
|
|||
using System.Linq;
|
||||
using NzbDrone.Common.Composition;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Download.TrackedDownloads;
|
||||
|
||||
namespace NzbDrone.App.Test
|
||||
{
|
||||
|
@ -64,7 +65,6 @@ public void should_resolve_command_executor_by_name()
|
|||
}
|
||||
|
||||
[Test]
|
||||
[Ignore("need to fix this at some point")]
|
||||
public void should_return_same_instance_of_singletons()
|
||||
{
|
||||
var first = _container.ResolveAll<IHandle<ApplicationShutdownRequested>>().OfType<Scheduler>().Single();
|
||||
|
@ -72,5 +72,23 @@ public void should_return_same_instance_of_singletons()
|
|||
|
||||
first.Should().BeSameAs(second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_return_same_instance_of_singletons_by_different_same_interface()
|
||||
{
|
||||
var first = _container.ResolveAll<IHandle<EpisodeGrabbedEvent>>().OfType<DownloadMonitoringService>().Single();
|
||||
var second = _container.ResolveAll<IHandle<EpisodeGrabbedEvent>>().OfType<DownloadMonitoringService>().Single();
|
||||
|
||||
first.Should().BeSameAs(second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_return_same_instance_of_singletons_by_different_interfaces()
|
||||
{
|
||||
var first = _container.ResolveAll<IHandle<EpisodeGrabbedEvent>>().OfType<DownloadMonitoringService>().Single();
|
||||
var second = (DownloadMonitoringService)_container.Resolve<IExecute<CheckForFinishedDownloadCommand>>();
|
||||
|
||||
first.Should().BeSameAs(second);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -51,7 +51,9 @@ public void Register<TService>(Func<IContainer, TService> factory) where TServic
|
|||
|
||||
public void RegisterSingleton(Type service, Type implementation)
|
||||
{
|
||||
_container.Register(service, implementation).AsSingleton();
|
||||
var factory = CreateSingletonImplementationFactory(implementation);
|
||||
|
||||
_container.Register(service, factory);
|
||||
}
|
||||
|
||||
public IEnumerable<T> ResolveAll<T>() where T : class
|
||||
|
@ -59,9 +61,23 @@ public IEnumerable<T> ResolveAll<T>() where T : class
|
|||
return _container.ResolveAll<T>();
|
||||
}
|
||||
|
||||
public void RegisterAllAsSingleton(Type registrationType, IEnumerable<Type> implementationList)
|
||||
public void RegisterAllAsSingleton(Type service, IEnumerable<Type> implementationList)
|
||||
{
|
||||
_container.RegisterMultiple(registrationType, implementationList).AsSingleton();
|
||||
foreach (var implementation in implementationList)
|
||||
{
|
||||
var factory = CreateSingletonImplementationFactory(implementation);
|
||||
|
||||
_container.Register(service, factory, implementation.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
private Func<TinyIoCContainer, NamedParameterOverloads, object> CreateSingletonImplementationFactory(Type implementation)
|
||||
{
|
||||
const string singleImplPrefix = "singleImpl_";
|
||||
|
||||
_container.Register(implementation, implementation, singleImplPrefix + implementation.FullName).AsSingleton();
|
||||
|
||||
return (c, p) => _container.Resolve(implementation, singleImplPrefix + implementation.FullName);
|
||||
}
|
||||
|
||||
public bool IsTypeRegistered(Type type)
|
||||
|
|
Loading…
Reference in a new issue