Deprecate BindWeak

Weak event subscriptions are basically impossible to get right, if
you allow anonymous delegates.

Fixes #38
This commit is contained in:
Antony Male 2018-09-30 18:39:07 +01:00
parent d32045b518
commit eb2da81b9d
2 changed files with 2 additions and 98 deletions

View File

@ -155,7 +155,7 @@ namespace Stylet
}
/// <summary>
/// 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
/// </summary>
/// <example>someObject.Bind(x => x.PropertyNameToBindTo, newValue => /* do something with the new value */)</example>
/// <typeparam name="TSource">Type of object providing the PropertyChanged event</typeparam>
@ -164,6 +164,7 @@ namespace Stylet
/// <param name="targetSelector">MemberExpression selecting the property to observe for changes (e.g x => x.PropertyName)</param>
/// <param name="handler">Handler called whenever that property changed</param>
/// <returns>Something which can be used to undo the binding. You can discard it if you want</returns>
[Obsolete("Don't use this - use Bind instead and explicitly .Unbind it when appropriate.", error: true)]
public static IEventBinding BindWeak<TSource, TProperty>(this TSource target, Expression<Func<TSource, TProperty>> targetSelector, EventHandler<PropertyChangedExtendedEventArgs<TProperty>> handler) where TSource : class, INotifyPropertyChanged
{
var attribute = handler.Target.GetType().GetCustomAttribute<CompilerGeneratedAttribute>();

View File

@ -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<BindingClass>(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<NotifyingClass>(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<InvalidOperationException>(() => c1.BindWeak(x => x.Foo, (o, e) => newVal = e.NewValue));
}
}
}