问题现在有一个FileStorageService类,继承自IStorageService,具体实现如下
publicinterfaceIStorageService
{voidWriteAllBytes(stringpath,byte[] buffer);byte[] ReadAllBytes(stringpath);
}publicclassFileStorageService : IStorageService
{publicvoidWriteAllBytes(stringpath,byte[] buffer)
{
File.WriteAllBytes(path, buffer);
}publicbyte[] ReadAllBytes(stringpath)
{returnFile.ReadAllBytes(path);
}
}
假设调用其中任一一个方法出现异常,例如读写文件时候经常碰见的异常:IOException, DirectoryNotFoundException, FileNotFoundException,UnauthorizedaccessException… 甚至是 OutOfMemoryException
方案1-不是我的问题IStorageService 不关心抛出的异常,那是使用者的职责。如此便将问题抛给了使用IStorageService接口的用户,它们必须要捕获有可能抛出的异常,往往一种偷懒的做法就是使用try catch语句将其包裹起来,如:
IStorageService myStorageService = Resolver.Resolve<IStorageService>();try{
myStorageService.ReadAllBytes("C:\stuff.data");
}catch(Exception exception)
{//please don't write generic error messages like this, be specificLogger.Log("Oops something went wrong:"+exception.Message);
}
catch exception并不是什么好主意,而且每次调用都需要使用try catch很不方便
方案2-创建新的异常类一种改进的方法就是创建我们自己的异常类如StorageReadException,不管以后具体的实现如何变化,我们仅捕获特定的异常来进行异常处理
publicclassStorageReadException : Exception
{publicStorageReadException(Exception innerException)
:base(innerException.Message, innerException)
{
}
}
之前的FileStorageService实现更改为:
publicbyte[] ReadAllBytes(stringpath)
{try{returnFile.ReadAllBytes(path);
}catch(FileNotFoundException fileNotFoundException)
{thrownewStorageReadException(fileNotFoundException);
}
}
调用代码:
IStorageService myStorageService = Resolver.Resolve<IStorageService>();try{
myStorageService.ReadAllBytes(path);
}catch(StorageReadException sre)
{
Logger.Log(String.Format("Failed to read file from path, {0}: {1}", path, sre.Message));
}
同样存在try catch 包裹问题,而且用户必须依赖一个新的异常类型
方案3-Try 模式并返回一个Complex Result我们可以使用Try模式来避免用户使用try catch,Try模式类似C#里int方法
bool TryParse(string s, out int result),我们对接口进行更改
byte[] ReadAllBytes(stringpath)
变为
boolTryReadAllBytes(stringpath,outbyte[] result)
但是这样不能提供用户更多的错误信息。如果我们想要显示更多的有帮助的异常信息给用户,可以返回一个通用的结果类OperationResult<TResult>
publicclassOperationResult<TResult>{PRivateOperationResult ()
{
}publicboolSuccess {get;privateset; }publicTResult Result {get;privateset; }publicstringNonSuccessMessage {get;privateset; }publicException Exception {get;privateset; }publicstaticOperationResult<TResult>CreateSuccessResult(TResult result)
{returnnewOperationResult<TResult> { Success =true, Result =result};
}publicstaticOperationResult<TResult> CreateFailure(stringnonSuccessMessage)
{returnnewOperationResult<TResult> { Success =false, NonSuccessMessage =nonSuccessMessage};
}publicstaticOperationResult<TResult>CreateFailure(Exception ex)
{returnnewOperationResult<TResult>{
Success=false,
NonSuccessMessage= String.Format("{0}{1}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace),
Exception=ex
};
}
}
FileStorageService的ReadAllBytes方法变为
publicOperationResult<byte[]> TryReadAllBytes(stringpath)
{try{varbytes =File.ReadAllBytes(path);returnOperationResult<byte[]>.CreateSuccessResult(bytes);
}catch(FileNotFoundException fileNotFoundException)
{returnOperationResult<byte[]>.CreateFailure(fileNotFoundException);
}
}
调用代码:
varresult =myStorageService.TryReadAllBytes(path);if(result.Success)
{//do something}else{
Logger.Log(String.Format("Failed to read file from path, {0}: {1}", path, result.NonSuccessMessage));
}
原文:Error Handling in SOLID C# .NET – The Operation Result Approach