Try/Catch 块过多

public Customer GetCustomer(string customerId)
{
try
{
var command = new SqlCommand();
var reader = command.ExecuteReader();
var customer = new Customer();
while(reader)
{
customer.customerId=customerId;
customer.CustomerName=reader["CustomerName"].ToString();
customer.CustomerStatus=reader["CustomerStatus"].ToString();
customer.LoyaltyProgram=reader["CustomerLoyaltyProgram"].ToString();
}
return customer;
}
catch(Exception ex)
{
_logger.LogException(ex);
var customer=new Customer{CustomerStatus="nuknown"};
return customer;
}

将每个代码块中的这些方法析取到外部方法

private static Customer GetCustomerFromDataStore(string customerId)
{
var command = new SqlCommand();
var reader = command.ExecuteReader();
var customer = new Customer();
while(reader)
{
customer.customerId=customerId;
customer.CustomerName=reader["CustomerName"].ToString();
customer.CustomerStatus=reader["CustomerStatus"].ToString();
customer.LoyaltyProgram=reader["CustomerLoyaltyProgram"].ToString();
}
return customer;
}
private Customer HandleDataStoreExceptionWhenRetrievingCustomer(Exception ex)
{
_logger.LogException(ex);
var customer=new Customer{CustomerStatus="nuknown"};
return customer;
}

重构结果:

public Customer GetCustomer(string customerId)
{
try
{
return GetCustomerFromDataStore(customerId);
}
catch(Exception ex)
{
return HandleDataStoreExceptionWhenRetrievingCustomer(ex);
}
}

典型重构3 (Try/Catch)的更多相关文章

  1. Python积木之with

    简而言之,with 语句是典型的程序块 “try catch finally”的一种模式抽取.python的作者在PEP343中写道 “ This PEP adds a new statement & ...

  2. Java 写 Log

    . 一个最基本的例子 使用Logging框架写Log基本上就三个步骤 引入loggerg类和logger工厂类 声明logger 记录日志 下面看一个例子 //1. 引入slf4j接口的Logger和 ...

  3. 重构机房收费系统你要用的——异常处理和抛出异常(try catch finally)——(vb.net)

    你能保证你的程序不会出问题吗? 不能 当你的程序执行到某个地方发生了你不想要的结果.你是否想让它一错再错? 不想 你是否想让你的程序占着茅坑不拉屎? 不想 你是否想知道你的程序出错的原因? 想 个问题 ...

  4. C#重构经典全面汇总

    C#重构经典全面汇总 1.  封装集合 概念:本文所讲的封装集合就是把集合进行封装,仅仅提供调用端须要的接口. 正文:在非常多时候,我们都不希望把一些不必要的操作暴露给调用端,仅仅须要给它所须要的操作 ...

  5. 记一次.NET代码重构

    好久没写代码了,终于好不容易接到了开发任务,一看时间还挺充足的,我就慢慢整吧,若是遇上赶进度,基本上直接是功能优先,完全不考虑设计.你可以认为我完全没有追求,当身后有鞭子使劲赶的时候,神马设计都是浮云 ...

  6. ASP.NET MVC5+EF6+EasyUI 后台管理系统(58)-DAL层重构

    系列目录 前言:这是对本文系统一次重要的革新,很久就想要重构数据访问层了,数据访问层重复代码太多.主要集中增删该查每个模块都有,所以本次是为封装相同接口方法 如果你想了解怎么重构普通的接口DAL层请查 ...

  7. ASP.NET MVC5+EF6+EasyUI 后台管理系统(59)-BLL层重构

    系列目录 前言:  这应该是本系统最后一次重构,将重构BLL层和Model层.来完全取代代码生成器生成的BLL层和DAL层.完全废掉了代码生成器的DAL,BLL,MODEL层.  全自动生成增,删,改 ...

  8. Rafy 领域实体框架设计 - 重构 ORM 中的 Sql 生成

    前言 Rafy 领域实体框架作为一个使用领域驱动设计作为指导思想的开发框架,必然要处理领域实体到数据库表之间的映射,即包含了 ORM 的功能.由于在 09 年最初设计时,ORM 部分的设计并不是最重要 ...

  9. .NET重构(类型码的设计、重构方法)

    阅读目录: 1.开篇介绍 2.不影响对象中的逻辑行为(枚举.常量.Entity子类来替代类型码) 3.影响对象中的逻辑行为(抽象出类型码,使用多态解决) 4.无法直接抽象出类型码(使用策略模式解决) ...

随机推荐

  1. mysql 基础知识

    Mysql 远程登录及常用命令 第一招.mysql服务的启动和停止 net stop mysql net start mysql 第二招.登陆mysql 语法如下: mysql -u用户名 -p用户密 ...

  2. hover和mouseover,mouseout的区别

    说道hover和mouseover,mouseout的区别,不得不联系到mouseenter,mouseleave. mouseover,mouseout是指鼠标指针在穿过/离开被选元素或其子元素时触 ...

  3. Freezable 对象(WPF)

    # Freezable 对象(WPF) # > Freezable 继承自 DependencyObject,同时添加了 Freezable 方法,用于冻结对象. --- ## 冻结对象 ## ...

  4. 《零成本实现Web自动化测试--基于Selenium》第一章 自动化测试基础

    第一篇 Selenium 和WebDriver工具篇 第一章 自动化测试基础 1.1    初识自动化测试 自动化测试有两种常见方式 1.1.1 代码驱动测试,又叫测试驱动开发(TDD) 1.1.2 ...

  5. Azure IaaS for IT Pros Online Event 总结

    微软一个为期4天的一个有关于Azure的介绍,主要总结了些Azure现有的技术以及将会推出东西 主题链接 http://channel9.msdn.com/Events/Microsoft-Azure ...

  6. OBIEE 11g:Error:nQSError 36010 Server version 318 cannot read the newer version of the repository

    biee11g升级到最新版以后,发现了一些bug,需要回退到原来的版本,卸载掉升级包以后,启动BI服务,会报上述错误.这是因为资料库文件已经升级为了最新版本.这时候我们需要将资料库文件进行降版本操作. ...

  7. gdb调试大全

    原文:http://blog.csdn.net/dadalan/article/details/3758025

  8. UIActivityIndicatorView的使用

    class ViewController: UIViewController,UIActionSheetDelegate{ @IBOutlet weak var label1: UILabel! @I ...

  9. 指向const的指针和const指针

    1.指向const的指针:const int *p 或 int const *p 解释:p是一个指针,指向const int类型的常量:指针指向的内容为常量,因此不能改变*p的值,但指针p可以改变,指 ...

  10. Opencv图像与矩阵的操作

    #include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h ...