1

I'm new to WPF and MVVM (PRISM) (Java is my speciality), however I have created a web service that interacts with my database, with all web service calls handled by my ViewModel Class.

So far, I have attempted a number of possible solutions, which are as follows:

  • Observable collections
  • Implementing INotifyPropertyChanged
  • Implementing OnPropertyChanged

In spite of my efforts, none of these solutions worked for me.

One solution that I did manage to figure out, albeit unsatisfactorily involved creating a TimerDispacher object to make all the web service calls in the ViewModel at an interval of once per second, but I'm sure this is not the best way to do it.

It may even be the worst, because I want the interface to respond as the database changes.

Here is a sample code of the logic in my ViewModel class and please tell me where I went wrong:

public class MyViewModel : ViewModelBase {

    private ObservableCollection<SomeClass> _listeData;
    public ObservableCollection<SomeClass> ListeData {
        set {
            _listeData= value;
            OnPropertyChanged("ListeData");
        }
        get { return _listeData; }
    }

    private string _textData;
    public string TextData {
        get { return _textData; }
        set {
            _textData = value;
            OnPropertyChanged("TextData");

        }
    }

    //Ctor
    protected internal MyViewModel ( ) {
        InitData();
        SetTimer();
    }

    public void InitData() {
        GetTextData();
        GetListeData();
    }

    protected void GetTextData(string param) {
        using (var proxy = new WCFServiceClient()) {
            TextData = proxy.GetTextData(param);
        }

    }

    protected void GetListeData(string param) {
        using (var proxy = new WCFServiceClient()) {
            ListeData = new ObservableCollection<SomeClass> (proxy.GetListeData(param));
        }

    }

    protected void dispatcherTimer_Tick(object sender, EventArgs e) {
        InitData();
    }

    private void SetTimer() {
        var dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += dispatcherTimer_Tick;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();
    }

} 

Note: I want my code to make most of use of WPF functionalities and not having to rely on timer based solutions.

WonderWorker
  • 8,539
  • 4
  • 63
  • 74
  • What triggers the change of the data? Does the database change automatically (from somewhere outside your application) or do you change the data only from within your application? – LionAM Apr 18 '16 at 13:52
  • database changes occurs from outside my application. i just read data from it and display it. – Sami Errougui Apr 18 '16 at 13:59

1 Answers1

0

The WCF interface you built into your application separates the client and server/DB tiers. Through regular WCF, changes made to the DB are not observed by the client (nor by the server tier actually). No notification system based losely on the client would remedy this.

Have a look here and at this SO post for suggestions on how to tackle your requirement.

Community
  • 1
  • 1
Wicher Visser
  • 1,513
  • 12
  • 21