序言

第一篇讲解了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. c = (a / b, a%b) 运算输出顺序

    #include<iostream> int main() { using namespace std; int a, b, c; a = ; b = ; c = ; printf(&qu ...

  2. 送干货,实用内联gulp插件——gulp-embed

    现在npm上有很多gulp内联工具,用于把脚本和样式内嵌到HTML页面上,之前搞项目我也在这些插件中寻觅许久,但均不满足公司项目的一个需求—— HTML上同时插入了开发(dev版,src文件夹下,比如 ...

  3. Javascript中相同Function使用多个名称

    原创文章转载请注明出处:@协思, http://zeeman.cnblogs.com   看Log4js源码有如下实现: ['Trace','Debug','Info','Warn','Error', ...

  4. upload4j安全、高效、易用的java http文件上传框架

    简介 upload4j是一款轻量级http文件上传框架,使用简单,实现高效,功能专一,摆脱传统http文件上传框架的繁琐. upload4j的诞生并不是为了解决所有上传需求,而是专注于基础通用需求. ...

  5. 【原】通过JS打开IE新tab(非Window)的解决方案

    近日项目里遇到限定在IE的tab窗口里打开新窗口的需求,结合网上的资源和亲自实践,总结以下比较可行的解决方案. 1.首先必须保证IE的设置正确.打开IE的Internet options ->G ...

  6. Mysql 备份

    MySQL数据库备份命令   备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword databasename > backupfile ...

  7. 如何用注解简化SSH框架

    一.简化代码第一步,删除映射文件,给实体类加上注解 @Entity //声明当前类为hibernate映射到数据库中的实体类 @Table(name="news") //声明tab ...

  8. iOS开发——高级技术&本地化与国际化详解

    本地化与国际化详解 效果如下:   英语:                                                                    中文: 具体实现如下: ...

  9. Android笔记——eclipse快捷键

    查看快捷键定义的地方 Window->Preferences->General->Keys. 编辑相关快捷键 1. [ALT+/] 自动提示方法 此快捷键为用户编辑的好帮手,能为用户 ...

  10. rabbitMQ第五篇:Spring集成RabbitMQ

    前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...