在MIS系统中,大部分的操作都是基本的CRUD,并且这样的Controller非常多。

为了复用代码,我们常常写一个泛型的基类。

public class EntityController<T> : ApiController
    {
        public
IQueryable<T> GetAll()
        {
            ...
        }

public
T Get(int id)
        {
            ...
        }

public
T Put(int id, Ink ink)
        {
            ...
        }

public
T Post(Ink ink)
        {
            ...
        }

public
void Delete(int id)
        {
            ...
        }
    }

当增加一种类型的时候,我们需要手动增加一个子类:

public
class
InkController : EntityController<Ink>
    {
    }

public
class
PenController : EntityController<Pen>
    {
    }

当实体模型比较多的时候仍然就存在繁琐和难以维护的问题。因此我们也需要一种自动创建的机制,

要实现自动创建Controller,首先得把现在这种静态创建Controller的方式改成动态创建的方式。在WebAPI中,我们可以通过替换IHttpControllerSelector来实现动态创建Controller。

首先我们实现自己的IHttpControllerSelector。

public
class
MyControllerSelector : DefaultHttpControllerSelector
    {
        private
readonly
HttpConfiguration _configuration;

public MyControllerSelector(HttpConfiguration configuration)
            : base(configuration)
        {
            _configuration = configuration;
        }

public
override
HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            var controllerName = base.GetControllerName(request);
            return
new
HttpControllerDescriptor(_configuration, controllerName, typeof(Controllers.EntityController<Ink>));
        }
    }

然后在初始化函数中注册我们的ControllerSelector

public
static
class
WebApiConfig
{
public
static
void Register(HttpConfiguration config)
{
            // Web API configuration and services
            GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new
MyControllerSelector(GlobalConfiguration.Configuration));

// Web API routes
            config.MapHttpAttributeRoutes();
            ……
}
}

这样一来,即使没有子类InkController,我们同样可以特化泛型控制器EntityController<Ink>实现同样的效果。

到这一步后,还存在一个问题:ControllerSelector只能根据HttpRequest获取ControllerName,并不知道其对应的Model,不知道该如何特化EntityController。解决这个问题的常见的方式就是维护一张Controller名称和实体类型的映射表:

public
class
MyControllerSelector : DefaultHttpControllerSelector
    {
        static
Dictionary<string, Type> _entityMap;
        static MyControllerSelector()
        {
            _entityMap = new
Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);

_entityMap["Ink"] = typeof(Ink);
            _entityMap["Pen"] = typeof(Pen);
        }

private
readonly
HttpConfiguration _configuration;

public MyControllerSelector(HttpConfiguration configuration)
            : base(configuration)
        {
            _configuration = configuration;
        }

public
override
HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            var controllerName = base.GetControllerName(request);

Type entityType = null;
            if (!_entityMap.TryGetValue(controllerName.ToLower(), out entityType))
            {
                return
base.SelectController(request);
            }

return
new
HttpControllerDescriptor(_configuration, controllerName, typeof(Controllers.EntityController<>).MakeGenericType(entityType));
        }
    }

虽然这样做本身没有什么问题。这种手动维护Controller列表的方式仍然无法达到自动创建Controller的要求,因此我们还需要一种自动生成这种映射表的机制。这里我仍然是采用同前文一样的Attribute+反射的方式。

首先写一个ControllerAttribute,

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public
class
ControllerAttribute : Attribute
    {
        public
string Name { get; private
set; }

public ControllerAttribute(string name)
        {
            this.Name = name;
        }
    }

然后,在数据模型中标记该Attribute

[Controller("Pen")]
    public
class
Pen

[Controller("Ink")]
    public
class
Ink

最后,根据反射建立Controller名称和类型的关联关系

static
Dictionary<string, Type> _entityMap;
    static MyControllerSelector()
    {
        var assembly = typeof(MyControllerSelector).Assembly;

var entityTypes = from type in assembly.GetTypes()
                            let controllerAtt = type.GetCustomAttribute<ControllerAttribute>()
                            where controllerAtt != null
                            select
new { Type = type, ControllerName = controllerAtt.Name };

_entityMap = entityTypes.ToDictionary(i => i.ControllerName, i => i.Type, StringComparer.OrdinalIgnoreCase);
    }

这样基本上就可以用了。最后顺手做一下优化,减少的HttpControllerDescriptor创建操作,最终版本的ControllerSelector如下。

public
class
MyControllerSelector : DefaultHttpControllerSelector
    {
        private
Dictionary<string, HttpControllerDescriptor> _controllerMap;

public MyControllerSelector(HttpConfiguration configuration)
            : base(configuration)
        {
            var entityTypes = from type in
typeof(MyControllerSelector).Assembly.GetTypes()
                             let controllerAtt = type.GetCustomAttribute<ControllerAttribute>()
                             where controllerAtt != null
                             select
new { Type = type, ControllerName = controllerAtt.Name };

_controllerMap = entityTypes.ToDictionary(
                                i => i.ControllerName,
                                i => new
HttpControllerDescriptor(configuration, i.ControllerName,
                                        typeof(Controllers.EntityController<>).MakeGenericType(i.Type)),
                                StringComparer.OrdinalIgnoreCase);
        }

public
override
HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            HttpControllerDescriptor controllerDescriptor = null;
            if (!_controllerMap.TryGetValue(base.GetControllerName(request), out controllerDescriptor))
            {
                return
base.SelectController(request);
            }
            else
            {
                return controllerDescriptor;
            }
        }
    }

在WebAPI中自动创建Controller的更多相关文章

  1. 在Code First中自动创建Entity模型

    之前我在博客文章中介绍过如何使用Code First来创建数据库,通过CodeFirst的方式,可以大幅的减少开发人员的工作量,比传统的先创建库再ORM的方式简单了不少.但是,很多时候,特别是一些MI ...

  2. ExtJS6的中sencha cmd中自动创建案例项目代码分析

    在之前的博文中,我们按照sencha cmd的指点,在自己win7虚拟机上创建了一个案例项目,相当于创建了一个固定格式的文档目录结构,然后里面自动创建了一系列js代码.这是使用sencha cmd自动 ...

  3. Xcode6中如何修改文件中自动创建的Created by和Copyright

    转自: http://blog.csdn.net/bjourney/article/details/46832159 在Xcode6创建问的时候,会自动生成注释 //  Created byxxx o ...

  4. 在 Xcode中 修改文件中自动创建的Created by和Copyright

    在Xcode里创建的时候,会自动生成注释 //  Created byxxx on 15/7/10. //  Copyright (c) 2015年 xxxx. All rights reserved ...

  5. 浅谈ETL架构中ODS的作用以及如何在HaoheDI中自动创建ODS表

    什么是ODS表? 在ETL架构中,源数据很少会直接抽取加载到数据仓库EDW,二者之间往往会设置一个源数据的临时存储区域,存储数据在清洗转换前的原始形态,通常被大家称做操作型数据存储,简称ODS,在Ki ...

  6. 013_使用 user.txt 文件中的人员名单,在计算机中自动创建对应的账户并配置初始密码

    for i in `cat user.txt`do    useradd $i    echo "123456" | passwd --stdin $idone

  7. 转载:性能优化——统计信息——SQLServer自动更新和自动创建统计信息选项

    这段时间AX查询变得非常慢,每天都有很多锁. 最后发现是数据库统计信息需要更新. ----------------------------------------------------------- ...

  8. 性能优化——统计信息——SQLServer自动更新和自动创建统计信息选项

    原文:性能优化--统计信息--SQLServer自动更新和自动创建统计信息选项 原文译自:http://www.mssqltips.com/sqlservertip/2766/sql-server-a ...

  9. [.NET] WebApi 生成帮助文档及顺便自动创建简单的测试工具

    ==========最终的效果图========== ==========下面开始干活:生成帮助文档========== 一.创建 WebApi 项目 二.找到 HelpPageConfig.cs 并 ...

随机推荐

  1. JS运动基础(四) 碰撞运动

    碰撞运动撞到目标点,速度反转无重力的漂浮Div速度反转滚动条闪烁的问题过界后直接拉回来 加入重力反转速度的同时,减小速度纵向碰撞,横向速度也减小横向速度小数问题(负数) <!DOCTYPE HT ...

  2. 12-4mysql 查询

    简单查询select * from 表名; 注意:*代表所有); 查询指定列 select 列名,列名 from 表名 修改结果集的列名select 列名 as'',列名 as'' from 表名 条 ...

  3. Android 学习第6课,循环功能

    package ch02; public class jiujiuchengfa { public static void main(String[] args) { // TODO 自动生成的方法存 ...

  4. C# 获取MAC地址

    /********************************************************************** * C# 获取MAC地址 * 说明: * 在C#中获取本 ...

  5. Linux版网易云音乐播放音乐时无限显示“网络错误”的解决办法

    安装 gstreamer0.10-plugins-good debian类系统: -plugins-good

  6. 20145113 实验二 Java面向对象程序设计

    20145113 实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 1.初 ...

  7. ✡ leetcode 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) --------- java

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  8. PHP代码标识

    1. Echo语句(打印) <?php echo "想学习PHP么"; ?> 2. 计算表达式 <?php echo 12*3; ?> 3. 字符串 < ...

  9. Android——什么是3G

    第三代数字通讯技术(3id Generation) 3G与2G的主要区别是:在传输声音和数据的速度上的提升. 1995年问世的第一代模拟制式手机1G只能进行语音通话. 1996年出现的第二代GSM C ...

  10. px和em区别-在font-size的 css 的使用

    px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的. em是相对长度单位.相对于当前对象内文本的字体尺寸,多理解父级设定font-size的尺寸.如当前对行内文本的字体尺寸未 ...