dimanche 12 juillet 2015

create a SaveAll method for ObservableCollections in different ViewModels

I have a DataLayer which contains all the methods to get data from the SQLite database and store it in the different ObservableCollections spread over different ViewModels. This all works fine.

Now I want to save the data or datachanges from those ObservableCollections back to the SQLite Database in a SaveAll Method by clicking on SaveAll in the menu.

What I have so far is that when I click SaveAll it sends a message to the DataLayer Class telling to fire a Method. This is what it looks like.

public class DataLayer
{
    public DataLayer()
    {
        Messenger.Default.Register<string>(this, "SaveAll", SaveAll());
    }

private void SaveAll(ObservableCollection<Scene> AllScenes, 
                     ObservableCollection<Image> VisualReferences,
                     ObservableCollection<Shot> AllShots)
    {
        using (TransactionScope ts = new TransactionScope())
        {
           //Write Data from ObservableCollection 1 back to SQLdB

            ts.Complete();
            ts.Dispose();
        }
        using (TransactionScope ts2 = new TransactionScope())
        {
           //Write Data from ObservableCollection 2 back to SQLdB

            ts2.Complete();
            ts2.Dispose();
        }
        using (TransactionScope ts3 = new TransactionScope())
        {
           //Write Data from ObservableCollection 3 back to SQLdB

            ts3.Complete();
            ts3.Dispose();
        }
      MessageBox.Show("All Saved");
}

I'm not sure if this is the right approach of making a save all. First problem I run into is in the Messenger where the SaveAll() needs 3 arguments. Second is how do I get the changed ObservableCollections from the different ViewModels back to the DataLayer class?

I'm very new to c# and I've never needed to do anything like this, I know how to store 1 ObservableCollection back to SQLite dB, but not multiple from different ViewModels. What is the right approach of doing this?

Aucun commentaire:

Enregistrer un commentaire