I have a cms with a page that contains forms.
I want in the cms to have a dropdown with different class names that all implement a validation interface.
My form handlers contain a process method which I want to validate against the selected validation class.
So I have this interface:
public interface IEligibilityValidation
{
bool IsValid();
}
and for example this class
public class ComplexEligibilityValidation : IEligibilityValidation
{
public bool IsValid()
{
return true; /* complex logic here */
}
}
public class SimpleEligibilityValidation : IEligibilityValidation
{
public bool IsValid()
{
return true; /* less complex logic here */
}
}
And then I have the form handler that can read the selected class name for the validation.
I am unsure whether my handler should implement IEligibilityValidation as well or whether I can somehow invoke the class using reflection.
For example:
public class SampleFormHandler : IEligibilityValidation
{
public SimpleFormHandler(FormViewModel model, INode node)
{
this.model = model;
this.node = node;
eligiblityValidationClass = GetPropertyValue("eligibilityValidation");
}
public SampleProcessResult Process()
{
if (!string.IsNullOrEmpty(eligiblityValidationClass))
{
var thisType = Type.GetType(eligiblityValidationClass);
var thisInstance = Activator.CreateInstance(thisType);
var isValid = ((IEligibilityValidation)thisInstance).IsValid();
/* Works however, I need the IsValid to have access to a the FormHandler's properties and injected services */
}
}
public bool IsValid()
{
/* Will Always be ignored */
return true;
}
}
What is the most elegant way for doing something like this?
The idea is that different FormHandlers will have a different Validation class assigned to them and when invoked the IsValid method will be able to use the properties of that form.
I am looking for some advanced architecture or something smarter than just require unnecessary code.
Thanks