开发者

Backgroundworker - Data Binding - View Update

开发者 https://www.devze.com 2023-03-31 16:41 出处:网络
I have a requirement to select bulk data (million rows) and then update the DB Table. Now, I am reading the data chunk by chunk in a BackgroundWorker (as the usage being specified in MSDN)

I have a requirement to select bulk data (million rows) and then update the DB Table. Now, I am reading the data chunk by chunk in a BackgroundWorker (as the usage being specified in MSDN)

I have a ViewModel class which implements INotifyPropertyChanged and all the public properties are bound to GUI via the BindingSource component - the classic开发者_运维百科 WinForms way:

Problem is that all my logic is in one ViewModel class. How do I segregate my logic in a much better way ? The data fetching and update logic is in a loop in the DoWork() method and it signals the UI via the ReportProgress method of BackgroundWorker.

EDIT - I do not have to display any kind of data on the GUI other than the progress. Its just a one time tool. Suggestions?

EDIT - Solution - http://www.codeproject.com/KB/architecture/MVPVMWindowsForms.aspx


Move all of your BackgroundWorker logic into a Model class, and make it execute work asynchronously. Supply events for notifying your ViewModel when new records are available.

In general, your ViewModels shouldn't have much knowledge of threading (other than maybe a Dispatcher.Invoke here and there). A ViewModel should merely accept input from the View, call the Model and then update the View with INotifyPropertyChanged. Not much logic should exist in the ViewModel.

You will probably also want to use ObservableCollection, but it sounds like you haven't gotten that far yet.


Solution - http://www.codeproject.com/KB/architecture/MVPVMWindowsForms.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号