views:

138

answers:

2

In my WPF app I have implemented LINQ to SQL query which populates an Observable Collection connected with ListView. It is in this method which I can call from somwhere and it works OK:

private void VisualizeAllShows()
{

    MyfirstdbDataContext context = new MyfirstdbDataContext();
    _ShowQuCollection.Clear();

    var sh = from p in context.Shows select p;

    foreach (var p in sh)  
      _ShowCollection.Add(new ShowsQu
        {
            Date = p.Date, 
            Time = p.Time, 
            Description = p.Description 
        });                 
}

Now I need this updating to take place automatically on database table data changing.

Should I use for this purpose this public event in my LINQ to SQL class:

public event PropertyChangedEventHandler PropertyChanged;

If so, please, how to use an event-handler which would fire from data changing? If no, where I should look for right way to do this?

+2  A: 

As far as I know, there is nothing that comes from the database engine that notifies your application of updates to a table on the SQL server. Best bet is to add this using CLR integration on SQL Server, see #3.

These are the options I can think of: 1. Poll for changes by querying the database repeatedly to check for changes. 2. I have seen SqlCacheDependancy used for this, but from what I read it was CPU intensive. 3. Add a CLR assembly to the SQL Server that has code implemented in a trigger, such that updates then run .NET code which notifies your application of the update. If you can get this to work it will probably be the most efficient. I'm not sure though what limitations you have when writing .NET code for a CLR Integration on SQL Server. I.e. whether or not it will let you connect to an external application. How you communicate could vary across any network technology, TCP/IP, WCF, Remoting, etc.

AaronLS
I like the idea of using a SQLCLR trigger to post the change notification. I had clients post change notifications to a WCF service that basically held a blocking queue of notifications for every other client. It scaled well (because requests were blocking practically all the time) but it relied on client cooperation.
Josh Einstein
+2  A: 

INotifyPropertyChanged.PropertyChanged event is used if the ShowsQu object were to have its properties changed. ObservableCollection already implements INotifyCollectionChanged to notify when its contents change.

What you're talking about is having changes in the database automatically trigger a refresh of the collection. So the PropertyChanged event does not apply here.

What you're trying to do can get pretty complex. The simplest (and least scalable) solution is to poll the table on a timer. What I've done in the past is use a "broadcast" mechanism where each client polled a WCF service that blocked until events were available. Then when any client updated the database, they would broadcast an event to all other clients letting them know they should refresh their UI. This kind of notification is probably overkill in your situation and certainly too in depth to cover in my answer. But hopefully it points you in the right direction.

Josh Einstein