You could use a chain of responsibility style pattern where a there are a series of classes each one responsible for checking one aspect of validity and reporting the type of error.
you would pass your Prod
to the first class which would check the validity, if it was invalid it would report and return false. if it was valid it would return the validity check of the successor of the current instance (if it had one).
then your code could just get the Prod
and pass it to the root validity checker and continue if it reported false.
This would also allow you to easily add new validators in the future, potentially being able to do it without having to recompile, depending on how you implemented the construction of the chain of validators.
something like this: (pseudo code, not tested/compiled)
public interface IValidator
{
bool IsValid(Product product);
IValidator Successor{get;set;};
}
public class AbstractValidator : IValidator
{
IValidator m_successor=null;
public abstract bool IsValid(Product product);
IValidator Successor
{
get
{
if(m_sucessor==null)
return new AlwaysValidSuccessor();
else
return m_successor;
}
set
{
m_successor=value;
}
}
}
public class NullValidator : AbstractValidator
{
public bool IsValid(Product product)
{
if (product!=null)
{
return Successor.IsValid(product);
}
return false;
}
}
public class ExpiredValidator : AbstractValidator
{
public bool IsValid(Product product)
{
if (product.Expired==false) //whatever you need to do here
{
return Successor.IsValid(product);
}
return false;
}
}
then you use it:
IValidator validatorRoot = new NullValidator();
validatorRoot.Successor = new ExpiredValidator();
// construct the chain with as many as you need
//then use it
if (validatorRoot.IsValid(Prod)==false)
{
continue;
}