I'm implementing some input data validators in my app and I ended up with an Abstract class containing common props that are mapped to JSON. I also created abstract and virtual methods to force derived classes to always keep the same validation algorithm pattern. I have not followed any Design Pattern and because of it I would like to know your opinion about my code.
My abstract class is something like that:
public abstract class DataValidation
{
[JsonProperty("cleaned")]
public string CleanedInput => _cleanedInput;
[JsonProperty("isValid")]
public bool IsValid => _isValid;
protected string _cleanedInput;
private bool _isValid;
public DataValidation(string input)
{
_cleanedInput = CleanInput(input);
_isValid = ValidateInput(_cleanedInput);
}
protected abstract bool ValidateData(string cleanedInput);
protected virtual string CleanInput(string input) => input.Trim();
}
And some derived classes where I perform validations like birth date or documents format:
public sealed class ClassA : DataValidation
{
public ClassA(string input) : base(input)
{
}
protected override bool ValidateData(string input)
{
// ... Logic to validate input.
// Returns true as an example.
return true;
}
}
And another class that overrides CleanInput method and returns invalid validation:
public sealed class ClassB : DataValidation
{
public ClassB(string input) : base(input)
{
}
protected override string CleanInput(string input) =>
return input.Trim().Replace(".", "").Replace("-", "");
protected override bool ValidateData(string input)
{
// ... Logic to validate input.
// Returns false as an example.
return false;
}
}
Finally, I serialize these objects and have a json like that:
// ClassA
{
"cleaned": "inputA_cleaned",
"isValid": true
}
// ClassB
{
"cleaned": "inputB_cleaned",
"isValid": false
}
What I did is considered to be a good or bad practice?
Aucun commentaire:
Enregistrer un commentaire