目录

Cache数据库方法的RESTful封装

================================================================

因为对web service的基础理论了解不多,所以本篇笔记仅讨论在一个已有框架中添加并封装新的表方法供前端调用,工程整体框架如果以后看懂了再补吧。

首先在Ensemble里找到名为CacheNetWizard的应用程序,该程序目的是产生一个.dll文件,通过将该.dll文件添加到程序引用中,可以实现asp.net与数据库之间的通信。

如图,连接服务器,输入用户名密码并选择命名空间,选择Assembly,语言选择C#,输出文件存放到工程目录/bin/Cache.classname.dll,选择class时父子表关系的只选择父表即可。每一次改动Ensemble里的class就要重新生成一次.dll

封装的目的是可以通过前端调用Ensemble中的方法,和其他web开发相似,在程序中由controller与前端通信。

 //Controllers/ExampleController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using SpaceBed.CommonLibrary;
using System.Web.Http.OData;
using InterSystems.Data.CacheClient;
using SpaceBed.DataModels;
using SpaceBed.Models; namespace SpaceBed.Controllers
{
[WebApiTracker]
[RESTAuthorizeAttribute]
public class ExampleController : ApiController
{
static readonly IExampleRepository repository = new ExampleRepository();
DataConnection pclsCache = new DataConnection(); /// <summary>
/// 插入数据 2016-10-13
/// </summary>
/// <param name="TestBedInfo"></param>
/// <returns></returns>
[Route("Api/v1/Example/SetExampleData")] public HttpResponseMessage SetExampleData(Example Example)
{
int ret = repository.SetExampleData(pclsCache, Example.PlanNo, Example.StartDate, Example.Status, Example.DoctorId);
return new ExceptionHandler().SetData(Request, ret);
} /// 根据主键获取除主键外所有信息 2016-10-13
///
[Route("Api/v1/Example/GetPatientPlan")] public Example GetPatientPlan(string strPlanNo)
{
return repository.GetPatientPlan(pclsCache, strPlanNo);
} /// 根据PlanNo修改Status 2016-10-14
///
[Route("Api/v1/Example/ChangeStatus")] public HttpResponseMessage ChangeStatus(string strPlanNo, int strStatus)
{
int ret = repository.ChangeStatus(pclsCache, strPlanNo, strStatus);
return new ExceptionHandler().ChangeStatus(Request, ret);
} /// 根据Status和StartDate查找所有数据 2016-10-17
///
[Route("Api/v1/Example/GetPlan")] public List<Example> GetPlan(int strStatus, int strStartDate)
{
return repository.GetPlan(pclsCache, strStatus, strStartDate);
} ///添加Detail数据 2016-10-17
///
[Route("Api/v1/Example/SetDetailData")] public HttpResponseMessage SetExampleDetailData(Detail Detail)
{
int ret = repository.SetExampleDetailData(pclsCache, Detail.Plan, Detail.Value, Detail.Instruction);
return new ExceptionHandler().SetData(Request, ret);
}
}
}

Controller调用model来实现与method的连接:

 //Models/ExampleRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web;
using SpaceBed.CommonLibrary;
using SpaceBed.DataMethod;
using SpaceBed.DataModels; namespace SpaceBed.Models
{
public class ExampleRepository : IExampleRepository
{
public int SetExampleData(DataConnection pclsCache, string PlanNo, int StartDate, int Status, string DoctorId)
{
return new ExampleMethod().SetExampleData(pclsCache, PlanNo, StartDate, Status, DoctorId);
} public Example GetPatientPlan(DataConnection pclsCache, string strPlanNo)
{
return new ExampleMethod().GetPatientPlan(pclsCache, strPlanNo);
} public int ChangeStatus(DataConnection pclsCache, string strPlanNo, int strStatus)
{
return new ExampleMethod().ChangeStatus(pclsCache, strPlanNo, strStatus);
}
public List<Example> GetPlan(DataConnection pclsCache, int strStatus, int strStartDate)
{
List<Example> Items = new List<Example>();
Items = new ExampleMethod().GetPlan(pclsCache, strStatus, strStartDate);
return Items;
} public int SetExampleDetailData(DataConnection pclsCache, string Plan, string Value, string Instruction)
{
return new ExampleMethod().SetExampleDetailData(pclsCache, Plan, Value, Instruction);
}
}
}

而model与controller之间需要接口,命名为IExampleRepository,也存放在model文件夹中即可:

 //Models/IExampleRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Text;
using System.Threading.Tasks;
using SpaceBed.CommonLibrary;
using SpaceBed.DataModels; namespace SpaceBed.Models
{
public interface IExampleRepository
{
int SetExampleData(DataConnection pclsCache, string PlanNo, int StartDate, int Status, string DoctorId);
Example GetPatientPlan(DataConnection pclsCache, string strPlanNo);
int ChangeStatus(DataConnection pclsCache, string strPlanNo, int strStatus);
List<Example> GetPlan(DataConnection pclsCache, int strStatus, int strStartDate); int SetExampleDetailData(DataConnection pclsCache, string Plan, string Value, string Instruction); }
}

Model可以整合method,在前端调用多个method时可以在此处理,但是不在本文讨论范围内。Method对应的就是Ensemble里每个class中的方法

 //DataMethod/ExampleMethod.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web;
using InterSystems.Data.CacheClient;
using SpaceBed.DataModels;
using SpaceBed.CommonLibrary; namespace SpaceBed.DataMethod
{
public class ExampleMethod
{
#region <EG.Example> gy 2016-10-13
// EG.Example表 SetData
public int SetExampleData(DataConnection pclsCache, string PlanNo, int StartDate, int Status, string DoctorId)
{
int ret = ;
try
{
if (!pclsCache.Connect())
{
return ret;
}
ret = (int)EG.Example.SetData(pclsCache.CacheConnectionObject, PlanNo, StartDate, Status, DoctorId);
return ret;
}
catch (Exception ex)
{
HygeiaComUtility.WriteClientLog(HygeiaEnum.LogType.ErrorLog, "ExampleMethod.SetExampleData", "数据库操作异常! error information : " + ex.Message + Environment.NewLine + ex.StackTrace);
return ret;
}
finally
{
pclsCache.DisConnect();
}
} // EG.Example表 GetPatientPlan
public Example GetPatientPlan(DataConnection pclsCache, string strPlanNo)
{
Example PatientPlan = new Example();
try
{
if (!pclsCache.Connect())
{
return null;
}
InterSystems.Data.CacheTypes.CacheSysList list = null;
list = EG.Example.GetPatientPlan(pclsCache.CacheConnectionObject, strPlanNo);
if (list != null)
{
PatientPlan.StartDate = Convert.ToInt32(list[]);
PatientPlan.Status = Convert.ToInt32(list[]);
PatientPlan.DoctorId = list[];
}
return PatientPlan;
}
catch (Exception ex)
{
HygeiaComUtility.WriteClientLog(HygeiaEnum.LogType.ErrorLog, "ExampleMethod.GetPatientPlan", "数据库操作异常! error information : " + ex.Message + Environment.NewLine + ex.StackTrace);
return null;
}
finally
{
pclsCache.DisConnect();
}
} // EG.Example表 ChangeStatus
public int ChangeStatus(DataConnection pclsCache, string strPlanNo, int strStatus)
{
int ret = ;
try
{
if (!pclsCache.Connect())
{
return ret;
}
ret = (int)EG.Example.ChangeStatus(pclsCache.CacheConnectionObject, strPlanNo, strStatus);
return ret;
}
catch (Exception ex)
{
HygeiaComUtility.WriteClientLog(HygeiaEnum.LogType.ErrorLog, "ExampleMethod.ChangeStatus", "数据库操作异常! error information : " + ex.Message + Environment.NewLine + ex.StackTrace);
return ret;
}
finally
{
pclsCache.DisConnect();
}
} // EG.Example表 GetPlan
public List<Example> GetPlan(DataConnection pclsCache, int strStatus, int strStartDate)
{
List<Example> items = new List<Example>();
CacheCommand cmd = null;
CacheDataReader cdr = null;
try
{
if (!pclsCache.Connect())
{
return null;
}
cmd = EG.Example.GetPlan(pclsCache.CacheConnectionObject);
cmd.Parameters.Add("Status", CacheDbType.NVarChar).Value = strStatus;
cmd.Parameters.Add("StartDate", CacheDbType.NVarChar).Value = strStartDate; cdr = cmd.ExecuteReader(); while (cdr.Read())
{
Example item = new Example();
item.PlanNo = cdr["PlanNo"].ToString();
item.StartDate = (int)cdr["StartDate"];
item.Status = (int)cdr["Status"];
item.DoctorId = cdr["DoctorId"].ToString();
items.Add(item);
}
return items;
}
catch (Exception ex)
{
HygeiaComUtility.WriteClientLog(HygeiaEnum.LogType.ErrorLog, "ExampleMethod.GetPatientPlan", "数据库操作异常! error information : " + ex.Message + Environment.NewLine + ex.StackTrace);
return null;
}
finally
{
if ((cdr != null))
{
cdr.Close();
cdr.Dispose(true);
cdr = null;
}
if ((cmd != null))
{
cmd.Parameters.Clear();
cmd.Dispose();
cmd = null;
}
pclsCache.DisConnect();
}
} #endregion #region <EG.Detail> gy 2016-10-17
//EG.Detail表 SetData
public int SetExampleDetailData(DataConnection pclsCache, string Plan, string Value, string Instruction)
{
int ret = ;
try
{
if (!pclsCache.Connect())
{
return ret;
}
ret = (int)EG.Detail.SetData(pclsCache.CacheConnectionObject, Plan, Value, Instruction);
return ret;
}
catch (Exception ex)
{
HygeiaComUtility.WriteClientLog(HygeiaEnum.LogType.ErrorLog, "ExampleMethod.SetExampleData", "数据库操作异常! error information : " + ex.Message + Environment.NewLine + ex.StackTrace);
return ret;
}
finally
{
pclsCache.DisConnect();
}
}
#endregion
} }

为了更加清晰地在前端将数据进行结构化显示和获取,使用datamodel将列表整合成类:

 //DataModels/Example.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web; namespace SpaceBed.DataModels
{
public class Example
{
private string strPlanNo = string.Empty;
private int strStartDate = ;
private int strStatus = ;
private string strDoctorId = string.Empty; public string PlanNo
{
set { strPlanNo = value; }
get { return strPlanNo; }
} public int StartDate
{
set { strStartDate = value; }
get { return strStartDate; }
} public int Status
{
set { strStatus = value; }
get { return strStatus; }
} public string DoctorId
{
set { strDoctorId = value; }
get { return strDoctorId; }
} } public class Detail
{
private string strPlan = string.Empty;
private string strValue = string.Empty;
private string strInstruction = string.Empty; public string Plan
{
set { strPlan = value; }
get { return strPlan; }
}
public string Value
{
set;
get;
}
public string Instruction
{
set;
get;
}
}
}

以上就是将Ensemble方法进行RESTful封装的全过程(数据库连接与网络配置等不在讨论范围内),封装结果大部分为API中的GET和POST方法

补充1:web.config中可以修改网络配置,包括连接的服务器、端口、命名空间、用户名密码等,新导入时要注意修改;在引用中要添加产生的.dll文件,因为要依靠.dll才能与数据库中的方法取得通信。

补充2:父子表中父表多主键时,在数据库中处理relationship时,需要将所有父表的主键属性拼接起来,拼接符号为”||”,在RESTful里可以处理,在前端也可以处理。在RESTful里处理时,从controller到method的声明都需要使用所有父表主键属性,而在method里添加一句话,将字符串拼接到一起,于此同时,调用数据库的那句函数需要使用拼接起来的那个relationship

补充3:在Ensemble里打开子表的数据条目时需要在前面拼接父表的主键

Caché数据库学习笔记(5)的更多相关文章

  1. Caché数据库学习笔记(1)

    目录: Caché的概念和基础知识 Caché数据库的安装 创建命名空间(namespace)和数据库(database) Documentation的使用 ===================== ...

  2. Caché数据库学习笔记(4)

    目录 DeepSee的使用 数据.方法等的导入与导出 ======================================================== ================ ...

  3. Caché数据库学习笔记(2)

    目录: 创建新类(表)(class文件)与创建routine(.mac  .inc) 在类里面添加函数(classmethod) Terminal的使用 ======================= ...

  4. Caché数据库学习笔记(3)

    目录 Query函数及其测试 重建索引表 Management portal简介 远程访问Ensemble ============================================== ...

  5. MySQL数据库学习笔记(十二)----开源工具DbUtils的使用(数据库的增删改查)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  6. MySQL数据库学习笔记(十)----JDBC事务处理、封装JDBC工具类

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  7. MySQL数据库学习笔记(九)----JDBC的ResultSet接口(查询操作)、PreparedStatement接口重构增删改查(含SQL注入的解释)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  8. Mysql数据库学习笔记之数据库索引(index)

    什么是索引: SQL索引有两种,聚集索引和非聚集索引,索引主要目的是提高了SQL Server系统的性能,加快数据的查询速度与减少系统的响应时间. 聚集索引:该索引中键值的逻辑顺序决定了表中相应行的物 ...

  9. MYSQL数据库学习笔记1

      MYSQL数据库学习笔记1 数据库概念 关系数据库 常见数据库软件 SQL SQL的概念 SQL语言分类 数据库操作 创建数据库 查看数据库的定义 删除数据库 修改数据库 创建表 数据类型 约束 ...

随机推荐

  1. 170103、Redis官方集群方案 Redis Cluster

    前面我们谈了Redis Sharding多服务器集群技术,Redis Sharding是客户端Sharding技术,对于服务端来说,各个Redis服务器彼此是相互独立的,这对于服务端根据需要灵活部署R ...

  2. JavaScript检测文件上传的类型与大小

    $(function(){ $("#files").change(function(){ // 添加允许上传的文件类型 var exts = new Array(); exts[0 ...

  3. 通过正则表达式实现简单xml文件解析

    这是我通过正则表达式实现的xml文件解析工具,有些XHTML文件中包含特殊符号,暂时还无法正常使用. 设计思路:常见的xml文件都是单根树结构,工具的目的是通过递归的方式将整个文档树装载进一个Node ...

  4. [linux系统]--spawn 用法

    spawn与except组合可达到远程登录设备执行命令的作用 下面是登录设备的一段代码 #!/usr/bin/expect -f user=root host=1.1.1.1 password=roo ...

  5. JavaScript中正则表达式test()、exec()、match() 方法

    转自http://www.cnblogs.com/jane-y/articles/5183859.html 1.test test 返回 Boolean,查找对应的字符串中是否存在模式.var str ...

  6. HTML5 History 模式

    vue-router 默认 hash 模式 -- 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载. 如果不想要很丑的 hash,我们可以用路由的 his ...

  7. linux解压/压缩文件

    1.*.tar 用 tar –xvf 解压  2.*.gz 用 gzip -d或者gunzip 解压  3.*.tar.gz和*.tgz 用 tar –xzf 解压  4.*.bz2 用 bzip2 ...

  8. python学习之函数

    1.函数名可以被赋值 比如: def aaa(): pass b = aaa//将函数名字赋值给b b()//跟aaa()效果一样 2.return 2.1.如果函数不写return的话,会默认返回N ...

  9. 将 List<UserInfo>中的对象按照UserInfo.name进行分组

    package yq; import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List; import ...

  10. Excel-漏斗图分析(差异分析)

    漏斗图适用于业务流程比较规范.周期长.环节多的流程分析,通过漏斗各环节业务数据的比较,能够直观地发现和说明问题所在.在网站分析中,通常用于转化率比较,它不仅能展示用户从进入网站到实现购买的最终转化率, ...