序言

第一篇讲解了UI与业务逻辑分层的框架(UIMediator)的使用。本篇将说明该框架的原理及代码实现。

整体结构

UI与后台类绑定主要分为UI输入->后台属性,后台属性-UI更新两部分,为符合依赖倒置原则,分别抽象出IUIToProperty和IPropertyToUI两个接口。

为了匹配WinForm的窗体事件委托方法格式(object sender, EventArgs e)两个接口方法都实现了多态。

Mediator采用了模板方法的设计模式,实现了整个绑定方法的算法框架,子类只需实现ChangeType、BindPropertyValue、BindUIValue三个抽象方法即可。

TextBoxMediator、RadioButtonMediator、CheckBoxMediator为Mediator子类,根据各个不同的WinForm控件而实现的中介类,实现了上述三个抽象方法。

ChangeType:将Control基类转换为具体的控件类;

BindPropertyValue:实现UI输入->后台属性;

BindUIValue:实现后台属性-UI更新。

UML图如下所示。接下来讲解具体代码实现。

依赖倒置

UI输入->后台属性接口:IUIToProperty

 public interface IUIToProperty
{
void BindPropertyValue(object sender, EventArgs e);
void BindPropertyValue(PropertyInfo prop);
}

后台属性-UI更新接口:IPropertyToUI

public interface IPropertyToUI
{
void BindUIValue(object sender, EventArgs e);
void BindUIValue(PropertyInfo prop);
}

Mediator模板类

 public abstract class Mediator:IUIToProperty,IPropertyToUI
{
protected Type type;
protected object BindInstance;
protected string BindProperty; public void Bind<T>(Control control, T BindInstance, string BindProperty) where T : class ,IPropertyChange
{
this.BindInstance = BindInstance as T;
this.BindProperty = BindProperty;
type = typeof(T);
BindInstance.PropertyChanged += new EventHandler(BindUIValue);
ChangeType(control);
BindPropertyValue(null, null);
} public void BindPropertyValue(object sender, EventArgs e)
{
BindPropertyValue(GetProperty());
} private PropertyInfo GetProperty()
{
return type.GetProperties().First(c => c.Name == BindProperty);
} public void BindUIValue(object sender, EventArgs e)
{
BindUIValue(GetProperty());
} public abstract void BindPropertyValue(PropertyInfo prop);
protected abstract void ChangeType(Control control);
public abstract void BindUIValue(PropertyInfo propertyInfo);

TextBoxMediator类

public class TextBoxMediator:Mediator
{ private TextBox tb;
public override void BindPropertyValue(System.Reflection.PropertyInfo prop)
{
if (prop.PropertyType.IsValueType && string.IsNullOrEmpty(tb.Text))
{
prop.SetValue(BindInstance, , null);
return;
}
try
{
object value = Convert.ChangeType(tb.Text, prop.PropertyType);
prop.SetValue(BindInstance, value, null);
}
catch (FormatException fex)
{
throw fex;
}
catch (Exception ex)
{
throw ex;
}
} protected override void ChangeType(Control control)
{
tb = control as TextBox;
tb.TextChanged+=new EventHandler(BindPropertyValue);
} public override void BindUIValue(System.Reflection.PropertyInfo prop)
{
tb.Text = prop.GetValue(BindInstance, null).ToString();
}
}

CheckBoxMediator类

public class CheckBoxMediator:Mediator
{ private CheckBox cb;
public override void BindPropertyValue(PropertyInfo prop)
{
prop.SetValue(BindInstance, cb.Checked, null);
} protected override void ChangeType(Control control)
{
cb = control as CheckBox;
cb.CheckedChanged += new EventHandler(BindPropertyValue);
} public override void BindUIValue(PropertyInfo prop)
{
cb.Checked = Convert.ToBoolean(prop.GetValue(BindInstance, null));
} }

RadioButtonMediator类

public class RadioButtonMediator:Mediator
{
RadioButton rb;
public override void BindPropertyValue(System.Reflection.PropertyInfo prop)
{
prop.SetValue(BindInstance, rb.Checked, null); }
protected override void ChangeType(System.Windows.Forms.Control control)
{
rb = control as RadioButton;
rb.CheckedChanged += new EventHandler(BindPropertyValue);
}
public override void BindUIValue(System.Reflection.PropertyInfo prop)
{
rb.Checked = Convert.ToBoolean(prop.GetValue(BindInstance, null));
}
}

关于后台属性-UI更新的说明

分析下Mediator类中的Bind方法

 public void Bind<T>(Control control, T BindInstance, string BindProperty) where T : class ,IPropertyChange
{
this.BindInstance = BindInstance as T;
this.BindProperty = BindProperty;
type = typeof(T);
BindInstance.PropertyChanged += new EventHandler(BindUIValue);
ChangeType(control);
BindPropertyValue(null, null);
}

泛型T有一个IPropertyChange的约束,具有PropertyChanged事件,用来注册绑定BindUIValue方法。

IPropertyChange的代码如下

public interface IPropertyChange
{
event EventHandler PropertyChanged;
void UpdateUI();
}

由于.NET只支持类的单继承,为避免框架对代码的侵入性选择了接口继承。

后台类通过继承IPropertyChange,在UpdateUI实现方法中调用PropertyChanged事件。

在需要后台驱动UI更新时调用UpdateUI方法即可。

 public void UpdateUI()
{
PropertyChanged(null, null);
}

分享一个UI与业务逻辑分层的框架(二)的更多相关文章

  1. 分享一个UI与业务逻辑分层的框架(一)

    序言 .NET(C#)的WinForm如何简单易行地进行UI与业务逻辑分层?本系列文章介绍一个WinForm分层框架,该框架针对WinForm中的TextBox,CheckBox,RadioButto ...

  2. 分享一个UI与业务逻辑分层的框架(三)

    序言 前两篇讲解了UIMediator框架的使用及具体原理代码.本篇讲述MediatorManager的实现代码及展望. MediatorManager MediatorManager的作用有两点: ...

  3. 分享一个漂亮的ASP.NET MVC界面框架

    本文分享一个插件化的界面框架,该框架提供了用户.角色.权限管理功能,也提供了插件的管理和插件中心.下图是该界面框架的样式(全部源码和原理介绍下一篇分享,推荐越多,源码放的越早,呵呵). 要使用该界面框 ...

  4. 用c#开发微信 (12) 微统计 - 阅读分享统计系统 2 业务逻辑实现

    微信平台自带的统计功能太简单,有时我们需要统计有哪些微信个人用户阅读.分享了微信公众号的手机网页,以及微信个人用户访问手机网页的来源:朋友圈分享访问.好友分享消息访问等.本系统实现了手机网页阅读.分享 ...

  5. iOS开发---业务逻辑

    iOS开发---业务逻辑   1. 业务逻辑 iOS的app开发的最终目的是要让用户使用, 用户使用app完成自己的事就是业务逻辑, 业务逻辑的是最显眼开发工作.但是业务逻辑对于开发任务来说, 只是露 ...

  6. RxJava系列番外篇:一个RxJava解决复杂业务逻辑的案例

    之前写过一系列RxJava的文章,也承诺过会尽快有RxJava2的介绍.无奈实际项目中还未真正的使用RxJava2,不敢妄动笔墨.所以这次还是给大家分享一个使用RxJava1解决问题的案例,希望对大家 ...

  7. [Prodinner项目]学习分享_第三部分_Service层(业务逻辑层)

    前两节讲到怎样生成一个Model和怎样将Model映射到数据库,这一节将讲到业务逻辑层,也就是Service层. 1.Prodinner架构已经构建好的,基本的增删改查. 假设,我现在想操作第二节中讲 ...

  8. 发现 一个业务管理系统 解决了 orm 和 前端框架 剩下的 是 业务逻辑 了 。 哈

    解决了 orm 和 前端框架 剩下的 是 业务逻辑 了 . 哈 还有 各种 aop 组件 呢 . 大家 high 来 准备 用 fluent data  和 mysql 写一个 wcf 的 接口呢. ...

  9. shell脚本就是由Shell命令组成的执行文件,将一些命令整合到一个文件中,进行处理业务逻辑,脚本不用编译即可运行。它通过解释器解释运行,所以速度相对来说比较慢。

    shell脚本?在说什么是shell脚本之前,先说说什么是shell. shell是外壳的意思,就是操作系统的外壳.我们可以通过shell命令来操作和控制操作系统,比如Linux中的Shell命令就包 ...

随机推荐

  1. 如何让Maple中的数学引擎进入你的桌面应用程序和网站

    MapleNET数学服务套件将Maple 2015强大的数学引擎引入您的应用程序和网站.使用MapleNET,您可以添加数学计算和可视化功能到网页和桌面程序中,通过互联网/局域网分享“活”的Maple ...

  2. 设置Android Studio启动时可选最近打开过的工程

    Android Studio启动时,默认会打开最近关闭的工程. 如果想Android Studio在启动时,打开欢迎界面(Welcome to Android Studio界面),则可以通过设置Set ...

  3. ASP.Net请求处理机制初步探索之旅 - Part 3 管道

    开篇:上一篇我们了解了一个ASP.Net页面请求的核心处理入口,它经历了三个重要的入口,分别是:ISAPIRuntime.ProcessRequest().HttpRuntime.ProcessReq ...

  4. .NET支持多平台后的一点拙见

    我们目前对.NET的理解大部分可以归纳为:起初它是Java平台(注意是平台,不要跟Java语言搞混淆)的一个克隆品,后来慢慢演变,有了自己的特性.由于Java平台最显著的特点就是“平台独立性”(或者说 ...

  5. 【VC++技术杂谈003】打印技术之打印机状态监控

    在上一篇博文中我主要介绍了如何获取以及设置系统的默认打印机,本文将介绍如何对打印机状态进行实时监控,记录下所打印的文档.打印的份数以及打印时间等打印信息. 1.打印机虚脱机技术 在正式介绍如何对打印机 ...

  6. Html5 设置菱形链接菜单

    本例是采用html5+css3.0设置的菜单链接.其中主要用到了以下几个方面: 1. CSS3.0中的2D变换,如:旋转transform:rotate(45deg);移动,放大transform:r ...

  7. 每天一个linux命令(26):用SecureCRT来上传和下载

    用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem.文 ...

  8. Atitit 代码复用的理解attilax总结

    Atitit 代码复用的理解attilax总结 1.1. 继承1 1.1.1. 模式1:原型继承1 1.1.2. 模式2:复制所有属性进行继承 拷贝继承1 1.1.3. 模式3:混合(mix-in)1 ...

  9. PDO 数据访问抽象层

    1.操作其它数据库 (1)造对象 $dsn = "mysql:dbname=test3;host=localhost"; //数据源:两个参数:数据库驱动,链接数据库 $pdo = ...

  10. Spring学习记录(九)---通过工厂方法配置bean

    1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class C ...