• Self referencing when returning chain of objects. This can be solved using a design pattern called theModel Factory.
  • We are returning all the fields from the domain model object and leaking sensitive information to the client, for example if you take a look on “Tutor” object you will notice that we are returning the “password” field which shouldn’t be leaked to API consumer. This can be solved using the Model Factory pattern.
  • Each resource returned in the response should be linked to a URI, this will simplify resources query for the client. This can be solved using the Model Factory pattern.
  • We should return HTTP status code when returning single resource, i.e if the resource was not found we should return 404 in response header, if it was found we should return 200 OK, etc…, this can be solved by returning HttpResponseMessage object.
  • Inside each method we are instantiating our repository, this operation is expensive as it includes opening connection to the database, we need to implement Dependency Injection pattern, this can be solved by using Ninject DI framework.
  • The format for JSON response objects are in Pascal Case i.e. “FirstName”, and most probably our API will be consumed in client using JavaScript, it is easier for JS developers to work with properties formatted in Camel Case “firstName”. this can be solved by configuring JSON formatters of the response.

返回对象时出现循环依赖,可以通过模型工厂模式解决。

我们返回了领域模型中所有的字段给客户端,然而有一些敏感信息不应该返回(例如:password字段),解决方案:模型工厂模式

每一个返回给客户端的资源都应该包含一个URI以便客户端查询,解决方案依旧是模型工厂模式。

对于返回单个资源,我们应当返回相应的状态码(例如:成功200,资源未找到404等),解决方案:HttpResponseMessage对象

在每个方法里我们都实例化了一个repository,这个对象包含了一些昂贵的操作(例如:数据库连接),解决方案:依赖注入模式

对于返回的Json对象格式是以“帕斯卡”风格的(例如“FirstName”),然而我们的Api有很大的可能被带有Javascript的客户端消费,对于JS开发者来说可能更适合“驼峰”风格(例如”firstName”)的数据。解决方案:配置Json格式。

asp.net web api 开发时应当注意的事项的更多相关文章

  1. Asp.Net Web API开发微信后台

    如果说用Asp.Net开发微信后台是非主流,那么Asp.Net Web API的微信后台绝对是不走寻常路. 需要说明的是,本人认为Asp.Net Web API在开发很多不同的请求方法的Restful ...

  2. 水果项目第3集-asp.net web api开发入门

    app后台开发,可以用asp.net webservice技术. 也有一种重量级一点的叫WCF,也可以用来做app后台开发. 现在可以用asp.net web api来开发app后台. Asp.net ...

  3. [目录]ASP.NET web api开发实战

    第一章:Restful web service v.s. RPC style web service 第二章:ASP.NET web api v.s. WCF v.s. ASP.NET web ser ...

  4. Asp.net Web Api开发Help Page配置和扩展

    为了方面APP开发人员,服务端的接口都应当提供详尽的API说明.但每次有修改,既要维护代码,又要维护文档,一旦开发进度紧张,很容易导致代码与文档不一致. Web API有一个Help Page插件,可 ...

  5. Asp.net Web Api开发Help Page 添加对数据模型生成注释的配置和扩展

    在使用webapi框架进行接口开发的时候,编写文档会需要与接口同步更新,如果采用手动式的更新的话效率会非常低.webapi框架下提供了一种自动生成文档的help Page页的功能. 但是原始版本的效果 ...

  6. Asp.net Web Api开发(第四篇)Help Page配置和扩展

    https://blog.csdn.net/sqqyq/article/details/52708613

  7. 基于.Net Framework 4.0 Web API开发(3):ASP.NET Web APIs 异常的统一处理Attribute 和统一写Log 的Attribute的实现

    概述:  ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但是项目,总有异常发生,本节就来谈谈API的异常 ...

  8. ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API

    在前一篇博文中,我们使用OAuth的Client Credential Grant授权方式,在服务端通过CNBlogsAuthorizationServerProvider(Authorization ...

  9. ASP.NET Web API Controller 是怎么建成的

    先看ASP.NET Web API 讯息管线: 註:为了避免图片太大以至于超过版面,上图中的「HTTP 讯息处理程序」区块省略了 HttpRoutingDispatcher 处理路由分派的部分.「控制 ...

随机推荐

  1. Jersey(1.19.1) - XML Support

    As you probably already know, Jersey uses MessageBodyWriters and MessageBodyReaders to parse incomin ...

  2. js和jQuery创建元素和把元素插入到文档中所用的方法

    js创建元素: document.createElement(" 创建的元素");   //“创建的元素”指:p ,h1,div,span........ js插入元素: docu ...

  3. AES加密解密的例子小结

    话不多说,先放上代码,一共有两个文件:AES.php(aes算法类文件)和aesDemo.php(应用实例文件),这里只贴出aesDemo.php,其他的看附件吧!aesDemo.php: 例子,   ...

  4. OC第一天-Xcode、工程组成及运行状态

    Xcode 中iOS工程模版: 1.Application类型: Master-detail Application. 可以构建树形结构导航模式应用,生成的代码中包含了导航控制器和表示图控制器.(表示 ...

  5. OC7_单词个数

    // // WordManger.h // OC7_单词个数 // // Created by zhangxueming on 15/6/17. // Copyright (c) 2015年 zhan ...

  6. PHP中的变量

    PHP中的变量 程序是由代码与数据两部分组成,数据存储在变量,变量的本质是内存中的一个存储空间.变量对应的空间有一个名子,叫变量名,变量名用于对数据进行读写. 变量的定义 在php变量名之前必须使用' ...

  7. java匿名内部类练习

    interface Inter { void method(); } class Test { //补足代码.通过匿名内部类. /* static class Inner implements Int ...

  8. From MSI to WiX, Part 4 - Features and Components by Alex Shevchuk

    Following content is directly reprinted from : http://blogs.technet.com/b/alexshev/archive/2008/08/2 ...

  9. Android Studio生成APK自动追加版本号

    转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/android-apk-studio-generated-automatically-appends-a-ve ...

  10. iOS开发基础之排序

    Objective-C 有排序的API,省了我们很多事. 主要有以下3种方法. NSComparator NSArray *unsortedArray = @[@5,@3,@8,@1,@7]; NSA ...