我想知道在我们开始实现这个接口验证之前,是否有任何方法可以改进它。总之,此代码定义了用于验证连接的接口和相关方法,特别关注参数验证和特定于连接的验证。
public interface IConnValidation
{
Task<ResultTuple> Validate(ConnectionSide side)
{
ResultTuple result = ValidateParameters(side);
return result.Ok
? ValidateConnection(side)
: result.ToTask();
}
ResultTuple ValidateParameters(ConnectionSide side)
{
try
{
return ValidateParametersInternal(side);
}
catch (Exception ex)
{
string message = $"{INTERNAL_ERROR} {ex.ToReport(false)}";
return ResultTuple.Failure(message);
}
}
async Task<ResultTuple> ValidateConnection(ConnectionSide side)
{
try
{
//DO NOT return the internal task as we need to handle the failure in this method
ResultTuple result = await ValidateConnectionInternal(side);
return result;
}
catch (Exception ex)
{
string message = $"{INTERNAL_ERROR} {ex.ToReport(false)}";
return ResultTuple.Failure(message);
}
}
protected ResultTuple ValidateParametersInternal(ConnectionSide side);
protected Task<ResultTuple> ValidateConnectionInternal(ConnectionSide side);
}
我尝试过这样的事情:
public interface IConnValidation
{
Task<ResultTuple> Validate(ConnectionSide side);
}
public class ConcreteConnValidation : IConnValidation
{
public async Task<ResultTuple> Validate(ConnectionSide side)
{
try
{
ResultTuple parameterValidationResult = await ValidateParameters(side);
if (!parameterValidationResult.Ok)
{
return parameterValidationResult;
}
return await ValidateConnection(side);
}
catch (Exception ex)
{
string message = $"{INTERNAL_ERROR} {ex.ToReport(false)}";
return ResultTuple.Failure(message);
}
}
protected virtual Task<ResultTuple> ValidateParameters(ConnectionSide side)
{
try
{
// Validate parameters asynchronously and return ResultTuple
}
catch (Exception ex)
{
string message = $"{INTERNAL_ERROR} {ex.ToReport(false)}";
return Task.FromResult(ResultTuple.Failure(message));
}
}
protected virtual async Task<ResultTuple> ValidateConnection(ConnectionSide side)
{
try
{
// Validate connection asynchronously and return ResultTuple
}
catch (Exception ex)
{
string message = $"{INTERNAL_ERROR} {ex.ToReport(false)}";
return ResultTuple.Failure(message);
}
}
}
但我想知道有没有更好的方法?