Desktop application using a database ... why is it so hard to meet client expectations?
I'm developing a desktop application in WPF, application that is manipulating a considerable amount of data stored in a database. The application presents that data using diagrams and grids.
I chose to access the data using Entity Framework (code first). It is easy to use and the lazy loading is something I was looking for. Searching the NET, I discovered that Entity Framework seems to be designed to be used in a "Unit Of Work" strategy.
Lets take 2 simple expectations from my client:
- I want to print my data and be informed of the print progress.
- I want to generate a PDF report and be informed of the generation progress.
The client is seeing grids so he is expecting something closed to Excel.
- Printing and report generation should take into account all modifications made to the data.
- The modifications are not valid until saved by the user.
For instance, open excel, modify a worksheet, print it and close the application discarding changes.
- All worksheet was printed, including not saved data
- If I reopen the file, the unsaved data is no more there.
Assuming that I open a DBContext in the UI thread to manipulate data and accumulate changes and that I want to do the export in another thread because it is time consuming (a new DBContext), my changes to the data are not taken into account, except if I save the data.
So, what should I do ...
- Load the data from the database to XML files on disk and work on these files and put back the data on the database when saving?
- Use a lot of imagination to put the DBContext in its own thread and make loading and lazy loading redirected to the DBContext thread?
- Always save data in the DB, but when an "unsaved" flag or something like that?
Any suggestions are welcomed.
I would have the ViewModel expose an observable collection that gets loaded asynchrnosously so as to not block the UI thread. I would then expose a Command that provides the save functionality you are looking for. This way, you can access your modified objects (ObservableCollection) for printing and discard them if the user doesn't hit the Save Command.