現在有一個FileStorageService類,繼承自IStorageService,具體實現如下
public interface IStorageService{ void WriteAllBytes(string path, byte[] buffer); byte[] ReadAllBytes(string path);}public class FileStorageService : IStorageService{ public void WriteAllBytes(string path, byte[] buffer) { File.WriteAllBytes(path, buffer); } public byte[] ReadAllBytes(string path) { return File.ReadAllBytes(path); }}
假設調用其中任一一個方法出現異常,例如讀寫文件時候經常碰見的異常:IOException
, DirectoryNotFoundException
, FileNotFoundException
,UnauthorizedaccessException
… 甚至是 OutOfMemoryException
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 specific Logger.Log("Oops something went wrong: " + exception.Message);}
catch exception并不是什么好主意,而且每次調用都需要使用try catch很不方便
一種改進的方法就是創建我們自己的異常類如StorageReadException,不管以后具體的實現如何變化,我們僅捕獲特定的異常來進行異常處理
public class StorageReadException : Exception{ public StorageReadException(Exception innerException) : base(innerException.Message, innerException) { }}
之前的FileStorageService實現更改為:
public byte[] ReadAllBytes(string path){ try { return File.ReadAllBytes(path); } catch (FileNotFoundException fileNotFoundException) { throw new StorageReadException(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 包裹問題,而且用戶必須依賴一個新的異常類型
我們可以使用Try模式來避免用戶使用try catch,Try模式類似C#里int方法
bool TryParse(string s, out int result),我們對接口進行更改
byte[] ReadAllBytes(string path)
變為
bool TryReadAllBytes(string path, out byte[] result)
但是這樣不能提供用戶更多的錯誤信息。如果我們想要顯示更多的有幫助的異常信息給用戶,可以返回一個通用的結果類OperationResult<TResult>
public class OperationResult<TResult>{ PRivate OperationResult () { } public bool Success { get; private set; } public TResult Result { get; private set; } public string NonSuccessMessage { get; private set; } public Exception Exception { get; private set; } public static OperationResult<TResult> CreateSuccessResult(TResult result) { return new OperationResult<TResult> { Success = true, Result = result}; } public static OperationResult<TResult> CreateFailure(string nonSuccessMessage) { return new OperationResult<TResult> { Success = false, NonSuccessMessage = nonSuccessMessage}; } public static OperationResult<TResult> CreateFailure(Exception ex) { return new OperationResult<TResult> { Success = false, NonSuccessMessage = String.Format("{0}{1}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace), Exception = ex }; }}
FileStorageService的ReadAllBytes 方法變為
public OperationResult<byte[]> TryReadAllBytes(string path){ try { var bytes = File.ReadAllBytes(path); return OperationResult<byte[]>.CreateSuccessResult(bytes); } catch (FileNotFoundException fileNotFoundException) { return OperationResult<byte[]>.CreateFailure(fileNotFoundException); }}
調用代碼:
var result = 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
新聞熱點
疑難解答