分享一个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命令就包 ...
随机推荐
- angularjs provider 供应商服务
今天学习了angularjs的provider的供应商服务,写了个例子(自定义供应商服务,也可使用angularjs内部提供的服务) var starterApp = angular.module(' ...
- 如何在Windows 2003+IIS6的环境下找回应用程序池(application pool)中的服务账号密码
上一篇文章说了说如何在Win2008+iis7中取出SharePoint管理账号密码的方法. 整个过程简单的讲,就是通过使用要找回密码的账号用来在SharePoint中创建一个临时的Web Appli ...
- PHP的日期和时间处理函数
1. 将日期和时间转变为时间戳 1.1 time() 原型:time(void) 作用:返回当前时间的 UNIX时间戳. 参数:void,可选(即无参数) 1.2 mktime() 原型:int mk ...
- 火狐下white-space: nowrap对float的影响
- 剑指offer编程题java实现(正在更新)
面试题三:查找二维数组中元素问题 public static void main(String[] args){ int[][] num = {{1,2,8,9},{2,4,9,12},{4,7,10 ...
- Response.StatusCode的HTTP状态代码列表
1xx - 信息提示这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个 1xx 响应. · 100 - Continue 初始的请求已经接受,客户应当继续发送请求的其余部分.( ...
- 利用JNI技术在Android中调用、调试C++代码
参考:http://blog.micro-studios.com/?p=4212 代码:http://pan.baidu.com/s/1sjukSDf
- CocoaPods pod install
加参数可以提升更新的速度 方法1: pod install --verbose --no-repo-update pod update --verbose --no-repo-update 方法2: ...
- .NET中那些所谓的新语法之三:系统预定义委托与Lambda表达式
开篇:在上一篇中,我们了解了匿名类.匿名方法与扩展方法等所谓的新语法,这一篇我们继续征程,看看系统预定义委托(Action/Func/Predicate)和超爱的Lambda表达式.为了方便码农们,. ...
- Web前端开发工程师养成计划【转载】
Web前端开发工程师养成计划(入门篇) 最原始的忠告:这个世界上有想法的人很多,但是有想法又能实现它的人太少! 首先要感谢伟大的Web2.0概念.产品概念.用户体验概念.jQuery插件,是它们在中国 ...