分享一个UI与业务逻辑分层的框架(二)
序言
第一篇讲解了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与业务逻辑分层的框架(二)的更多相关文章
- 分享一个UI与业务逻辑分层的框架(一)
序言 .NET(C#)的WinForm如何简单易行地进行UI与业务逻辑分层?本系列文章介绍一个WinForm分层框架,该框架针对WinForm中的TextBox,CheckBox,RadioButto ...
- 分享一个UI与业务逻辑分层的框架(三)
序言 前两篇讲解了UIMediator框架的使用及具体原理代码.本篇讲述MediatorManager的实现代码及展望. MediatorManager MediatorManager的作用有两点: ...
- 分享一个漂亮的ASP.NET MVC界面框架
本文分享一个插件化的界面框架,该框架提供了用户.角色.权限管理功能,也提供了插件的管理和插件中心.下图是该界面框架的样式(全部源码和原理介绍下一篇分享,推荐越多,源码放的越早,呵呵). 要使用该界面框 ...
- 用c#开发微信 (12) 微统计 - 阅读分享统计系统 2 业务逻辑实现
微信平台自带的统计功能太简单,有时我们需要统计有哪些微信个人用户阅读.分享了微信公众号的手机网页,以及微信个人用户访问手机网页的来源:朋友圈分享访问.好友分享消息访问等.本系统实现了手机网页阅读.分享 ...
- iOS开发---业务逻辑
iOS开发---业务逻辑 1. 业务逻辑 iOS的app开发的最终目的是要让用户使用, 用户使用app完成自己的事就是业务逻辑, 业务逻辑的是最显眼开发工作.但是业务逻辑对于开发任务来说, 只是露 ...
- RxJava系列番外篇:一个RxJava解决复杂业务逻辑的案例
之前写过一系列RxJava的文章,也承诺过会尽快有RxJava2的介绍.无奈实际项目中还未真正的使用RxJava2,不敢妄动笔墨.所以这次还是给大家分享一个使用RxJava1解决问题的案例,希望对大家 ...
- [Prodinner项目]学习分享_第三部分_Service层(业务逻辑层)
前两节讲到怎样生成一个Model和怎样将Model映射到数据库,这一节将讲到业务逻辑层,也就是Service层. 1.Prodinner架构已经构建好的,基本的增删改查. 假设,我现在想操作第二节中讲 ...
- 发现 一个业务管理系统 解决了 orm 和 前端框架 剩下的 是 业务逻辑 了 。 哈
解决了 orm 和 前端框架 剩下的 是 业务逻辑 了 . 哈 还有 各种 aop 组件 呢 . 大家 high 来 准备 用 fluent data 和 mysql 写一个 wcf 的 接口呢. ...
- shell脚本就是由Shell命令组成的执行文件,将一些命令整合到一个文件中,进行处理业务逻辑,脚本不用编译即可运行。它通过解释器解释运行,所以速度相对来说比较慢。
shell脚本?在说什么是shell脚本之前,先说说什么是shell. shell是外壳的意思,就是操作系统的外壳.我们可以通过shell命令来操作和控制操作系统,比如Linux中的Shell命令就包 ...
随机推荐
- Third Day:正式编程第三天,学习实践内容TextView跑马灯、AutoCompleteTextView、multiAutoCompleteTextView以及ToggleButton、checkedBox、RadioButton等相关实践
2.针对Focused的TextView跑马灯(文字较多一行无法显示)效果 针对单个TextView的跑马灯效果,可直接在TextView控件参数中添加三个属性: android:singleLine ...
- Entity Framework扩展库
这个Entity Framework扩展完全支持EF 5.0/6.0,项目地址 https://github.com/loresoft/EntityFramework.Extended,这个库支持批量 ...
- 业务安全通用解决方案——WAF数据风控
业务安全通用解决方案——WAF数据风控 作者:南浔@阿里云安全 “你们安全不要阻碍业务发展”.“这个安全策略降低用户体验,影响转化率”——这是甲方企业安全部门经常听到合作团队抱怨.但安全从业者加入公司 ...
- UI控件(UISlider)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UISlider* slider = [[UISli ...
- Spring Rabbitmq HelloWorld实例
之前的博客和大家分享了Rabbitmq的基本框架,及其工作原理,网址为 < http://www.cnblogs.com/jun-ma/p/4840869.html >.今天呢,想和大家一 ...
- C#中Finally的一个不太常见的用法
最近在看.net BCL 传送门 的源码. 在 System.Collections.Concurrent.ConcurrentQueue 中看到一段有意思的代码.注意这段代码是写在Concurren ...
- 关于大型网站技术演进的思考(二十)--网站静态化处理—web前端优化—中(12)
Web前端很多优化原则都是从如何提升网络通讯效率的角度提出的,但是这些原则使用的时候还是有很多陷阱在里面,如果我们不能深入理解这些优化原则背后所隐藏的技术原理,很有可能掉进这些陷阱里,最终没有达到最佳 ...
- 七天学会ASP.NET MVC (三)——ASP.Net MVC 数据处理
第三天我们将学习Asp.Net中数据处理功能,了解数据访问层,EF,以及EF中常用的代码实现方式,创建数据访问层和数据入口,处理Post数据,以及数据验证等功能. 系列文章 七天学会ASP.NET M ...
- WCF basicHttpBinding之Transport Security Mode, clientCredentialType="None"
原创地址:http://www.cnblogs.com/jfzhu/p/4071342.html 转载请注明出处 前面文章介绍了<WCF basicHttpBinding之Message Sec ...
- ajax的使用:例题、ajax的数据处理
需要注意的是,调用的封装的数据库,和jQuery的保存地址 一.注册 (1)写文本框来进行用户名的验证 <input type="text" id="uid&quo ...