Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the property.

C#
private MyModel _selectedModel;
public MyModel SelectedModel
{
   get {return _selectedModel;}
   set {ValidateProperty(ref _selectedModel, value);}
}   

ValidateProperty
is a somehow base class which inherits
inotifypropertychanged
. It has
public event PropertyChangedEventHandler PropertyChanged;
But I can't change it because everybody is using it.

My question is when the property changed, I need to call the service. So my code is
private MyModel _selectedModel;
public MyModel SelectedModel
{
   get {return _selectedModel;}
   set
   {
         ValidateProperty(ref _selectedModel, value);
		 if(value != null)
		     CallService(value);
   }
}


Not sure if it is a good practice?

What I have tried:

Thanks for the code support. Maybe use event instead of call service in setter?
Posted
Comments
Richard Maly 30-Aug-18 3:45am    
I am going to go out on a limb and say it sounds okay to me. This is exactly why Properties are different from Variables. However depending on what is happening further along the code there could be side effects. I am assuming this is either SL or XAML so you might want to consider spawning of on a thread so whatever the service does, doesn't block your UI
Christiaan van Bergen 30-Aug-18 9:17am    
Be aware that the ValidateProperty method might do some modifications on values before the are transferred to _selectedModel. You are executing your code on the 'value', are you sure you want to do that? Is it not that you want to execute your code on '_selectedModel' ?

And actually your code will execute everytime the value of SelectedModel will be set and not be null. This is somewhat different than it calling your code whenever the property is changed.

And yes, you might be better off creating a new handler responding to the PropertyChanged event.
Member 12658724 30-Aug-18 9:30am    
It is a wpf application. When I click a row on the grid, the property changes and will call the service.
Member 12658724 6-Sep-18 20:53pm    
I may use event, but not sure how to do it for this case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900