Updated Container to handle Singleton Implementations instead of Singleton Interfaces.

pull/2/head
Taloth Saldono 10 years ago
parent d0bf539a73
commit cf9391a7a3

@ -14,6 +14,7 @@ using FluentAssertions;
using System.Linq;
using NzbDrone.Common.Composition;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Download.TrackedDownloads;
namespace NzbDrone.App.Test
{
@ -64,7 +65,6 @@ namespace NzbDrone.App.Test
}
[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 @@ namespace NzbDrone.App.Test
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 @@ namespace NzbDrone.Common.Composition
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 @@ namespace NzbDrone.Common.Composition
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…
Cancel
Save