From eb2da81b9d0ce0c28ea7c274204e9c25c2b6a5bf Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sun, 30 Sep 2018 18:39:07 +0100 Subject: [PATCH] Deprecate BindWeak Weak event subscriptions are basically impossible to get right, if you allow anonymous delegates. Fixes #38 --- Stylet/PropertyChangedExtensions.cs | 3 +- .../PropertyChangedExtensionsTests.cs | 97 ------------------- 2 files changed, 2 insertions(+), 98 deletions(-) diff --git a/Stylet/PropertyChangedExtensions.cs b/Stylet/PropertyChangedExtensions.cs index 3ba3caa..4a09487 100644 --- a/Stylet/PropertyChangedExtensions.cs +++ b/Stylet/PropertyChangedExtensions.cs @@ -155,7 +155,7 @@ namespace Stylet } /// - /// Weakly bind to PropertyChanged events for a particular property on a particular object + /// Obsolete: Weakly bind to PropertyChanged events for a particular property on a particular object /// /// someObject.Bind(x => x.PropertyNameToBindTo, newValue => /* do something with the new value */) /// Type of object providing the PropertyChanged event @@ -164,6 +164,7 @@ namespace Stylet /// MemberExpression selecting the property to observe for changes (e.g x => x.PropertyName) /// Handler called whenever that property changed /// Something which can be used to undo the binding. You can discard it if you want + [Obsolete("Don't use this - use Bind instead and explicitly .Unbind it when appropriate.", error: true)] public static IEventBinding BindWeak(this TSource target, Expression> targetSelector, EventHandler> handler) where TSource : class, INotifyPropertyChanged { var attribute = handler.Target.GetType().GetCustomAttribute(); diff --git a/StyletUnitTests/PropertyChangedExtensionsTests.cs b/StyletUnitTests/PropertyChangedExtensionsTests.cs index 4e7a9d8..dfdf0d1 100644 --- a/StyletUnitTests/PropertyChangedExtensionsTests.cs +++ b/StyletUnitTests/PropertyChangedExtensionsTests.cs @@ -38,11 +38,6 @@ namespace StyletUnitTests // Must make sure the compiler doesn't generate an inner class for this, otherwise we're not testing the right thing return notifying.Bind(x => x.Foo, (o, e) => this.LastFoo = e.NewValue); } - - public IEventBinding BindWeak(NotifyingClass notifying) - { - return notifying.BindWeak(x => x.Foo, (o, e) => this.LastFoo = e.NewValue); - } } private string newVal; @@ -125,97 +120,5 @@ namespace StyletUnitTests Assert.AreEqual(null, newVal); } - - [Test] - public void WeakBindingBinds() - { - var c1 = new NotifyingClass(); - c1.BindWeak(x => x.Foo, (o, e) => this.newVal = e.NewValue); - c1.Foo = "bar"; - - Assert.AreEqual("bar", this.newVal); - } - - [Test] - public void WeakBindingIgnoresOtherProperties() - { - var c1 = new NotifyingClass(); - c1.BindWeak(x => x.Bar, (o, e) => this.newVal = e.NewValue); - c1.Foo = "bar"; - - Assert.IsNull(this.newVal); - } - - [Test] - public void WeakBindingListensToEmptyString() - { - var c1 = new NotifyingClass(); - c1.Bar = "bar"; - c1.BindWeak(x => x.Bar, (o, e) => this.newVal = e.NewValue); - c1.NotifyAll(); - - Assert.AreEqual("bar", this.newVal); - } - - [Test] - public void WeakBindingDoesNotRetainBindingClass() - { - var binding = new BindingClass(); - - // Means of determining whether the class has been disposed - var weakBinding = new WeakReference(binding); - - var notifying = new NotifyingClass(); - binding.BindWeak(notifying); - - - - binding = null; - GC.Collect(); - Assert.IsFalse(weakBinding.TryGetTarget(out binding)); - } - - [Test] - public void WeakBindingDoesNotRetainNotifier() - { - var binding = new BindingClass(); - var notifying = new NotifyingClass(); - // Means of determining whether the class has been disposed - var weakNotifying = new WeakReference(notifying); - // Retain binder, as that shouldn't affect anything - var binder = binding.BindWeak(notifying); - - notifying = null; - GC.Collect(); - Assert.IsFalse(weakNotifying.TryGetTarget(out notifying)); - } - - [Test] - public void WeakBindingUnbinds() - { - var c1 = new NotifyingClass(); - var binding = c1.BindWeak(x => x.Bar, (o, e) => this.newVal = e.NewValue); - binding.Unbind(); - c1.Bar = "bar"; - - Assert.IsNull(this.newVal); - } - - [Test] - public void BindWeakPassesSender() - { - var c1 = new NotifyingClass(); - c1.BindWeak(x => x.Foo, (o, e) => this.sender = o); - c1.Foo = "foo"; - Assert.AreEqual(c1, this.sender); - } - - [Test] - public void BindWeakThrowsIfTargetIsCompilerGenerated() - { - var c1 = new NotifyingClass(); - string newVal = null; - Assert.Throws(() => c1.BindWeak(x => x.Foo, (o, e) => newVal = e.NewValue)); - } } }