问题现在有一个
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); } }
假设调用其中任一一个方法出现异常,例如读写文件时候经常碰见的异常: 方案1-不是我的问题
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 }; } }
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)); }
|
|
来自: 昵称10504424 > 《工作》