http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 4338|回复: 2

[C#语言基础] 使用Dictionary替换IF和switch语言实现高效的Action分流

[复制链接]
发表于 2018-3-8 17:25:42 | 显示全部楼层 |阅读模式
使用Dictionary替换IF和switch语言实现高效的Action分流


   说到Action,对于每一个开发者来说都不是一个陌生的东西。

我们每一次前端与后台之前的沟通,或者是访问一次接口都需要有一个Action,Action的作用就是告诉服务端你要执行什么操作。

比如我们在删除一行时会用到action=deleteone
添加一行时会用到addone等方法

今天我们使用Dictionary来实现这种多Action的选择问题

通常我们会用IF或者是switch语句来实现

下面一起来看一下Dictionary是怎么实现的
[C#] 纯文本查看 复制代码
 // 摘要:
    //     表示键和值的集合。
    //
    // 类型参数:
    //   TKey:
    //     字典中的键的类型。
    //
    //   TValue:
    //     字典中的值的类型。

上面是关于Dictionary类的说明,很简单就是一个Key ,Value的静态集合。

我们来新建一个handerTest.ashx的一般处理程序
在类的最上面定义一个委托和我们需要的集合
[C#] 纯文本查看 复制代码
  delegate void myFunc(HttpContext context);
        static Dictionary<string, myFunc> dict = new Dictionary<string, myFunc>();


定义myFunc关于HttpContext 的委托仅仅是为了书写方法,大家也可以不去定义,直接使用也是可以的。

HanderTest的构造方法里添加我们所有需要的项目

[C#] 纯文本查看 复制代码
  static handerTest()
        {
            dict.Add("del", context =>
            {
                string jsonback = context.Request.Params["jsoncallback"];
                string id = context.Request["id"];
                //输出
            });
            dict.Add("mgdel", context =>
            {
                string jsonback = context.Request.Params["jsoncallback"];
                string id = context.Request["id"];
                //输出
            });
            dict.Add("address", context =>
            {
                string jsonback = context.Request["jsoncallback"];
                string proid = context.Request["proid"];
                //输出
            });
            dict.Add("logout", context => {
             
                context.Response.Redirect("/Login");
                context.Response.ContentType = "text/plain";
                context.Response.Write("1;");
            });
            dict.Add("getUserName", context => {
                string userName = string.Empty;
                string userid = LoginUser.GetLoginUserID();

                context.Response.Write(userName);
            });
        }


然呢

ProcessRequest方法书写如下,

[C#] 纯文本查看 复制代码
 public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string act = context.Request["action"];
if (!string.IsNullOrWhiteSpace(act))
{
if (dict.ContainsKey(act))
{
dict[act](context);
}

}
}



这样程序就会自动找到相应的方法去执行。
很简单的一种解决方案
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DistributedBLL.User;
using DistributedDict;
using System.Text;
using DistributedBLL;
using DistributedBLL.Mongodb.User;

namespace DistributedWeb.Hander
{
    /// <summary>
    /// hander 的摘要说明
    /// </summary>
    public class handerTest : IHttpHandler
    {
        delegate void myFunc(HttpContext context);
        static Dictionary<string, myFunc> dict = new Dictionary<string, myFunc>();
     
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string act = context.Request["action"];
            if (!string.IsNullOrWhiteSpace(act))
            {
                if (dict.ContainsKey(act))
                {
                    dict[act](context);
                }
                
            }
        }
        static handerTest()
        {
            dict.Add("del", context =>
            {
                string jsonback = context.Request.Params["jsoncallback"];
                string id = context.Request["id"];
                //输出
            });
            dict.Add("mgdel", context =>
            {
                string jsonback = context.Request.Params["jsoncallback"];
                string id = context.Request["id"];
                //输出
            });
            dict.Add("address", context =>
            {
                string jsonback = context.Request["jsoncallback"];
                string proid = context.Request["proid"];
                //输出
            });
            dict.Add("logout", context => {
             
                context.Response.Redirect("/Login");
                context.Response.ContentType = "text/plain";
                context.Response.Write("1;");
            });
            dict.Add("getUserName", context => {
                string userName = string.Empty;
                string userid = LoginUser.GetLoginUserID();

                context.Response.Write(userName);
            });
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


以上是全部代码,可以直接复制使用
这是我的分布式框架http://fenbu.sufeinet.com/中用到的一些小技巧
希望对大家有帮助


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2018-3-8 18:53:13 | 显示全部楼层
这种方式要比传统的方式更有效率
发表于 2018-3-8 20:04:13 | 显示全部楼层
楼主威武  学习ing
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-24 12:37

© 2014-2021

快速回复 返回顶部 返回列表