国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

錯誤處理(OperationResult)方法

2019-11-14 15:50:25
字體:
來源:轉載
供稿:網友

問題

現在有一個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);    }}

 

假設調用其中任一一個方法出現異常,例如讀寫文件時候經常碰見的異常:IOExceptionDirectoryNotFoundExceptionFileNotFoundException,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 specific    Logger.Log("Oops something went wrong: " + exception.Message);}

catch exception并不是什么好主意,而且每次調用都需要使用try catch很不方便

方案2-創建新的異常類

一種改進的方法就是創建我們自己的異常類如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 包裹問題,而且用戶必須依賴一個新的異常類型

方案3-Try 模式并返回一個Complex Result

我們可以使用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        };    }}

FileStorageServiceReadAllBytes 方法變為

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

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 启东市| 广德县| 凯里市| 南岸区| 卓尼县| 安塞县| 孙吴县| 济宁市| 杂多县| 永靖县| 京山县| 马关县| 萨迦县| 孝昌县| 交城县| 甘泉县| 阿拉善右旗| 四会市| 嵊泗县| 什邡市| 阿尔山市| 固原市| 泉州市| 新乡县| 嘉鱼县| 越西县| 门头沟区| 乌苏市| 蓝田县| 寻乌县| 商洛市| 南岸区| 平舆县| 丰镇市| 巴彦县| 浦县| 临漳县| 宜兰县| 绩溪县| 武强县| 舞阳县|