+ 2
Self validating class
If I am making a class for Person which includes name parts, first middle last etc. and age (among other things), what is the best way to implement an “IsValid” concept? Pseudo code examples of the code using my class. If (Person.firstName.isValid) { //act on the object } else { //do work to report invalid data } If you are tempted to point out that bad data should not have made it into the object when it was instantiated, then it would be helpful for you to know that this code is intended to process bad data and report that it is bad so the original faulty state must be stored to report the issues before another process is performed where bad data is not allowed.
9 Respuestas
+ 5
Not long ago, I've written classes in which constructors receive input from a source which may fail. A quick research concluded that the best practice would be to throw an exception from the constructor, and to only resort to placing flags within the class if exceptions, for whatever reason, cannot be implemented. I'm not sure how relevant this would be in C#, but this appears to be your case since receiving bad data is exactly what you want to do.
Considering the nature of what you are trying to validate, I would suggest implementing isValid as a method which returns a boolean value based on appropriate regex matches.
+ 4
C# actually has what you are looking for built in, they are called DataAnnotations!
Check out this article: https://code.msdn.microsoft.com/Basic-Introduction-to-Data-244734a4
You can even validate using Regex and lots of other stuff: https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations?redirectedfrom=MSDN&view=netframework-4.8
+ 3
Schindlabua , this is great news as I was about to be writing about 60 validation functions but this will shrink the code and make it more readable, reusable, modifiable, and maintainable
+ 3
SQrL Ha, every problem you have was had by at least a thousand other people before and some of them will have come up with great solutions. It's a nice realization to make :D
+ 2
David Carroll or David Ashton I would really love your opinion on such things
+ 2
SQrL Good question... There are several ways one could go about implementing data validation on POCO types. It really depends on your use cases and application architecture that will determine the most ideal approaches to consider.
Validation Attributes are great when the data must always be in a valid state and you prefer to define validation rules at the property level.
However, in some cases, it might be expected that data in an object will start out in an invalid state and later be ready to be validated. In this case, you'll want to expose an IsValid() method on the POCO type, maybe as an Extension method, or even as a separate Validation class. You could even use a combination of Validation Attributes and IsValid methods.
If method based validation is called for, I recommend checking out the following library. Hopefully this was helpful.
https://fluentvalidation.net
0
Implement an extension method by adding a static class with a static method with method signature, static bool IsValid(this string valueToValidate){
// validation logic on a single string goes here
}
This will allow you to use the code like If(Person.firstName.IsValid ) as long as the the property or variable you are attaching IsValid to is a string it will work.
0
Matengele Kaira , if I have to do this to 60-100 strings, i thik the extension pattern might cause a problem since the rules are different for most (not all) strings
0
SQrL, depends , you could have some if statements for the different scenarios in the extension method and when you attach it to a string value using the .IsValid it will check the string and pick the appropriate if statement block to validate that particular string. Therefore you could get away with writing just 1 extension method that handles several scenarios