异步数据加载和 Windows Forms 数据绑定






4.29/5 (6投票s)
本文介绍了一个可重用库,其中包含两个用于数据绑定和异步数据加载的实用程序组件。
引言
本文介绍了我为 WinForms 应用程序开发的数据绑定和异步数据加载实用程序组件库。此处介绍的组件按“现状”提供,并不声称遵循任何标准“设计模式”。但是,它们是为了避免在 Window Forms 应用程序中实现简单绑定场景所需的重复编码工作而开发的。本文仅基于“Windows 应用程序环境”。
必备组件
本文介绍的组件使用了三个开源实现
- NHibernate (一个开源 ORM 框架)
- ObjectViews (一个开源数据绑定框架)
- NofifyWindow (CP 上由 Robert Misiak 先生发布的一个库)
我想对这些有价值的框架和库表示感谢,通过集成它们,我实现了下面文章内容中描述的小型实用组件。
背景
如果您开发过带有数据绑定的 WinForms 应用程序,您一定遇到过实现应用程序中支持的数据绑定功能的艰巨任务。当您构建比演示测试应用程序更精致的东西时,实现保存和删除数据的重复方法以及错误处理是一个枯燥且非常容易出错的过程。在我对数据绑定进行反复试验后,我开发了两个实用组件
1. 数据绑定器
它具有数据导航器所需的所有功能,以及处理保存、删除和错误处理。它还通过通知窗口提供通知。
2. 数据加载器
这是一个运行时不可见的组件,它使用后台工作进程实现异步数据加载行为,并提供一些事件通知来消耗异步操作获取的数据。
使用代码
数据绑定器
这是一个具有用户界面的组件,因此我们可以通过 Window Forms 设计器的工具箱创建新实例。要将控件添加到工具箱,请从工具箱的上下文菜单中选择“选择项”,然后浏览到附加项目已编译的库(DataBinder.dll)。将控件添加到 Windows 窗体后,您可以调用 StartProgress
和 StopProgress
方法来显示和隐藏进度条。
VB
'It will display the progress bar
databinder1.StartProgress()
'It will hide the progress bar
databinder1.StopProgress()
C#
//It will display the progress bar
databinder1.StartProgress();
//It will hide the progress bar
databinder1.StopProgress();
要将控件绑定到实现 IList
的 ICollection
的任何数据源,请使用以下语法
VB
'This must be NHibernate session, an instance of ISession
DataBinder1.DAOObject = session
'This is type of entity that needs to be persisted
DataBinder1.EntityType = GetType(User)
'It contains any collection of entities
DataBinder1.DataSource = objectList
datagrid1.DataSource = DataBinder1.BindSource
'This will enable data binding between
'UI controls and internal ObjectView instance
DataBinder1.SetBinding()
'This will enable the notification window
'for and save and delete operation
DataBinder1.ShowNotification=True
C#
//This must be NHibernate session, an instance of ISession
DataBinder1.DAOObject = session;
//This is type of entity that needs to be persisted
DataBinder1.EntityType = GetType(User);
//It contains any collection of entities
DataBinder1.DataSource = objectList;
datagrid1.DataSource = DataBinder1.BindSource;
//This will enable data binding between UI controls
//and internal ObjectView instance
DataBinder1.SetBinding();
//This will enable the notification window for and save and delete operation
DataBinder1.ShowNotification=True;
这就是让数据绑定器控件运行所需的一切。
数据加载器
此组件没有可见的用户界面,因此我们可以直接像声明其他变量一样创建实例。该组件支持泛型声明,因此任何有效的实体(POCO)都可以用来创建 DataLoader
实例;例如,
VB
Dim ldr as New DataUtils.DataLoader(Of User)()
'After creating instance of DataLoader,
'event subscription is required to catch some usefull
'events notification like Cancelled,Finished
Private Sub ldr_Cancelled() Handles ldr.Cancelled
MessageBox.Show("Operation cancelled")
DataBinder1.StopProgress()
End Sub
Private Sub ldr_Finished() Handles ldr.Finished
'Put any data binding code here
userDataBinder.StopProgress()
End Sub
Private Sub ldr_LoadError(ByVal e As System.Exception) Handles ldr.LoadError
MessageBox.Show(e.Message, "Error")
DataBinder1.StopProgress()
End Sub
C#
private DataUtils.DataLoader(Of User) ldr =
New DataUtils.DataLoader(Of User)();
//After creating instance of DataLoader,
//event subscription is required to catch some usefull
//events notification like Cancelled,Finished
ldr.Cancelled+=new CancelledEventHandler(ldr_Cancelled)
ldr.Finished+=new FinishedEventHandler(ldr_Finished)
ldr.LoadErrod+=new LoadErrorEventHandler(ldr_LoadError)
private void ldr_Cancelled() {
MessageBox.Show("Operation cancelled");
DataBinder1.StopProgress();
}
private Sub ldr_Finished() {
//Put any data binding code here
userDataBinder.StopProgress();
}
private Sub ldr_LoadError(System.Exception e) {
MessageBox.Show(e.Message, "Error");
DataBinder1.StopProgress();
}
每个数据加载器实例都需要一个 NHibernate Session 实例才能从持久化存储中获取数据。
VB
ldr.Session = session
C#
ldr.Session = session;
现在,作为最后一步,调用 FetchAll
方法,该方法将内部创建一个后台工作进程并开始异步获取数据记录。在此期间,应用程序可以显示响应式 UI,或执行其他任务。一旦数据获取完成,应用程序将根据数据检索操作的结果收到 Finished
或 LoadError
事件。
VB
ldr.FetchAll()
C#
ldr.FetchAll();
这就是运行数据加载器组件所需的一切。
虽然我已尽力保持本文在 CP 上的质量,但我仍然认为本文共享的组件不遵循任何标准设计模式,并且可能不够高效,无法提供可扩展的解决方案。无论如何,我认为它可能对某人有用,并可能避免一些重复的编码工作。
请分享您对本文的看法和评论;我将很高兴听到所有这些赞美。:)
历史
- 初次修订 - 2008 年 12 月 16 日。