diff --git a/src/NzbDrone.App.Test/ContainerFixture.cs b/src/NzbDrone.App.Test/ContainerFixture.cs index 493ffafef..1064d1c5b 100644 --- a/src/NzbDrone.App.Test/ContainerFixture.cs +++ b/src/NzbDrone.App.Test/ContainerFixture.cs @@ -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>().OfType().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>().OfType().Single(); + var second = _container.ResolveAll>().OfType().Single(); + + first.Should().BeSameAs(second); + } + + [Test] + public void should_return_same_instance_of_singletons_by_different_interfaces() + { + var first = _container.ResolveAll>().OfType().Single(); + var second = (DownloadMonitoringService)_container.Resolve>(); + + first.Should().BeSameAs(second); + } } } \ No newline at end of file diff --git a/src/NzbDrone.Common/Composition/Container.cs b/src/NzbDrone.Common/Composition/Container.cs index 9e80b75c5..a52ad0dc7 100644 --- a/src/NzbDrone.Common/Composition/Container.cs +++ b/src/NzbDrone.Common/Composition/Container.cs @@ -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 ResolveAll() where T : class @@ -59,9 +61,23 @@ namespace NzbDrone.Common.Composition return _container.ResolveAll(); } - public void RegisterAllAsSingleton(Type registrationType, IEnumerable implementationList) + public void RegisterAllAsSingleton(Type service, IEnumerable implementationList) { - _container.RegisterMultiple(registrationType, implementationList).AsSingleton(); + foreach (var implementation in implementationList) + { + var factory = CreateSingletonImplementationFactory(implementation); + + _container.Register(service, factory, implementation.FullName); + } + } + + private Func 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)