0

I'm currently working on WPF app that is architectured as follows:

  • MVVM
  • Entity Framwork 4 (with LINQ).
  • WCF service that pool Database to get data (Oracle).
  • I Make my WFC call in my View Model Class and put my data in an ObsevableCollections.
  • Db Changes occurs from a another app.

So my app does not do any write actions on the DB what-so-ever (Zéro), it only reads data and displays it on the UI.

How can I make my app to be quickly responsive to DB changes, I read about the following solutions but I'm confused and don't know what to use:

  • Pooling DB every n seconds with a DispatcherTimer (seems to be too much work cause data changes every millisecond)
  • SqlDependency, searched all over the internet but didn't find a proper implementation with EF.

As I said, db changes every millisecond (financial data from other sources),

How can resolve this?

Thank you.

Elydasian
  • 2,016
  • 5
  • 23
  • 41
  • Did you see this example (SqlDependency + Entity Framework)... https://code.msdn.microsoft.com/How-to-use-SqlDependency-5c0da0b3 – Monty May 27 '16 at 10:27
  • yes i did but coudnt get what i wanted from if... i think i found a suitable solution, i'll post it soon. – Sami Errougui May 27 '16 at 14:42

1 Answers1

0

i tried the code bellow and it seemed to be working okay for the moment (but i still have some doubts about that infinit loop), let me know what you thing :

public class MyViewModel
    {


          BackgroundWorker _bgWorker ;
          //some props
          //some funcs


protected internal MyViewModel(Session session)
        {
            Session = session;
            RefreshData();
        }        

protected void RefreshData()
        {
            try
            {
                _bgWorker = new BackgroundWorker
                {
                    WorkerReportsProgress = true,
                    WorkerSupportsCancellation = true

                };
                _bgWorker.DoWork += bgDoWork;

                if (!_bgWorker.IsBusy)
                {
                    _bgWorker.RunWorkerAsync();
                }
            }
            catch (Exception)
            {
                _bgWorker.CancelAsync();
            }
        }

        private void bgDoWork(object sender, DoWorkEventArgs e)
        {
            var worker = (BackgroundWorker)sender;
            while (!worker.CancellationPending)
            {
                //Thread.Sleep(1000); should i keep this or not ?
                Proxy(); // WCF calls
            }
        }
        }