Validation rules with support for immediate termination of processing I am picking up a code smell from this code and I am not sure of the best way to rid myself of it.

I have a validation class that has a function that returns a list of IValidationRule&lt;T&gt; implementations (RuleOne, RuleTwo, RuleThree).

public interface IValidationRule&lt;T&gt;
{
    ValidationRuleResult Validate(T instance);
}

public override ValidationRules&lt;Order&gt; AllRules() 
=&gt; new ValidationRules&lt;Order&gt;
{
    Rules =
    {
        RuleOne(),
        RuleTwo(),
        new StopProcessingIfInvalidCheckpoint&lt;Order&gt;(),
        RuleThree()
    }
};


As the rules are iterated through, the IValidationRule&lt;T&gt;.Validate method is called on them.  They will return their results which are added to a collection.

I implemented a class that inherits from IValidationRule&lt;T&gt; called StopProcessingIfInvalidCheckpoint which will cease the processing of any rules following it if any of the rules before it fail.
I felt this was a clean and visual way to kick out of the validation routine early.

What I don't like is that the StopProcessingIfInvalidCheckpoint class has to be passed the same generic as all the other rules around it.  The Validate method on this class is never even called and thus, it does not matter that it knows it's generic is an Order.

Below is the extension method I use to validate a list of rules and you can see where I kick out if I come across the StopProcessingIfInvalidCheckpoint class:

public static ValidationRuleResult Validate&lt;T&gt;(
    this IEnumerable&lt;IValidationRule&lt;T&gt;&gt; validationRules, 
    T instance
)
{
    var validationResults = new List&lt;ValidationResult&gt;();

    foreach (var vr in validationRules)
    {
        if (vr is StopProcessingIfInvalidCheckpoint&lt;T&gt; &amp;&amp; validationResults.Any()) { break; }
        var result = vr.Validate(instance);
        if (result.IsValid) { continue; }
        validationResults.AddRange(result.ValidationResults);
    }

    var res = new ValidationRuleResult
    {
        ValidationResults = validationResults,
        IsValid = !validationResults.Any()
    };
    return res;
}


How can I improve this code?
You shouldn't bind the implementation to any particular type in this case the StopProcessingIfInvalidCheckpoint.

I think it would be better to drop this class and instead add a new property IsFinal to the interface. You could check if the validation fails and IsFinal is set to true then you stop processing other rules.

If you want to get rid of the generic argument but at same time you want to keep it for other implementations then add another abstraction layer. Make the interface non-generic and add an abstract class that is derived from the non-generic interface:

interface IValidationRule
{
    ValidationRuleResult Validate(object instance);
    bool IsFinal { get; }
}

abstract class ValidationRule&lt;T&gt; : IValidationRule
{
    public abstract ValidationRuleResult Validate(object instance);
    public bool IsFinal { get; set; }
}

class FooValidationRule : ValidationRule&lt;int&gt;
{
    public override ValidationRuleResult Validate(object instance)
    {
        ... do something with (T)instance;
    }
}


and

foreach (var vr in validationRules)
{
    var result = vr.Validate(instance);
    if (!result.IsValid &amp;&amp; vr.IsFinal)
    {
        break;
    }
    validationResults.AddRange(result.ValidationResults);
}


so

Rules =
{
    RuleOne(),
    RuleTwo() { IsFinal = true },        
    RuleThree()
}