WPF TwoWay Binding Custom with Data Provider - c#

I am trying to use twoway binding to a custom datasource (other than a db or xml where twoway binding is supported out of the box) in WPF to update the UI automatically when the source is updated and update the source when the object is changed in the UI.
What I currently have is a class, CacheObject, which implements INotifyPropertyChanged and a custom (derived) ObservableCollection, CacheWrapper, which subscribes to changes in the cache and updates the appropriate CacheObject which in turn automatically updates the UI.
Is there a smart way that I can build into my CacheWrapper a way to update the cache - make a call using the cache API - when a CacheObject is updated in the list. If I subscribe to the PropertyChanged events I will receive events both when the objects are updated from code (changes from cache) and from the UI. Basically I want to do one thing if the update comes from the source and another if it comes from the target.
Some code examples.
CacheObject
public class CacheObject : INotifyPropertyChanged
{
private object _key;
private object _value;
public event PropertyChangedEventHandler PropertyChanged;
public CacheObjectViewModel(object key, object value)
{
_key = key;
_value = value;
}
public CacheObjectViewModel(KeyValuePair<object, object> keyValuePair)
: this(keyValuePair.Key, keyValuePair.Value)
{
}
public object Key
{
get { return _key; }
set
{
_key = value;
OnPropertyChanged("Key");
}
}
public object Value
{
get { return _value; }
set
{
_value = value;
OnPropertyChanged("Value");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
CacheWrapper
public class CacheWrapper : ObservableCollection<CacheObject>, ICacheObserver
{
private ICache _cache;
public String Name { get; private set; }
public CacheWrapepr(string name, ICache cache)
{
_cache = cache;
Name = name;
cache.AddListener(this);
}
public void CacheEntryInserted(CacheEventArgs eventArgs)
{
CacheObject cacheObject = eventArgs.NewElement as CacheObject;
if(cacheObject != null)
{
this.Add(cacheObject);
}
}
public void EntryUpdated(CacheEventArgs eventArgs)
{
CacheObject cacheObjectNew = eventArgs.NewElement as CacheObject;
if(cacheObjectNew != null)
{
CacheObject cacheObjectExisting = this.FirstOrDefault(x => x.Key == cacheObjectNew.Key);
if(cacheObjectExisting != null)
{
cacheObjectExisting.Value = cacheObjectNew.Value;
}
}
}
public void EntryDeleted(CacheEventArgs eventArgs)
{
CacheObject cacheObject= = eventArgs.OldElement as CacheObject;
if(cacheObject != null)
{
CacheObject cacheObjectExisting = this.FirstOrDefault(x => x.Key == cacheObject.Key);
if(cacheObjectExisting != null)
{
this.Remove(cacheObjectExisting);
}
}
}
}
Help and ideas are much appreciated.

Related

INotifyPropertyChanged and property on BaseClass C#

I am working with WPF and MVVM model. I have a base viewmodel class called ViewModelBase. It has a property on it called Config that is a complex type. I need a derived class to be able to databind to the base class Config property in a View.
public class ViewModelBase : INotifyPropertyChanged
{
private Configuration _config;
public event PropertyChangedEventHandler PropertyChanged;
public Configuration Config
{
get { return _config; }
set
{
if(_config == null || !_config.Equals(value))
{
_config = value;
OnPropertyChanged(new PropertyChangedEventArgs("Config"));
}
}
}
public ViewModelBase()
{
}
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if(PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
}
Databinding seems to be working in a read capacity, but when a property of the Config is altered in the OptionsView, the changes are not reflected in the Config itself. Any suggestions?
Configuration implementation, per request.
public class Configuration : IEquatable<Configuration>, INotifyPropertyChanged
{
private string _primaryUrl;
private string _secondaryUrl;
private DateTime _scheduledStart;
private DateTime _scheduledEnd;
private string _buffer;
private bool _isScheduleEnabled;
private int _logDays;
private int _retryDuration;
private int _maxRetryAttempts;
private string _registrationKey;
private string _email;
public string PrimaryURL
{
get { return _primaryUrl; }
set
{
if(_primaryUrl != value)
{
_primaryUrl = value;
OnPropertyChanged(new PropertyChangedEventArgs("PrimaryURL"));
}
}
}
public string SecondaryURL
{
get { return _secondaryUrl; }
set
{
if(_secondaryUrl != value)
{
_secondaryUrl = value;
OnPropertyChanged(new PropertyChangedEventArgs("SecondaryURL"));
}
}
}
public DateTime ScheduledStart
{
get { return _scheduledStart; }
set
{
if(_scheduledStart != value)
{
_scheduledStart = value;
OnPropertyChanged(new PropertyChangedEventArgs("ScheduledStart"));
}
}
}
public DateTime ScheduledEnd
{
get { return _scheduledEnd; }
set
{
if(_scheduledEnd != value)
{
_scheduledEnd = value;
OnPropertyChanged(new PropertyChangedEventArgs("ScheduledEnd"));
}
}
}
public string Buffer
{
get { return _buffer; }
set
{
if(_buffer != value)
{
_buffer = value;
OnPropertyChanged(new PropertyChangedEventArgs("Buffer"));
}
}
}
public bool IsScheduleEnabled
{
get { return _isScheduleEnabled; }
set
{
if(_isScheduleEnabled != value)
{
_isScheduleEnabled = value;
OnPropertyChanged(new PropertyChangedEventArgs("IsScheduleEnabled"));
}
}
}
public int LogDays
{
get { return _logDays; }
set
{
if(_logDays != value)
{
_logDays = value;
OnPropertyChanged(new PropertyChangedEventArgs("LogDays"));
}
}
}
public int RetryDuration
{
get { return _retryDuration; }
set
{
if(_retryDuration != value)
{
_retryDuration = value;
OnPropertyChanged(new PropertyChangedEventArgs("RetryDuration"));
}
}
}
public int MaxRetryAttempts
{
get { return _maxRetryAttempts; }
set
{
if(_maxRetryAttempts != value)
{
_maxRetryAttempts = value;
OnPropertyChanged(new PropertyChangedEventArgs("MaxRetryAttempts"));
}
}
}
public string RegistrationKey
{
get { return _registrationKey; }
set
{
if(_registrationKey != value)
{
_registrationKey = value;
OnPropertyChanged(new PropertyChangedEventArgs("RegistrationKey"));
}
}
}
public string Email
{
get { return _email; }
set
{
if(_email != value)
{
_email = value;
OnPropertyChanged(new PropertyChangedEventArgs("Email"));
}
}
}
public Configuration() { }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if(PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
}
Here is one of the culprit bindings:
<xctk:DateTimePicker Grid.Column="1" Value="{Binding Config.ScheduledStart}" Height="20" VerticalAlignment="Top"/>
The INotifyPropertyChanged implementation only applies to the class directly. So in your case, to the ViewModelBase class and its subtypes.
In this case, the PropertyChangedEvent is raised in the setter of the Config property, so whenever the Config property is set (and the setter is called), the event is raised.
This however does not mean that when mutating the Config object that the even is also raised. In general, this is not the case.
In order to raise the event when the Config object is changed, you would have to reassign the object to the view model (calling the setter again). This however does not work when data binding to the object.
A better solution is to make the Configuration implement INotifyPropertyChanged interface itself. So when a property within that object is changed, an event is raised as well. WPF will also recognize this for subobjects, so it will automatically work.
Databinding seems to be working in a read capacity..
Which is fine but if you want a change capacity, then the class Configuration will have to adhere to INotifyPropertyChanged and each property on the class needs to report PropertyChange notifications for any changes to be shown in bound xaml controls.
but when a property of the Config is altered in the OptionsView, the changes are not reflected
What you have now only notifies if the instance of Configuration has been replaced; not individual property changes within the current instance.

Xamarin forms, working with a OnPropertyChanged with a INT value

I have one button on my MasterDetailPage changing the value on an INT (named App.value1) depending on what you click looking like this:
void click1 (object s, EventArgs a)
{
if (App.value1 == 0) {
App.value1 = App.value1 + 1;
} else {
App.value1 = 0;
}
}
And I want this click function to immediately change the value on my StartPage (another ContentPage). So I have created a viewmodel looking like this, where I try to work with the current value:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null) {
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
public int currentValue {
get {
return App.value1;
}
set {
if (App.value1 == 0) {
App.value1 = 0;
} else {
App.value1 = 1;
}
}
}
And this is the StartPage where I want the value of the INT to update immediately depending on what you clicked on at the MasterDetailView.
public StartPage ()
{
var ourView = new StartPageViewModel ();
ourCurrentValue = ourView.currentValue;
}
protected async override void OnAppearing()
{
LoadData();
}
private async Task<List<Pin>> LoadData() //I work with pins here (not showing that code though as it is irrelavant.
{
if (ourCurrentValue == 0) {
System.Diagnostics.Debug.WriteLine ("Value is 0");
}
else {
System.Diagnostics.Debug.WriteLine ("Value is 1");
}
}
Right now I only see "Value is 0" in my log. Nothing updates when I click on my button on the MasterDetailPage.
UPDATED CODE:
public class StartPageViewModel : INotifyPropertyChanged
{
private ICommand clickCommand;
private int currentValue;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
public StartPageViewModel()
{
ClickCommand = new Command(() => CurrentValue = CurrentValue + 1);
}
public ICommand ClickCommand
{
get { return clickCommand; }
set
{
clickCommand = value;
OnPropertyChanged("ClickCommand");
}
}
public int CurrentValue
{
get { return currentValue; }
set
{
currentValue = value;
OnPropertyChanged("CurrentValue");
}
}
}
And StartPage:
public StartPage ()
{
App.PropertyChanged += (sender, args) => OnPropertyChanged("currentValue"); // ERROR: `An object reference is requiered to access non-static member 'Xamarin.Forms.BindableObject.PropertyChanged`
}
You can proceed with something like that:
Make following changes to your App class and value1 property inside that class:
public static event PropertyChangedEventHandler PropertyChanged;
private static void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged (null, new PropertyChangedEventArgs (propertyName));
}
}
private static int _value1;
public static int value1
{
get { return _value1; }
set
{
_value1 = value;
OnPropertyChanged("value1");
}
}
Then add this line to your StartPageViewModel constructor:
App.PropertyChanged += (sender, args) => OnPropertyChanged("currentValue");
In that code you are just leveraging PropertyChanged for your own purposes (you can even create your own event for that).
I mean StartPageViewModel subscribes to PropertyChanged event in Appclass, so it will be notified when value1 change. And when it actually occurs, then it is invoking his own PropertyChanged to notify View about currentValue change.
However, I would say better solution is to share View Model between MasterDetailPage and StartPage, because using global state makes your solution hard to understand :
public class SharedViewModel : INotifyPropertyChanged
{
private ICommand clickCommand;
private int currentValue;
/* INotifyPropertyChanged implementation */
public SharedViewModel()
{
ClickCommand = new Command(() => CurrentValue = CurrentValue + 1);
}
public ICommand ClickCommand
{
get { return clickCommand; }
set
{
clickCommand = value;
OnPropertyChanged("ClickCommand");
}
}
public int CurrentValue
{
get { return currentValue; }
set
{
currentValue = value;
OnPropertyChanged("CurrentValue");
}
}
}
And you need to use the same instance of SharedViewModel in MasterDetailPage as well as StartPage

Force INotifyDataErrorInfo validation

I have implemented INotifyDataErrorInfo exactly as described in the following link:
http://blog.micic.ch/net/easy-mvvm-example-with-inotifypropertychanged-and-inotifydataerrorinfo
I have a TextBox which is bound to a string property in my model.
XAML
<TextBox Text="{Binding FullName,
ValidatesOnNotifyDataErrors=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}" />
Model
private string _fullName;
public string FullName
{
get { return _fullName; }
set
{
// Set raises OnPropertyChanged
Set(ref _fullName, value);
if (string.IsNullOrWhiteSpace(_fullName))
AddError(nameof(FullName), "Name required");
else
RemoveError(nameof(FullName));
}
}
INotifyDataError Code
private Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>();
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
// get errors by property
public IEnumerable GetErrors(string propertyName)
{
if (_errors.ContainsKey(propertyName))
return _errors[propertyName];
return null;
}
public bool HasErrors => _errors.Count > 0;
// object is valid
public bool IsValid => !HasErrors;
public void AddError(string propertyName, string error)
{
// Add error to list
_errors[propertyName] = new List<string>() { error };
NotifyErrorsChanged(propertyName);
}
public void RemoveError(string propertyName)
{
// remove error
if (_errors.ContainsKey(propertyName))
_errors.Remove(propertyName);
NotifyErrorsChanged(propertyName);
}
public void NotifyErrorsChanged(string propertyName)
{
// Notify
if (ErrorsChanged != null)
ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
Now all this works fine, but it only validates as soon as I type something in my TextBox. I would like some way to validate on demand, without even touching the textbox, say on a button click.
I have tried raising PropertyChanged for all my properties as described in this question, but it does not detect the errors. I somehow need my property setter to be called so the errors can be detected. I'm looking for a MVVM solution.
The INotifyDataErrorInfo implementation you use is somewhat flawed IMHO. It relies on errors kept in a state (a list) attached to the object. Problem with stored state is, sometimes, in a moving world, you don't have the chance to update it when you want. Here is another MVVM implementation that doesn't rely on a stored state, but computes error state on the fly.
Things are handled a bit differently as you need to put validation code in a central GetErrors method (you could create per-property validation methods called from this central method), not in the property setters.
public class ModelBase : INotifyPropertyChanged, INotifyDataErrorInfo
{
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public bool HasErrors
{
get
{
return GetErrors(null).OfType<object>().Any();
}
}
public virtual void ForceValidation()
{
OnPropertyChanged(null);
}
public virtual IEnumerable GetErrors([CallerMemberName] string propertyName = null)
{
return Enumerable.Empty<object>();
}
protected void OnErrorsChanged([CallerMemberName] string propertyName = null)
{
OnErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
protected virtual void OnErrorsChanged(object sender, DataErrorsChangedEventArgs e)
{
var handler = ErrorsChanged;
if (handler != null)
{
handler(sender, e);
}
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(sender, e);
}
}
}
And here are two sample classes that demonstrate how to use it:
public class Customer : ModelBase
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged();
}
}
}
public override IEnumerable GetErrors([CallerMemberName] string propertyName = null)
{
if (string.IsNullOrEmpty(propertyName) || propertyName == nameof(Name))
{
if (string.IsNullOrWhiteSpace(_name))
yield return "Name cannot be empty.";
}
}
}
public class CustomerWithAge : Customer
{
private int _age;
public int Age
{
get
{
return _age;
}
set
{
if (_age != value)
{
_age = value;
OnPropertyChanged();
}
}
}
public override IEnumerable GetErrors([CallerMemberName] string propertyName = null)
{
foreach (var obj in base.GetErrors(propertyName))
{
yield return obj;
}
if (string.IsNullOrEmpty(propertyName) || propertyName == nameof(Age))
{
if (_age <= 0)
yield return "Age is invalid.";
}
}
}
It works like a charm with a simple XAML like this:
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Text="{Binding Age, UpdateSourceTrigger=PropertyChanged}" />
(UpdateSourceTrigger is optional, if you don't use it it will only work when focus is lost).
With this MVVM base class, you shouldn't have to force any validation. But should you need it, I have added a ForceValidation sample method in ModelBase that should work (I have tested it with for example a member value like _name that would have been changed without passing through the public setter).
Your best bet is to use a relay command interface. Take a look at this:
public class RelayCommand : ICommand
{
Action _TargetExecuteMethod;
Func<bool> _TargetCanExecuteMethod;
public RelayCommand(Action executeMethod)
{
_TargetExecuteMethod = executeMethod;
}
public RelayCommand(Action executeMethod, Func<bool> canExecuteMethod)
{
_TargetExecuteMethod = executeMethod;
_TargetCanExecuteMethod = canExecuteMethod;
}
public void RaiseCanExecuteChanged()
{
CanExecuteChanged(this, EventArgs.Empty);
}
#region ICommand Members
bool ICommand.CanExecute(object parameter)
{
if (_TargetCanExecuteMethod != null)
{
return _TargetCanExecuteMethod();
}
if (_TargetExecuteMethod != null)
{
return true;
}
return false;
}
public event EventHandler CanExecuteChanged = delegate { };
void ICommand.Execute(object parameter)
{
if (_TargetExecuteMethod != null)
{
_TargetExecuteMethod();
}
}
#endregion
}
You would declare this relay command in your view model like:
public RelayCommand SaveCommand { get; private set; }
Now, in addition to registering your SaveCommand with OnSave and a CanSave methods, since you extend from INotifyDataErrorInfo, you can sign up to ErrorsChanged in your constructor as well:
public YourViewModel()
{
SaveCommand = new RelayCommand(OnSave, CanSave);
ErrorsChanged += RaiseCanExecuteChanged;
}
And you'll need the methods:
private void RaiseCanExecuteChanged(object sender, EventArgs e)
{
SaveCommand.RaiseCanExecuteChanged();
}
public bool CanSave()
{
return !this.HasErrors;
}
private void OnSave()
{
//Your save logic here.
}
Also, each time after you call PropertyChanged, you can call this validation method:
private void ValidateProperty<T>(string propertyName, T value)
{
var results = new List<ValidationResult>();
ValidationContext context = new ValidationContext(this);
context.MemberName = propertyName;
Validator.TryValidateProperty(value, context, results);
if (results.Any())
{
_errors[propertyName] = results.Select(c => c.ErrorMessage).ToList();
}
else
{
_errors.Remove(propertyName);
}
ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
With this setup, and if your viewmodel both extends from INotifyPropertyChanged and INotifyDataErrorInfo (or from a base class that extends from these two), when you bind a button to the SaveCommand above, WPF framework will automatically disable it if there are validation errors.
Hope this helps.

Adding property changed event to a custom control

For an application I have to use a custom button that react when one of its properties value is being changed. I addded a field named Data to the new button:
public class ButtonData
{
public string Name;
public string Color;
//And more stuff...
}
Then I have the folowing code for new button, I want it to be able to update itself (change background color and some other stuff) whenever its Data property gets updated from somewhere in application. I found some ideas about implementing INotifyPropertyChanged interface and I set it up in my custom button like this:
public partial class ButtonPin : Button, INotifyPropertyChanged
{
private ButtonData _data;
public ButtonData Data
{
get { return _data; }
set
{
if (value == _data) return;
_data = value;
OnPropertyChanged("Data");
}
}
private bool _buttonDataAdded;
public ButtonPin()
{
InitializeComponent();
}
public ButtonPin(ButtonData data)
{
Data = data;
_buttonDataAdded = true;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Now I am not sure how to use this! For example, if the Color in Data objects gets changed
somehow somewhere and its get assigned to current button's Data field, I want this button to change its background color. Something like
var data = new ButtonData();
data.Name = "Hi!";
data.Color = Color.Red;
buttonPin1.Data = data; //Here I need the changes to occur
You have implemented the interface INotifyPropertyChanged on the ButtonPin class, not on the ButtonData class, and you want to detect a change on an object of type ButtonData, thus you need to implement the interface INotifyPropertyChanged on the ButtonData class.
To detect the change you need to hook up to the PropertyChanged event of the ButtonData object in the setter of the ButtonPin.Data property. Something like this.
private bool _data;
public ButtonData Data {
get { return _data; }
set {
if (value != _data) {
// Unhook previous events
if (_data != null)
_data.PropertyChanged -= HandleButtonDataPropertyChanged;
// Set private field
_data = value;
// Hook new events
if (_data != null)
_data.PropertyChanged += HandleButtonDataPropertyChanged;
// Immediate update since we have a new ButtonData object
if (_data != null)
Update();
}
}
}
private void HandleButtonDataPropertyChanged(object sender, PropertyChangedEventArgs e) {
// Handle change in ButtonData
Update();
}
private void Update() {
// Update...
}

Injecting (INotifyPropertyChanged functionality) to an instance of an class

I have a class that implements INotifyPropertyChanged.
I create an instance of a class in some viewModel.
Is it possible to remove this functionality from the class and inject it after the instance was created? I heard that ICustomTypeDescriptor would make this happen, but i dont know how to use it.
public class C : ICustomNotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public int _id;
public string _name;
public int Id
{
get { return _id; }
set
{
if (_id == value)
{
return;
}
_id = value;
OnPropertyChanged("Id");
}
}
public string Name
{
get { return _name; }
set
{
if (_name == value)
{
return;
}
_name = value;
OnPropertyChanged("Name");
}
}
public void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
If you are just trying to prevent the notifications from being fired when the object is first created and properties set, you can add boolean flag(s) that is/are false until the properties have been set once. You only execute the notification if the flag is true.
Edit:
I don't think there's a clean way to get the functionality in there after removing all the INotifyPropertyChanged code, but there are many ways to control the functionality from outside the instance.
Please note that I wrote all this code in the text editor, not in VisualStudio; it has not been tested in any way.
Add a method to enable notifications:
public class OptionalNotification : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string name) ...
bool _shouldNotify;
public void EnableNotifications()
{
_shouldNotify = true;
}
string _someProperty;
public string SomeProperty
{
get { return _someProperty; }
set
{
if(_someProperty == value) return
_someProperty = value;
if(_shouldNotify) OnPropertyChanged("SomeProperty");
}
}
}
You could do the same thing without the method, if you knew at the time of instantiation whether or not the instance should produce notifications, in which case you'd just need a boolean parameter in the constructor.
Another variation would be to use the Factory pattern, where your Factory has internal access to the boolean flag and sets it upon construction.
Encapsulate the condition in a proxy:
public interface IEntity : INotifyPropertyChanged
{
string SomeProperty { get; set; }
}
public class Entity : IEntity
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name) ...
string _someProperty;
public string SomeProperty
{
get { return _someProperty; }
set
{
if(_someProperty == value) return
_someProperty = value;
OnPropertyChanged("SomeProperty");
}
}
}
public class EntityNotificationProxy : IEntity
{
IEntity _inner;
public EntityNotificationProxy(IEntity entity)
{
_inner = entity;
_inner.PropertyChanged += (o,e) => { if(ShouldNotify) OnPropertyChanged(o,e); }
}
public bool ShouldNotify { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(object sender, PropertChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler != null) handler(sender, e);
}
public string SomeProperty
{
get { return _inner.SomeProperty; }
set
{
if(_inner.SomeProperty == value) return
_inner.SomeProperty = value;
}
}
}
Here your consuming classes get the entity proxy instead of the entity itself (but is none the wiser because it references only IEntity when you program to interfaces/abstractions). The wrapping of the proxy can happen in a factory or through an IoC container/DI framework.
The main advantage to this approach is that your entity maintains a pure INotifyPropertyChanged implementation, and the conditional aspect is handled from without. Another advantage is that it helps to enforce programming to abstractions and inversion of control.
The main disadvantage is that you'll need to create proxies for each INotifyPropertyChanged implementation that you want to have this conditional behaviour.
Create a registry to keep track of what instances should or should not raise notifications:
public static class PropertyNotificationRegistry
{
static IDictionary<INotifyPropertyChanged, bool> _registeredClasses
= new Dictionary<INotifyPropertyChanged, bool>;
static void Register(INotifyPropertyChanged o, bool shouldNotify)
{
if(!(_registeredClasses.ContainsKey(o)) _registeredClasses.Add(o, shouldNotify);
// could also implement logic to update an existing class in the dictionary
}
public static void ShouldNotifyWhenPropertiesChange(this INotifyPropertyChanged o)
{
Register(o, true);
}
public static void ShouldNotNotifyWhenPropertiesChange(this INotifyPropertyChanged o)
{
Register(o, false);
}
public static void NotifyPropertyChanged(this INotifyPropertyChanged o, Action notificationAction)
{
if(_registeredClasses.ContainsKey(o))
{
bool shouldNotify = _registeredClasses.Where(x => x.Key == o).Single().Value;
if(shouldNotify) notificationAction();
}
}
}
public class EntityUsingNotificationRegistry : INotifyPropertyChanged
{
... // all the standard INotifyPropertyChanged stuff
string _someProperty;
public string SomeProperty
{
get { return _someProperty; }
set
{
if(_someProperty == value) return;
_someProperty = value;
this.NotifyPropertyChanged(() => OnPropertyChanged("SomeProperty"));
}
}
}
public class SomethingInstantiatingOurEntity
{
public void DoSomething()
{
var entity1 = new EntityUsingNotificationRegistry();
entity1.ShouldNotifyWhenPropertiesChange();
var entity2 = new EntityUsingNotificationRegistry();
entity2.ShouldNotNotifyWhenPropertiesChange();
entity1.SomeProperty = "arbitrary string"; // raises event
entity2.SomeProperty = "arbitrary string"; // does not raise event
var entity3 = new EntityUsingNotificationRegistry();
entity3.SomeProperty = "arbitrary string"; // does not raise event
entity3.ShouldNotifyWhenPropertiesChange();
entity3.SomeProperty = "another arbitrary string"; // now raises event
}
}
Now, the registry has a distinct shortcoming in that it holds references to every instance and will prevent those instances from being picked up by the garbage collector. There may be a solution to this by implementing the registry with WeakReferences, but I'm not up-to-snuff on their usage to recommend a particular implementation.
This will not work. You COULD subclass and inject it, but you would have to change the byte-code to make sure the proper methods are CALLED - and that is the harder method.

Categories

Resources