行为定义引擎
设计原因: UI层 用户操作复杂 ,事件繁多,业务繁琐,控制代码粒度如果太大,重复代码太多,灵活性差,如果降低控制代码粒度, 重用性好,灵活性好,但是,方法太多,调用麻烦
设计目的:使用小粒度方法,达到调用方便灵活直观的目的
设计思路:使用通一输入输出方法签名 Action<Dictionary<string, object>, Action<object>, Action<object>> func = null; Dictionary<string, object> 是输入数据,第一个 Action<object> 是 成功后 执行方法,第二个 Action<object> 是失败后执行方法,
所有方法都使用这一签名
先构建XML状的方法树 ,引擎+模板
然后运行
这一行为定义引擎已在项目中使用 ,效果不错
行为定义引擎 代码如下
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Linq;
namespace LFRT5.clint.myControls
{
public class pageDo
{
public string name { get; set; }
public string xmlstring { get; set; }
//string neweve { get; set; }
//string newobj { get; set; }
// List<string[]> neweve = new List<string[]>();
XDocument doc = null;
public void eve(string objname, string evename, Func<string, string, Action<Dictionary<string, object>, Action<object>, Action<object>>> getfunc)
{
//try
//{
pageDoMode m = new pageDoMode();
doc = XDocument.Parse(xmlstring);
var e = geteveEle(objname, evename, doc);
m = createEve(getfunc, m, e);
if (m != null) m.dofun();
//if (neweve.Count > 0)
//{
// var newevetemp = neweve;
// neweve = new List<string[]>();
// foreach (var item in newevetemp)
// {
// var ev = item[0];
// var ob = item[1];
// eve(ob, ev, getfunc);
// }
//}
//}
//catch (Exception ee)
//{
// MessageBox.Show(ee.Message);
//}
}
private XElement geteveEle(string objname, string evename, XDocument doc)
{
var e = doc.Root.Element(name).Element(objname).Element(evename);
if (e == null)
{
e = doc.Root.Element("通用").Element(objname).Element(evename);
return null;
}
return e;
}
private pageDoMode createEve(Func<string, string, Action<Dictionary<string, object>, Action<object>, Action<object>>> getfunc, pageDoMode m, XElement xmlread)
{
var e = xmlread;
if (e.Attribute("type") != null && e.Attribute("type").Value == "neweve")
{
// string[] strs = new string[] { e.Name.LocalName, e.Attribute("objname").Value };
// neweve.Add(strs);
// return null;
e = geteveEle(e.Attribute("objname").Value, e.Name.LocalName, doc);
}
{
Action<Dictionary<string, object>, Action<object>, Action<object>> fun = null;
if (e.Attribute("type") != null && e.Attribute("type").Value == "eve")
{
fun = (invalue, ok, err) => { ok(System.DBNull.Value); };
}
else
fun = getfunc(e.Attribute("objname").Value, e.Name.LocalName);
if (fun == null) throw new Exception("fun is null " + e.Name.LocalName);
m.func = fun;
var ns = e.Nodes();
foreach (var item in ns)
{
var el = item as XElement;
if (el == null) continue;
var n = el.Name.LocalName;
switch (n)
{
case "inv":
var tns2 = el.Nodes();
foreach (var item1 in tns2)
{
var el1 = item1 as XText;
if (el1 == null || string.IsNullOrEmpty(el1.Value)) continue;
m.invalue.Add(el.Attribute("vname").Value, el1.Value);
}
break;
case "in":
var tns3 = el.Nodes();
foreach (var item1 in tns3)
{
var el1 = item1 as XElement;
if (el1 == null) continue;
var f = getfunc(el1.Attribute("objname").Value, el1.Name.LocalName);
if (f == null) continue;
m.GetValue.Add(el.Attribute("vname").Value, f);
}
break;
case "ok":
var tns = el.Nodes();
foreach (var item1 in tns)
{
var el1 = item1 as XElement;
if (el1 == null) continue;
var okm = createEve(getfunc, new pageDoMode(), el1);
if (okm != null) m.ok.Add(okm);
}
break;
case "err":
var tns1 = el.Nodes();
foreach (var item1 in tns1)
{
var el1 = item1 as XElement;
if (el1 == null) continue;
var errm = createEve(getfunc, new pageDoMode(), el1);
if (errm != null) m.err.Add(errm);
}
break;
}
}
// fun(ok, err);
}
return m;
}
}
public class pageDoMode
{
public Dictionary<string, Action<Dictionary<string, object>, Action<object>, Action<object>>> GetValue = new Dictionary<string, Action<Dictionary<string, object>, Action<object>, Action<object>>>(); //new List<Action<List<object>, Action<object>, Action<object>>>();
// public List<object> invalue = new List<object>();
public Dictionary<string, object> invalue = new Dictionary<string, object>();
public List<pageDoMode> ok = new List<pageDoMode>();
public List<pageDoMode> err = new List<pageDoMode>();
public Action<Dictionary<string, object>, Action<object>, Action<object>> func = null;
public void dofun()
{
if (GetValue != null)
{
foreach (var item in GetValue)
{
object obj = null;
item.Value(null, (o) => { obj = o; }, null);
if (!(obj is DBNull)) invalue.Add(item.Key, obj);
}
}
func(invalue,
(o) =>
{
if (ok == null || ok.Count == 0) return;
ok.ForEach((c) =>
{
if (!(o is DBNull)) { if(c.invalue.ContainsKey("ok"))c.invalue.Remove("ok"); c.invalue.Add("ok", o); }
c.dofun();
});
},
(o) =>