在ASP.NET Web API中使用OData的单例模式
从OData v4开始增加了对单例模式的支持,我们不用每次根据主键等来获取某个EDM,就像在C#中使用单例模式一样。实现方式大致需要两步:
1、在需要实现单例模式的导航属性上加上[Singleton]特性
2、在EDM配置的时候使用builder.Singleton<SomeModel>("SomeModels")来创建SingletonConfiguration<SomeModel>
首先还是从模型开始。
public class Employee
{
public int ID { get; set; }
public string Name { get; set; } [Singleton]
public Company Company { get; set; }
} public enum CompanyCategory
{
IT = ,
Communication = ,
Electronics = ,
Others =
} public class Company
{
public int ID { get; set; }
public string Name { get; set; }
public Int64 Revenue { get; set; }
public CompanyCategory Category { get; set; }
public List<Employee> Employees { get; set; }
}
以上,Company和Employee存在1对多关系,我们在Employee的Compnay导航属性上加上了[Singleton]特性,也就意味着我们希望在Company上使用单例模式。
然后就在WebApiConfig中配置如下:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
... config.MapODataServiceRoute("ODataRoute", "odata", GetEdmModel());
} public static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); EntitySetConfiguration<Employee> employeesConfiguration = builder.EntitySet<Employee>("Employees");
EntityTypeConfiguration<Employee> employeeTypeConfiguration = employeesConfiguration.EntityType;
employeeTypeConfiguration.Action("ResetDataSource"); SingletonConfiguration<Company> companiesConfiguration = builder.Singleton<Company>("Umbrella");
companiesConfiguration.EntityType.Action("ResetDataSource");
companiesConfiguration.EntityType.Function("GetEmployeesCount").Returns<int>(); builder.Namespace = "Hello"; return builder.GetEdmModel();
}
}
以上,builder.Singleton<Company>("Umbrella")方法创建SingletonConfiguration<Company>类型的实例,这是EDM实现单例的方式。
Company对应的控制器UmbrellaController
再来看Company对应的控制器,大致如下:
public class UmbrellaController : ODataController
{
public static Company Umbrella; static UmbrellaController()
{
InitData();
} private static void InitData()
{
Umbrella = new Company()
{
ID = ,
Name = "Umbrella",
Revenue = ,
Category = CompanyCategory.Communication,
Employees = new List<Employee>()
};
} ... [HttpPost]
public IHttpActionResult ResetDataSourceOnCompany()
{
InitData();
return StatusCode(HttpStatusCode.NoContent);
} public IHttpActionResult GetEmployeesCount()
{
return Ok(Umbrella.Employees.Count);
}
}
以上,UmbrellaController提供的静态Company类型的Umbrella可以在全局获取。ResetDataSourceOnCompany对应配置单例EDM的companiesConfiguration.EntityType.Action("ResetDataSource")的Action,GetEmployeesCount对应配置单例EDM的companiesConfiguration.EntityType.Function("GetEmployeesCount").Returns<int>()的Function。
● 查询
[EnableQuery]
public IHttpActionResult Get()
{
return Ok(Umbrella);
} public IHttpActionResult GetRevenueFromCompany()
{
return Ok(Umbrella.Revenue);
} public IHttpActionResult GetName()
{
return Ok(Umbrella.Employees);
}
以上,GetRevenueFromCompany和GetName分别获取属性,要符合惯例,即"Get+属性名称"。
● 添加
public IHttpActionResult Put(Company newCompany)
{
Umbrella = newCompany;
return StatusCode(HttpStatusCode.NoContent);
}
● Patch
public IHttpActionResult Patch(Delta<Company> item)
{
item.Patch(Umbrella);
return StatusCode(HttpStatusCode.NoContent);
}
● 创建Company上的Employees关系
/// <summary>
/// 创建Company上Employees的关系
/// </summary>
/// <param name="navigationProperty"></param>
/// <param name="link">Empolyee的uri地址</param>
/// <returns></returns>
[AcceptVerbs("POST")]
public IHttpActionResult CreateRef(string navigationProperty, [FromBody] Uri link)
{
//获取Employee的外键
int employeeId = HelperFunction.GetKeyValue<int>(link);
Employee employee = EmployeesController.Employees.First(x => x.ID == employeeId); if(employee == null || navigationProperty!="Employees")
{
return BadRequest();
} if(Umbrella.Employees == null)
{
Umbrella.Employees = new List<Employee>() { employee};
}
else
{
Umbrella.Employees.Add(employee);
}
return StatusCode(HttpStatusCode.NoContent);
}
其实就是往Company的Employees集合导航属性中添加一个元素。其中,HelperFunction.GetKeyValue<int>()方法用来获取link中Empoyee的主键。如下:
public static class HelperFunction
{
//获取主键值
public static TKey GetKeyValue<TKey>(Uri uri)
{
if(uri ==null)
{
throw new ArgumentException("uri");
} var rootPath = uri.AbsoluteUri.Substring(, uri.AbsoluteUri.LastIndexOf('/') + );
var odataUriParser = new ODataUriParser(WebApiConfig.GetEdmModel(), new Uri(rootPath), uri);
var odataPath = odataUriParser.ParsePath();
var keySegment = odataPath.LastSegment as KeySegment;
if(keySegment==null)
{
throw new InvalidOperationException("The link does not contain a key");
}
return (TKey)keySegment.Keys.First().Value;
}
}
● 删除Company上的Employees关系
/// <summary>
/// 删除关系
/// </summary>
/// <param name="relatedKey">Employee的主键</param>
/// <param name="navigationProperty"></param>
/// <returns></returns>
public IHttpActionResult DeleteRef(string relatedKey, string navigationProperty)
{
int key = int.Parse(relatedKey);
Employee employee = Umbrella.Employees.First(x => x.ID == key); if(navigationProperty != "Employees")
{
return BadRequest();
} Umbrella.Employees.Remove(employee);
return StatusCode(HttpStatusCode.NoContent);
}
其实就是删除Company的集合属性Employees中的一个Employee元素。
● 往Company的Employees集合里添加一个Employee元素
/// <summary>
/// 从Compnay处添加某个Employee
/// </summary>
/// <param name="employee"></param>
/// <returns></returns>
[HttpPost]
public IHttpActionResult PostToEmployees([FromBody] Employee employee)
{
EmployeesController.Employees.Add(employee);
if(Umbrella.Employees == null)
{
Umbrella.Employees = new List<Employee>() { employee };
}
else
{
Umbrella.Employees.Add(employee);
}
return Created(employee);
}
EmployeesController不详诉
public class EmployeesController : ODataController
{
public static List<Employee> Employees; static EmployeesController()
{
InitData();
} private static void InitData()
{
Employees = Enumerable.Range(, ).Select(i =>
new Employee()
{
ID = i,
Name = string.Format("Name {0}", i)
}).ToList();
} [EnableQuery]
public IHttpActionResult Get()
{
return Ok(Employees.AsQueryable());
} [EnableQuery]
public IHttpActionResult Get(int key)
{
return Ok(Employees.Where(e => e.ID == key));
} public IHttpActionResult GetCompanyFromEmployee([FromODataUri] int key)
{
var company = Employees.First(e => e.ID == key).Company;
if(company==null)
{
return StatusCode(HttpStatusCode.NotFound);
}
return Ok(company);
} public IHttpActionResult Post([FromBody] Employee employee)
{
Employees.Add(employee);
return Created(employee);
} [AcceptVerbs("PUT")]
public IHttpActionResult CreateRef([FromODataUri] int key, string navigationProperty, [FromBody] Uri link)
{
if(navigationProperty!="Company")
{
return BadRequest();
}
Employees.First(e => e.ID == key).Company = UmbrellaController.Umbrella;
return StatusCode(HttpStatusCode.NoContent); } public IHttpActionResult DeleteRef([FromODataUri] int key, string navigationProperty)
{
if(navigationProperty!="Company")
{
return BadRequest();
} Employees.First(e => e.ID == key).Company = null;
return StatusCode(HttpStatusCode.NoContent);
} public IHttpActionResult PutToCompany(int key, Company company)
{
var navigateCompany = Employees.First(e => e.ID == key).Company;
Employees.First(e => e.ID == key).Company = company;
if(navigateCompany.Name == "Umbrella")
{
//体现Singleton
UmbrellaController.Umbrella = navigateCompany;
}
else
{
return BadRequest();
}
return StatusCode(HttpStatusCode.NoContent);
} public IHttpActionResult PatchToCompany(int key, Delta<Company> company)
{
var navigateCompan = Employees.First(e => e.ID == key).Company;
company.Patch(Employees.First(e => e.ID == key).Company); if(navigateCompan.Name == "Umbrella")
{
company.Patch(UmbrellaController.Umbrella);
}
else
{
return BadRequest();
}
return StatusCode(HttpStatusCode.NoContent);
} [HttpPost]
public IHttpActionResult ResetDataSourceOnCollectionOfEmployee()
{
InitData();
return Ok();
}
}
在ASP.NET Web API中使用OData的单例模式的更多相关文章
- ASP.NET Web API中使用OData
在ASP.NET Web API中使用OData 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在ASP.NET Web API中,对于CRUD(creat ...
- 在ASP.NET Web API中使用OData
http://www.alixixi.com/program/a/2015063094986.shtml 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在A ...
- 在ASP.NET Web API中使用OData的Action和Function
本篇体验OData的Action和Function功能.上下文信息参考"ASP.NET Web API基于OData的增删改查,以及处理实体间关系".在本文之前,我存在的疑惑包括: ...
- [翻译]在ASP.NET Web API中通过OData支持查询和分页
OData可以通过形如http://localhost/Products?$orderby=Name这样的QueryString传递查询条件.排序等.你可以在任何Web API Controller中 ...
- 在ASP.NET Web API中使用OData的Containment
通常情况下,一个OData的EDM(Entity Data Model)在配置的时候定义了,才可以被查询或执行各种操作.比如如下: builder.EntitySet<SomeModel> ...
- ASP.NET Web API中的Controller
虽然通过Visual Studio向导在ASP.NET Web API项目中创建的 Controller类型默认派生与抽象类型ApiController,但是ASP.NET Web API框架本身只要 ...
- ASP.NET Web API 中的异常处理(转载)
转载地址:ASP.NET Web API 中的异常处理
- 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化
谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...
- Asp.Net Web API 2第十三课——ASP.NET Web API中的JSON和XML序列化
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文描述ASP.NET W ...
随机推荐
- C语言字节对齐 __align(),__attribute((aligned (n))),#pragma pack(n)【转】
转自:https://www.cnblogs.com/ransn/p/5081198.html 转载地址 : http://blog.csdn.net/21aspnet/article/details ...
- C# Json To Object 无废话
json字符串如下: { success : 0, errorMsg : "错误消息", data : { total : "总记录数", rows : [ { ...
- 002_分布式搜索引擎Elasticsearch的查询与过滤
一.写入 先来一个简单的官方例子,插入的参数为-XPUT,插入一条记录. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 curl -XPUT 'http:/ ...
- yum安装软件报错:curl#6 - "Could not resolve host: mirrorlist.centos.org; Temporary failure in name resolut
# yum install -y epel-release Loaded plugins: fastestmirror Repository base is listed more than once ...
- nginx + php + centos 6.3
2014年2月7日 22:34:52 PHP 5.5.9 http://cn2.php.net/distributions/php-5.5.9.tar.bz2 nginx 1.5.10 http:// ...
- spring动态加载(刷新)配置文件 [复制链接]
待验证 在程序开发时,通常会经常修改spring的配置文件,不得不重启tomcat来加载spring配,费时费力.如果能在不重启tomcat的情况下,手动动态加载spring 配置文件,动态重启读取s ...
- https协议通信过程
https协议通信过程 我们都知道HTTPS能够加密信息,以免敏感信息被第三方获取.所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议. HTTPS简介 HTTPS其实是有两部分组 ...
- zabbix agent配置文件记录
一.无论主动还是被动模式都要配置server和linstenPort 二.若要设置主动模式那么要添加ServerActive,若不添加则默认为被动模式
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- conda设置Python虚拟环境
conda设置Python虚拟环境 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Co_zy/article/details/7741261 ...