diff --git a/Stylet/Bootstrapper.cs b/Stylet/Bootstrapper.cs index b37c2b7..e623ec5 100644 --- a/Stylet/Bootstrapper.cs +++ b/Stylet/Bootstrapper.cs @@ -25,7 +25,9 @@ namespace Stylet protected virtual void ConfigureIoC(IStyletIoCBuilder builder) { builder.Autobind(AssemblySource.Assemblies); + builder.Bind().To().InSingletonScope(); + builder.Bind().To().InSingletonScope(); builder.Bind().To().InSingletonScope(); } diff --git a/StyletUnitTests/EventAggregatorTests.cs b/StyletUnitTests/EventAggregatorTests.cs index ab34545..4b0d4ff 100644 --- a/StyletUnitTests/EventAggregatorTests.cs +++ b/StyletUnitTests/EventAggregatorTests.cs @@ -28,6 +28,11 @@ namespace StyletUnitTests public void Handle(M1 message) { this.ReceivedM1 = message; } } + public class C3 : IHandle + { + public void Handle(M1 message) { throw new Exception("Should not be called. Ever"); } + } + [Test] public void SubscribesAndDeliversExactMessage() { @@ -54,5 +59,33 @@ namespace StyletUnitTests Assert.AreEqual(message, target.ReceivedM1); Assert.AreEqual(message, target.ReceivedM2); } + + [Test] + public void UnsubscribeUnsubscribes() + { + var ea = new EventAggregator(); + var target = new C1(); + ea.Subscribe(target); + ea.Unsubscribe(target); + + var message = new M1(); + ea.Publish(message); + + Assert.IsNull(target.ReceivedMessage); + } + + [Test] + public void TargetReferenceIsWeak() + { + var ea = new EventAggregator(); + var target = new C3(); + ea.Subscribe(target); + + // Ugly, but it's the only way to test a WeakReference... + target = null; + GC.Collect(); + + Assert.DoesNotThrow(() => ea.Publish(new M1())); + } } }