在WebAPI中自动创建Controller
在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的更多相关文章
- 在Code First中自动创建Entity模型
之前我在博客文章中介绍过如何使用Code First来创建数据库,通过CodeFirst的方式,可以大幅的减少开发人员的工作量,比传统的先创建库再ORM的方式简单了不少.但是,很多时候,特别是一些MI ...
- ExtJS6的中sencha cmd中自动创建案例项目代码分析
在之前的博文中,我们按照sencha cmd的指点,在自己win7虚拟机上创建了一个案例项目,相当于创建了一个固定格式的文档目录结构,然后里面自动创建了一系列js代码.这是使用sencha cmd自动 ...
- Xcode6中如何修改文件中自动创建的Created by和Copyright
转自: http://blog.csdn.net/bjourney/article/details/46832159 在Xcode6创建问的时候,会自动生成注释 // Created byxxx o ...
- 在 Xcode中 修改文件中自动创建的Created by和Copyright
在Xcode里创建的时候,会自动生成注释 // Created byxxx on 15/7/10. // Copyright (c) 2015年 xxxx. All rights reserved ...
- 浅谈ETL架构中ODS的作用以及如何在HaoheDI中自动创建ODS表
什么是ODS表? 在ETL架构中,源数据很少会直接抽取加载到数据仓库EDW,二者之间往往会设置一个源数据的临时存储区域,存储数据在清洗转换前的原始形态,通常被大家称做操作型数据存储,简称ODS,在Ki ...
- 013_使用 user.txt 文件中的人员名单,在计算机中自动创建对应的账户并配置初始密码
for i in `cat user.txt`do useradd $i echo "123456" | passwd --stdin $idone
- 转载:性能优化——统计信息——SQLServer自动更新和自动创建统计信息选项
这段时间AX查询变得非常慢,每天都有很多锁. 最后发现是数据库统计信息需要更新. ----------------------------------------------------------- ...
- 性能优化——统计信息——SQLServer自动更新和自动创建统计信息选项
原文:性能优化--统计信息--SQLServer自动更新和自动创建统计信息选项 原文译自:http://www.mssqltips.com/sqlservertip/2766/sql-server-a ...
- [.NET] WebApi 生成帮助文档及顺便自动创建简单的测试工具
==========最终的效果图========== ==========下面开始干活:生成帮助文档========== 一.创建 WebApi 项目 二.找到 HelpPageConfig.cs 并 ...
随机推荐
- 回顾Spring框架
Spring框架: 传统JavaEE解决企业级应用问题时的"重量级"架构体系,使它的开发效率,开发难度和实际的性能都令人失望.Spring是以一个 救世主的身份降临在广大的程序员面 ...
- error CS0103: 当前上下文中不存在名称“ViewBag”
error CS0103: 当前上下文中不存在名称“ViewBag” View文件夹下缺少web.config文件
- 相同vlan之间的相互访问
- 巧用TexturePacker命令行
游戏开发使用TexturePacker来生成图片的atlas sheet, 工具非常好用. 一般GUI的方法, 新建一个tps文件, 将要图片加载进来,调整参数和输出路径, 最后点publish. 在 ...
- bootstrap-8
基本按钮: bootstrap框架V3.x版本的基本按钮和V2.x版本的基本按钮一样,都是通过类名.btn来实现,不同的是V3.x版本要简约很多,去除V2.x版本中的大量的CSS3的部分特效. 默认按 ...
- find查找命令的各种使用方法
find是文件查找工具,实时查找,速度慢,精确匹配 find命令基本使用格式 find [options] [查找路径] [查找条件] [处理动作] 查找路径:默认为当前目录 查找条件:默认为查找指定 ...
- 【转载】ANSYS有限元分析中的单位问题
原文地址:http://www.cnblogs.com/ylhome/archive/2009/02/26/1398756.html ansys中没有单位的概念,只要统一就行了.所以,很多人在使用时, ...
- Sea.js学习3——Sea.js的CMD 模块定义规范
在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(Common Module Definition) 模块定义规范.该规范明确了模块的基本书写格式和基本交互规则. 在 CMD 规范 ...
- 在命令行中通过adb shell am broadcast发送广播通知
通过命令行执行adb shell am broadcast发送广播通知. adb shell am broadcast 后面的参数有:[-a <ACTION>][-d <DATA_U ...
- url中参数以及callback后面的串
最近在写一个京东的爬虫,在模拟其http请求访问评论时,遇到http://club.jd.com/productpage/p-1419543-s-0-t-0-p-0.html?callback=jQu ...