Write a validation routine for a C# class on change event - c#

I'm trying to write an "onchange" event for a C# class that I have. The idea would be to capture anytime the class was instantiated or a property was changed and fire off some code to evaluate the "health" of the object, then set a property of the class to true or false based off of the method being invoked. My initial attempts were to simply call a private method in the setter of each property as such:
string _source = null;
public string Source
{
set
{
this._source = value;
OnClassChange();
}
get { return this._source; }
}
string _dest = null;
public string Dest
{
set
{
this._dest = value;
OnClassChange();
}
get { return this._dest; }
}
bool _isValid;
public bool IsValid
{
get { return _isValid; }
}
void OnClassChange()
{
_isValid = (_source == null) ? false : true ;
_isValid = (_dest == null) ? false : true;
}
but this seems sort of clunky and not elegant... I'd like to use some sort of listener, then in my OnClassChange() block simply loop through all the private properties of the class, determine the type of property and invoke some logic to determine if the values of the property is valid or not in a loop.

You don't really need a field for this at all - unless the validation is costly (so you want to avoid recomputing it each time it's requested) you can just have:
public string Source { get; set; }
public string Destination { get; set; }
public bool IsValid { get { return Source != null && Destination != null; } }
In C# 6 the IsValid code would be be even simpler:
public bool IsValid => Source != null && Destination != null;

Related

how to call Inner Class in C#

i am trying to set inner class value from outer class using event handler
here the line for passing the value , PaymentMode is Event
public event PaymentModeEven PaymentMode;
PaymentMode(this,new PaymentModeEvenArgs() { paymentSuccess = true });
Inner Class
public class PaymentModeEvenArgs: EventArgs
{
private bool PaymentSuccess;
public bool paymentSuccess
{
get { return paymentSuccess; }
set
{
paymentSuccess = value;
}
}
}
Program get stuck and stopped
You have a stack overflow exception. Consider your property:
public bool paymentSuccess
{
get { return paymentSuccess; }
set
{
paymentSuccess = value;
}
}
When you get or set paymentSuccess, what does it do internally? It gets or sets paymentSuccess. Which, internally, gets or sets paymentSuccess. Which, internally... You get the idea.
It looks like you meant to swap the casing between the field and the property:
private bool paymentSuccess;
public bool PaymentSuccess
{
get { return paymentSuccess; }
set
{
paymentSuccess = value;
}
}
Or, even better, just use an auto-implemented property so you only have to make one named member:
public bool PaymentSuccess { get; set; }

property private set crashes unity

I have a custom class that gets some data from the web.
When I get this data I want to set it to the value of a property but when I do this unity crashes. The commented line generates the crash without this line everything works fine. See my code below:
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class GetDB
{
private readonly Main m;
private readonly string Url;
public string DBData {
get
{
if(DBData == null)
return null;
else
return DBData;
}
private set
{
DBData = value;
}
}
public GetDB(Main m, string url)
{
this.m = m;
this.Url = url;
}
public void GetServerData(){
m.StartCoroutine(GetText(Url, (result) =>{
this.DBData = result; //THIS LINE CRASHES UNITY
Debug.Log(result);
}));
}
IEnumerator GetText(string url, Action<string> result) {
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
if (result != null)
result(www.downloadHandler.text);
}
}
}
How would I go about fixing this, and what exactly is happening here?
If anything is unclear let me know so I can clarify.
You have to use a backing field for the property:
string _dbData;
public string DBData
{
get
{
if(_dbData == null)
return null;
else
return _dbData;
}
private set
{
_dbData= value;
}
}
A property is just syntactic sugar for a getter and setter methods. So you can rewrite your property like:
public string GetDBData()
{
if(_dbData == null)
return null;
else
return _dbData;
}
public void SetDBData(string value)
{
_dbData = value;
}
The way you have implemented the property:
public void SetDBData(string value)
{
// you will never get out of here
SetDBData(value);
}
Properties act as accessors for variables. What is happening in your case is basically an endless loop - whenever somebody tries to get the value of your property, it keeps returning the property itself. Instead, you want a backing field _dbData:
private string _dbData;
public string DBData
{
get
{
return _dbData;
}
private set
{
_dbData = value;
}
}
Now your property controls the accesss to this field.
Your accessor can be really simplified.
Doing :
get
{
if(DBData == null)
return null;
else
return DBData;
}
Will provide exactly the same result than doing :
get
{
return DBData; //if DBData is null, it will return null
}
So, you can write your accessor that way :
public string DBData
{
get;
private set;
}

Can I use constructor logic with object initializer syntax?

i.e.
MyClass myClass = new MyClass() { Value = 5 };
I have a bunch of constructor calls like the one above, but now I've realized I need to add logic to the constructor, which was a massive oversight. Currently I have no constructor, so just a blank implicit default constructor.
The below code should explain my problem.
Edit: I'm not actually doing validation, that's just a simple example of constructor logic
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Test(1) + " should be true");
Console.WriteLine(new Test(0) + " should be false");
Test test = new Test(0) { Value = 1 }; // It allows this syntax, oddly, but the value that's used is the one passed as a parameter
Console.WriteLine("I wish " + test + " was true");
// This is what I have currently, but I'd like to add logic like that which exists in the parameterized constructor
//Test test = new Test() { Value = 1 } // Would ideally function just like Test(1), otherwise I have to go and change every call
// OUTPUT
// True should be true
// False should be false
// I wish False was true
Console.ReadLine();
}
}
class Test
{
public bool? IsGood { get; }
public int Value { get; set; }
// This doesn't currently exist in my class, but I'd like to add it
public Test(int value)
{
if (value == 1)
IsGood = true;
else
IsGood = false;
}
public override string ToString()
{
return IsGood.ToString();
}
}
Don't write code like this in the first place.
I'd write your code like this:
class Test
{
public static bool IsValid(int value)
{
return whatever; // test for validity here
}
public int Value { get; private set; } // Don't let anyone change it.
public Test(int value) {
if (!IsValid(value)) throw new InvalidArgumentException("value");
this.Value = value;
}
}
There, now Value is always valid; the user can know ahead of time whether it is valid or not; an attempt to set an invalid value produces an exception. This assumes that Value cannot change.
If Value can change then write it like this:
class Test
{
public static bool IsValid(int value)
{
return whatever; // test for validity here
}
private int value;
public int Value { get { return value; }
set
{
if (!IsValid(value)) throw new InvalidArgumentException("value");
this.value = value;
}
}
public Test(int value) {
this.Value = value;
}
}
Now the value is again always legal.
If it is legal for value to be invalid, then:
class Test
{
public bool IsValid
{
get
{
return whatever; // test for validity here
} // read-only property
}
public int Value { get; set; }
public Test(int value) {
this.Value = value;
}
}
Now the value can be any integer and whether it is valid or not can be tested dynamically.
Can I set members outside of a constructor while still using logic in the constructor?
Meaning, what, exactly?
Using the object initializer syntax, a constructor still runs. You may even choose which one to use, through the normal constructor overload syntax (which you seem to show, but you say it's not in your class?). The code in your constructor looks at the parameter value that is passed to it, not the property Value (which it doesn't even set). But if you meant for the two to work together, then sure…you can set the property in the constructor and set IsGood in the Value property setter.
If you're going to do it that way, then I would not bother with the logic in the constructor at all. Just set the Value property and let its setter do the rest of the work:
class Test
{
public bool? IsGood { get; private set; }
private int _value;
public int Value
{
get { return _value; }
set
{
_value = value;
IsGood = _value == 1;
}
}
public Test(int value)
{
Value = value;
}
}
I should point out that the semantics of the above is slightly different from what you seem to have started with. That is, the Value property is not read-only, and so can be set at any time. So, similarly, the IsGood property can change at any time. You previously had declared it as read-only and it was settable only in the constructor.
It's not clear from your question whether that's a problem or not. If you want IsGood to be strictly read-only (i.e. without even a private setter), then it won't be possible to do literally what you're asking for, because in the object initializer syntax, it relies on setting member properties after the constructor has already returned.
For the moment, I'll assume it's not a problem to add the private setter to the IsGood property.
Note that since IsGood apparently depends solely on the value of Value, you could even implement the above like this:
class Test
{
public bool? IsGood => _value != null ? _value == 1 : (bool?)null;
private int? _value;
public int Value
{
get { return _value ?? 0; }
set { _value = value; }
}
public Test(int value)
{
Value = value;
}
}
That is, don't even bother storing a value for IsGood. Just return the appropriate value based on the current state of the Value property (null if it's never been set, true if it's currently set to 1, and false otherwise).

Updating an object from another objects property history in C# for implementing a PATCH

I'm trying to implement a PATCH on Web API for an object that will be stored in a DB. The input object from the controller has all of the properties that can be modified but we allow the client to choose which fields to send back. We only want to update the MongoDB representation if some of the fields have changed or been set. We started using a Dirty object pattern (not sure this is a pattern) whereby when you set a property you also record that it is dirty. for instance
public class Example
{
private string _title;
public string Title
{
get { return _title; }
set
{
_title = value;
TitleWasSet = true;
}
}
public bool TitleWasSet {get;set;}
}
This could work but is kind of tedious and I feel it exposes lots of logic that could be contained.
So a solution I came up with was to store the update Actions in the inbound object then reapply them to the Mongo Object in a Try Update fashion.
like this:
public class Data
{
public string Header { get; set; }
public int Rating { get; set; }
}
public class EditDataRequest
{
private readonly List<Action<Data>> _updates;
public EditDataRequest()
{
_updates = new List<Action<Data>>();
}
public string Header
{
set
{
_updates.Add(data => {data.Header = value;});
}
}
public int Rating
{
set
{
_updates.Add(data => {data.Rating = value;});
}
}
public bool TryUpdateFromMe(Data original)
{
if (_updates.Count == 0)
return false;
foreach (var update in _updates)
{
update.Invoke(original);
}
return true;
}
}
Now this would work great but it doesn't take account of the values being the same. So i then looked at changing the list of actions to a list of functions that would return a bool if there was a difference in the value.
private readonly List<Func<Data, bool>> _updates;
And then the properties would look like this:
public int Rating
{
set
{
_updates.Add(data => {
if (data.Rating != value)
{
data.Rating = value;
return true;
}
return false;
});
}
}
And the try update method...
public bool TryUpdateFromMe(Data original)
{
if (_updates.Count == 0)
return false;
bool changesRequired = false;
foreach (var update in _updates)
{
changesRequired |= update.Invoke(original);
}
return changesRequired;
}
As you can see that property set implementation is rather clunky and would make the code nasty to read.
I'd like a way of extracting the check this property value then update it to another method that I can reuse in each property - I assume this is possibly somehow but it might not be.
Of course, if you have better suggestions for how to handle the PATCH situation then I'd be happy to hear them as well.
Thanks for reading this far.

What's the order of execution in property setters when using IDataErrorInfo?

Situation: Many times with WPF, we use INotifyPropertyChanged and IDataErrorInfo to enable binding and validation on our data objects. I've got a lot of properties that look like this:
public SomeObject SomeData
{
get { return _SomeData; }
set { _SomeData = value; OnPropertyChanged("SomeData"); }
}
Of course, I have an appropriate overridden IDataErrorInfo.this[] in my class to do validation.
Question: In a binding situation, when does the validation code get executed? When is the property set? When is the setter code executed? What if the validation fails?
For example:
User enters new data.
Binding writes data to property.
Property set method is executed.
Binding checks this[] for validation.
If the data is invalid, the binding sets the property back to the old value.
Property set method is executed again.
This is important if you are adding "hooks" into the set method, like:
public string PathToFile
{
get { return _PathToFile; }
set
{
if (_PathToFile != value && // prevent unnecessary actions
OnPathToFileChanging(value)) // allow subclasses to do something or stop the setter
{
_PathToFile = value;
OnPathToFileChanged(); // allow subclasses to do something afterwards
OnPropertyChanged("PathToFile");
}
}
}
If you want fine-grained control over the timing of validation, you can have it:
private Dictionary<string, string> Errors = new Dictionary<string, string>();
private object _MyProperty;
public object MyProperty
{
get { return _MyProperty; }
set
{
Errors["MyProperty"] = null;
if (value == _MyProperty)
{
return;
}
ValidateMyProperty(value); // may set Errors["MyProperty"]
if (Errors["MyProperty"] == null)
{
_MyProperty = value;
OnPropertyChanged("MyProperty");
}
}
}
public string this[string propertyName]
{
return Errors[propertyName];
}
No matter when data error information is requested and who's requesting it, it always returns the property's validation status as of the last time something tried to set the property.
Note that if you work at it, you can encapsulate the logic thusly:
public object MyProperty
{
set { _MyProperty = Validate("MyProperty", value, _MyProperty); }
}
private Dictionary<string, Func<object, string>> ValidationFunctions;
private object Validate(string propertyName, object value, object field)
{
Errors[propertyName] = null;
if (value == field)
{
return;
}
if (!ValidationFunctions.ContainsKey(propertyName))
{
return value;
}
Errors[propertyName] = ValidationFunctions[propertyName](value);
return (Errors[propertyName] == null)
? value
: field;
}
}

Categories