目录

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. winApi

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  2. Azure Web Site 之 利用Azure Web site 发布网站

    由于经常混迹于MSDN Azure论坛,少不了和一些外国朋友打交道.有的时候觉得还是有一些东西可以写出来与外国友人们分享下的, 所以就用一个开源项目建了一个英文blog项目. 在发布的时候,首选的就是 ...

  3. CSS、HTML5、JS

    [att*=value]{}包含value属性的所有元素样式.[id*=div]{} a[href$=jpg]:after{} [att^=value]{}开头字符包含value属性的所有元素样式 [ ...

  4. Dynamics AX 2012 R2 业务系列-销售业务流程

    在博文Dynamics AX R2 业务系列中,Reinhard对这个系列做了一个规划,下面我们就按照规划开始说业务吧. 1.销售的主要职责 其实这里说的职责主要是针对销售文员,并非整天外面满世界跑业 ...

  5. IE兼容性的注意点

    IE会将注释节点实现为元素,所以在IE中调用getElementsByTagName里面会包含注释节点,这个通常是不应该的 getElementById的参数在IE8及较低的版本不区分大小写 IE7及 ...

  6. javascript高级程序设计第四章 变量、作用域和内存问题

    变量包含两种,,基本类型和引用类型 基本类型是指一些简单的字段: 引用类型是☞由多个值构成的对象  引用类型的值是保存在内存中的对象,在javascript中是不允许直接访问内存中的位置; 函数的参数 ...

  7. Xcode 8 Swift 类似插件方法

    Xcode8 Swift使用技巧 1 option + cmd + / 可以弹出注释 2 color 然后加 enter 可以弹出颜色选择 3    #FIXME:  警告 4   #MARK: 备注 ...

  8. Java selenium web页面的滚动条操作

    摘录自:http://blog.csdn.net/iceryan/article/details/8162703 //移动到元素element对象的"顶端"与当前窗口的" ...

  9. LINUX安全加固规范

    1 概述 近几年来Internet变得更加不安全了.网络的通信量日益加大,越来越多的重要交易正在通过网络完成,与此同时数据被损坏.截取和修改的风险也在增加. 只要有值得偷窃的东西就会有想办法窃取它的人 ...

  10. 第一次接触OOM

    前几天机器上一直遇到cpu100%,负载很高,经常报out of memory. 今天机器又遇到了,感觉这个东西无从下手,内存不够,tree看了下cache是不是太多了. 清理了一下,其实占用的不是特 ...