简要:
我有些朋友以为YZR框架只能在webform模式下玩,其实不是的,在mvc下照样能跑.
YZR MVC:
MVC模式之中,Models模型,在这里我定义为Model实体层,不写业务逻辑.(有一部分开发人员会当成逻辑层写,实现一个"肥"的Models和一个"哑"的控制器).View视图这里不讲(Rzaor视图引擎我此前也有介绍过,感兴趣可以去了解了解),在Controller这块(具体的ActionResult,传值,还有机制这里也不多说,详细可以看回之前的相关随笔)我们讲一下框架MVC的AjaxController支持.
在MVC中,Action就相当.ashx一般处理程序,框架提供的支持使用一个AjaxController.
1.面向切面编程
实现权限,登录等等逻辑重写过滤器
2.框架对新增,修改的集合支持
3.UI层的相关参数
4.提供Request操作
#region Query<T> public T Query<T>(Enum key) { return Query<T>(key.ToString(), default(T)); } public T Query<T>(string key) { return Query<T>(key, default(T)); } public T Query<T>(string key, T defaultValue) { return WebHelper.Query<T>(key, defaultValue, false); } #endregion
5.统一处理
#region 统一处理 /// <summary> /// 统一处理:读取界面列表json数据 ===>单表 /// </summary> public virtual ActionResult GetList() { if (string.IsNullOrEmpty(TableName)) { throw new Exception("需要传递表名"); } IDataBase action = RUtility.Instance.GetDbUtility(TableName); int totalCount = 0; if (PageIndex < 1) throw new Exception("pageIndex参数不正确"); //去掉前缀 string orderField = Sort.Replace("JSON_", ""); action.ROpen(); DataTable dt = action.Select(getComBoxWhere, PageSize, PageIndex, orderField, Order, out totalCount); action.RClose(); string JsonStr = JsonHelper.CreateJsonParameters(dt, true, totalCount, "rows"); jsonResult = JsonStr; return Content(jsonResult); } /// <summary> /// 统一处理:编辑 /// </summary> public virtual ActionResult Update() { IDataBase action = RUtility.Instance.GetDbUtility(TableName); int blResult = 0; string writeMsg = "操作失败!"; TableNames result; if (Enum.TryParse<TableNames>(TableName, out result)) { SetRDic(result); } else { throw new Exception("tablename异常"); } action.ROpen(); blResult = action.Update(dic, PKey, true); action.RClose(); if (blResult > 0) { writeMsg = "更新成功!"; } else { writeMsg = "更新失败!"; } jsonResult = writeMsg; return Content(jsonResult); } /// <summary> /// 统一处理:查询一条数据 /// </summary> public virtual ActionResult SelectOne() { //IDataBase action = new RAction("SYS_ADMIN"); IDataBase action = RUtility.Instance.GetDbUtility(TableName); int id = GetID != "" ? Convert.ToInt32(GetID) : 0; List<DbParam> list = new List<DbParam>(); action.ROpen(); DataTable dt = action.SelectOne(" " + PKey + "=" + id, list); action.RClose(); string writeMsg = "操作失败!"; if (dt.Rows.Count > 0) { jsonResult = JsonHelper.CreateJsonOne(dt, null); } else jsonResult = writeMsg; return Content(jsonResult); } /// <summary> /// 统一处理:删除数据 /// </summary> public virtual ActionResult Delete() { IDataBase action = RUtility.Instance.GetDbUtility(TableName); string id = GetID != "" ? GetID : null; string writeMsg = "操作失败!"; int blResult; if (!string.IsNullOrEmpty(id)) { action.ROpen(); blResult = action.Remove(id, PKey); action.RClose(); if (blResult > 0) { writeMsg = "删除成功"; } } jsonResult = writeMsg; return Content(jsonResult); } /// <summary> /// 统一处理:增加数据 /// </summary> public virtual ActionResult Add() { IDataBase action = RUtility.Instance.GetDbUtility(TableName); int blResult = 0; string writeMsg = "操作失败!"; TableNames result; if (Enum.TryParse<TableNames>(TableName, out result)) { SetRDic(result); } action.ROpen(); blResult = action.Insert(dic); action.RClose(); if (blResult > 0) { writeMsg = "增加成功!"; } else { writeMsg = "增加失败!"; } jsonResult = writeMsg; return Content(jsonResult); } public virtual ActionResult Submit() { return Content(jsonResult); } #endregion
6.辅助方法
public void SetKeyValue(Enum key, string value) { SetKeyValue(key.ToString(), value); } public void SetKeyValue(string key, string value) { if (!keyValue.ContainsKey(key)) { //加入字典集合 keyValue.Add(key, value); } }
7.在UI层的Controller
/// <summary> /// 框架Controll类 /// </summary> public partial class HomeController : AjaxControll { // // GET: /Home/ public HomeController() { SysLogic sysLogic; sysLogic = new SysLogic(this); } }
写相关逻辑代码的地方:
public partial class HomeController { public ActionResult Index() { return View(); } public override ActionResult GetList() { //处理多表,视图操作 switch (ObjName) { default: return base.GetList(); } } public override ActionResult Submit() { //处理多表,视图操作 switch (ObjName) { default: return base.Submit(); } } public override ActionResult Add() { //处理多表,视图操作 switch (ObjName) { default: return base.Add(); break; } } public override ActionResult Update() { //处理多表,视图操作 switch (ObjName) { default: return base.Update(); break; } } public override ActionResult SelectOne() { //处理多表,视图操作 switch (ObjName) { default: return base.SelectOne(); break; } } public override ActionResult Delete() { switch (ObjName) { default: return base.Delete(); break; } } }
YZR框架是能够在MVC模式下跑起来的,YZR框架提供的封装能够在YZR MVC下使用(比如RPo,RTranscation)之外,在asp.net mvc下的技术也是能够在YZR MVC运行的.