我有一个Client
类,在构造函数中接受一个实现了IConfiguration
接口的对象。
Client
对象创建时应该验证配置。
public interface IConfiguration
{
int ReconnectDelay { get; }
}
public class Client
{
public Client(IConfiguration configuration)
{
if (configuration.ReconnectDelay < 60000)
{
throw new ConfigurationErrorsException();
}
}
}
为了测试目的,我需要一个客户端,其中
ReconnectDelay
属性设置为小于有效值的值。以下是我的当前解决方案:
public class Client
{
public Client(IConfiguration configuration)
{
ValidateConfiguration(configuration);
}
protected virtual void ValidateConfiguration(IConfiguration configuration)
{
if (configuration.ReconnectDelay < 60000)
{
throw new ConfigurationErrorsException();
}
}
}
public class TestClient : Client
{
public TestClient(IConfiguration configuration)
: base(configuration)
{
}
protected override void ValidateConfiguration(IConfiguration configuration)
{
}
}
它能够工作,但在构造函数中调用虚方法,这是不好的(我知道现在它不会有任何问题,但我仍想解决这个问题)。
所以,有没有优雅的解决方案呢?
TestClient
类声明为sealed
吗? - Dmitry