目录

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. 自定义Chrome插件Vimium

    自定义快捷键 map e scrollPageUp map w removeTab map s nextTab map a previousTab map q goNext map z restore ...

  2. 七牛整合PHP上传文件

    七牛支持抓取远程图片 API,用 access_key + secret_key + url 生成 access_token, 把 access_token 加在 header 里,然后向 post ...

  3. java连接mysql步骤

    转自:http://www.cnblogs.com/hongten/archive/2011/03/29/1998311.html JDBC连接mysql数据库   •创建一个以JDBC连接数据库的程 ...

  4. [转]SSAS没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)) (Microsoft Visual Studio)的解决办法

    转自:http://www.cnblogs.com/xvqm00/archive/2011/07/15/2107338.html 打开SSAS 数据源视图浏览数据时,提示 没有注册类别 (异常来自 H ...

  5. 一个被称为世界上最短的判断IE方法

    最近偶然看到一段判断是否为IE浏览器的代码: if(!+[1,]) { console.info("IE 浏览器"); } else { console.info("非 ...

  6. 图片使用base64展示代码,后台为jfinal

    前台使用ajax获取数据,下面步骤为把图片对应的id获取到,然后判断是否为空,不为空则发送请求获取数据,数据为base64数据格式: img需要注明数据类型格式:即data:image/jpg:bas ...

  7. 用hexdump获取event的输出信息

    当我们在调试输入设备时,如:键盘,触摸屏 会使用到hexdump工具.其内容如下: 1. 键盘: # cat /dev/input/event0 | hexdump 0000000 f6a6 4e15 ...

  8. 自定义控件(View的绘制流程源码解析)

    参考声明:这里的一些流程图援引自http://a.codekk.com/detail/Android/lightSky/%E5%85%AC%E5%85%B1%E6%8A%80%E6%9C%AF%E7% ...

  9. Java的日期工具类

    package com.tech.jin.util; import java.text.DateFormat; import java.text.ParseException; import java ...

  10. 在ubuntu 14.04上安装2.6的内核

    1.到http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.32.tar.bz2这里下载最新的稳定版内核: 2.根据各自系统,安装如下软件:l b ...