You can make a Validator interface with 2 implementations and then delegate to the validator. Technically, this is still a virtual call but its to another object so you don't have to worry about subclasses of Client
overriding the call or accessing the partially built Client.
public interface IValidator
{
bool Validate (IConfiguration configuration);
}
Then your normal usecase uses a ReconnectionValidator.
public class ReconnectionValidator : IValidator
{
bool Validate (IConfiguration configuration)
{
return configuration.ReconnectDelay >= 60000;
}
}
Your test Validator can always return true
public class NullValidator : IValidator
{
bool Validate (IConfiguration configuration)
{
return true;
}
}
Your Client code would then take both the IValidator
and an IConfiguration
in its constructor and test if the validator validates the configuration.
public Client(IConfiguration configuration, IValidator validator)
{
if(!validator.Validate(configuration))
{
throw new ConfigurationErrorsException();
}
}
The bonus of this technique is you can change the validator later or have a new implementation that chains several validators together to support "or"ing and "anding" validators together.