简体   繁体   中英

Central datacontext on WP7 Mango or not?

I'm starting to migrate one app to WP7 Mango now. One of the reasons is the database support :-)

Technically the database thing is really clear. But I'm a bit unsure where should I put my datacontext. I see primary two options:

  1. global datacontext for everything
  2. a separate datacontext instance per app feature

I would like to have some thoughts on that.

Thanks!

The demo included with the SQLite2CE tool might help you in that it gives an example of how to use datacontext and ViewModels. Visit http://sqlite2ce.codeplex.com/ and download the package, the linked articles on this page will also be a help. As a bonus, if you haven't yet converted your database from SQLite then the tool will help you with that as well.

If you take a look at the demo there's a MainViewModel (set as the datacontext of the application's MainPage.xaml) which contains an ObservableCollection for each referenced class of data. A second ViewModel is used to compile a custom set of data from the database but is still referenced though an ObservableCollection in MainViewModel. This way each control you wish to bind in XAML only needs to reference a column within the appropriate ObservableCollection object.

At the moment I'm would use multiple datacontext for the different features. I'll tried to use a single one through the singleton pattern. This caused some exceptions because you'll need to syncronize the SubmitChanges() calls - especially if different threads are used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM