在调用一个自定义的GeneralHandler类里面的一个方法,该方法是针对数据库的一张表的所有操作(CRUD),根据传入的DealType来判断做那种操作

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace WinFormPro
{
/// <summary>
/// 接收外部访问的处理类
/// </summary>
public class GeneralHandler
{
/// <summary>
/// 处理报价单的方法(增删改查)
/// </summary>
/// <param name="quote"></param>
/// <param name="dealType"></param>
/// <returns></returns>
public QuoteHandlerReturnModel DealQuote(Quote quote, string dealType)
{
QuoteHandlerReturnModel returnModel = new QuoteHandlerReturnModel();
QuoteService quoteHandler = new QuoteService();
Type type = quoteHandler.GetType();
MethodInfo[] quoteMethodList = type.GetMethods(BindingFlags.Instance | BindingFlags.Public);
foreach (var method in quoteMethodList)
{
if (dealType.Equals(method.Name,StringComparison.OrdinalIgnoreCase))
{
returnModel=(QuoteHandlerReturnModel)method.Invoke(quoteHandler, new object[] { quote });//这里代替了那些if-else的判断(但是前提是:这些被调用的方法必须有相同的参数)
}
}

return returnModel;
}
}

/// <summary>
/// 操作数据库报价单的Service
/// </summary>
public class QuoteService
{
public QuoteHandlerReturnModel Create(Quote quote)
{
//todo:向数据库增加报价单
return new QuoteHandlerReturnModel();
}
public QuoteHandlerReturnModel Delete(Quote quote)
{
//todo:在数据库中删除一条报价单
return new QuoteHandlerReturnModel();
}
public QuoteHandlerReturnModel Update(Quote quote)
{
//todo:在数据库中更新这条报价单
return new QuoteHandlerReturnModel();
}
public QuoteHandlerReturnModel Get(Quote quote)
{
//todo:在数据库中更新这条报价单
return new QuoteHandlerReturnModel();
}
}
/// <summary>
/// 报价单Model
/// </summary>
public class Quote
{
public int Id { get; set; }
public string QuoteNo { get; set; }
public string OwnerName { get; set; }
public DateTime QuoteDate { get; set; }
}

/// <summary>
/// 所有的报价单操作的方法的返回值类型
/// </summary>
public class QuoteHandlerReturnModel
{
public bool IsSuccess { get; set; }
public Quote quote { get; set; }
}
}

最新文章

  1. 自己封装了一个EF的上下文类.,分享一下,顺便求大神指点
  2. Testing - 测试基础 - 用例
  3. 关于IHttpModule的相关知识总结
  4. [ay原创作品]用wpf写了个模仿36Kr网站登录背景的效果
  5. ORACLE添加表约束的语法示例
  6. 分享一些前端chm文档
  7. Zookeeper集群和HBase集群
  8. 简学Python第一章__进入PY的世界
  9. 【草稿】实验室新手HandBook
  10. 变量、&amp;连接、函数与过程、按下某键
  11. 代码编辑器monaco-editor之基础使用
  12. JDK7新特性&lt;八&gt;异步io/AIO
  13. DevExpress v18.1新版亮点——ASP.NET Bootstrap篇(二)
  14. 【python】实例-创建文件并通过键盘输入字符
  15. BZOJ3672: [Noi2014]购票(dp 斜率优化 点分治 二分 凸包)
  16. 知识梳理——CSS篇
  17. jdk(1.8)命令行工具(二)
  18. android studio ndk 开发入门
  19. spring,springMVC,mybatis项目添加maven后报500错
  20. sublime text3 插件CTags

热门文章

  1. 带复杂类的list,list&lt;class&gt;前台往后台传输
  2. Rsyslog日志服务搭建
  3. 如何获取app的activity
  4. ci钩子
  5. RPM 安装oracle18c 修改字符集的方法
  6. php连接池 php–cp
  7. 每个Android开发者必须知道的内存管理知识
  8. HTML标签参考手册
  9. ASP.NET Core 2 学习笔记
  10. hbase 原子操作cas