分享一个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命令就包 ...
随机推荐
- ecshop二次开发 商品分类描述编辑框
- Default Parameter Values in Python
Python’s handling of default parameter values is one of a few things that tends to trip up most new ...
- 在Visual Studio中将现有.NET Framework项目迁移至.NET Core 1.1 Preview 1
1)下载安装包含 .NET Core 1.1 Preview 1 的 SDK:Windows x64 安装包(下载地址列表) 2)下载最新 VS 2015 NuGet 插件:https://dist. ...
- 辛巴学院-Unity-剑英的c#提高篇(一)主循环
这是测试版 辛巴学院:正大光明的不务正业. 最近刚刚离开了我服务了三年多的公司,因为一个无数次碰到的老问题,没钱了. 之前不知道做什么好的时候,机缘巧合之下和哒嗒网络的吴总聊了一下,发现了vr gam ...
- 将nuget与VS直接集成,实现一键上传等功能
nuget是一个非常方便的包管理工具,很多团队为了开发的方便也建立了自己的包源网站(nuget.server),本篇文章是笔者在配置nuget上面的一点小体,其最终目标是要达到能够在VS里一键打包上传 ...
- C语言 · 查找整数
问题描述 给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个. 输入格式 第一行包含一个整数n. 第二行包含n个非负整数,为给定的数列,数列中的每个数都不大于10000. 第三行包含一个 ...
- Html5 绘制旋转的太极图
采用Html5+JavaScript在Canvas中绘制旋转的太极图,如下图所示: 具体思路和绘制逻辑,在上图中已有说明,代码如下: <script type="text/javasc ...
- linux java 版本
之前linux已经安装了1.6的版本, 我想要升级,于是安装了1.7, /etc/profile 的最后几行是这么写的: JAVA_HOME=/usr/java/jdk1.7.0_79JRE_HOME ...
- sublime3 插件
Sublime Text 3能用支持的插件推荐 从二月份用测试版本build 3012开始用sublime text 3,虽然很多插件在sublime text 3不工作了,因为sublime tex ...
- Angular $watch
如果想在某个属性发生变化的时候执行某些操作,那么scope.$watch是最佳选择 https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$w ...