Question

I don't think I am even asking this right but here it goes. I have a .NET CF app that displays a datagrid of info. This app is connected by TCP Sockets to a Central Server that will periodically broadcast out data.

How do I get my datagrid on my ShellForm to update. Feels wrong to have a reference to my ShellForm in my DAL where the Socket stuff is happening.

Would I use a Delegate or Async Callback? Just looking for a little guidance. Thanks!

Was it helpful?

Solution

The DAL can just publish an Event, and then the GUI can subscribe to it.
The reference (and dependency) will be from the GUI to the DAL.

Watch your thread-safety as well.

OTHER TIPS

I'd suggest that your UI shouldn't know anything about your DAL at all. What I'd do for this would be to create an intermediate "presenter" class that watches the DAL and then can notify the UI, either via an event, callback or whatever.

I would most likely create a presenter class that implements INotifyPropertyChanged, which would allow you to directly watch the event or to data bind to the property that you're using to fill your grid. The presenter would also handle marshaling to the UI context, so neither the UI or the DAL would have to worry about it.

Some sort-of pseudo code might look like this. Bear in mind I have all sorts of infrastructure bits in my code, so this is not likely to just compile, but it should give you a flavor of how I'd attack the problem.

class PointPresenter : INotifyPropertyChanged
{
    private IDataService DAL { get; set; }

    protected Control EventInvoker { get; private set; }

    public PointPresenter()
    {
        // get your DAL reference however you'd like
        DAL = RootWorkItem.Services.Get<IDataService>();
        EventInvoker = new Control();
        // this is required to force the EE to actually create the 
        // control's Window handle
        var h = EventInvoker.Handle;
    }

    protected void RaisePropertyChanged(string propertyName)
    {
        try
        {
            if (m_disposed) return;

            EventInvoker.BeginInvokeIfRequired(t =>
            {
                try
                {
                    PropertyChanged.Fire(this, propertyName);

                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                }
            });
        }
        catch (ObjectDisposedException)
        {
            // if the Form to which we're sending a message is disposed, 
            // this gets thrown but can safely be ignored
        }
        catch (Exception ex)
        {
            // TODO: log this
        }
    }

    public int MyDataValue
    {
        get { return DAL.Data; }
        set
        {
            if (value == MyDataValue) return;
            DAL.Data = value;
            RaisePropertyChanged("MyDataValue");
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top