开发者

C# WCF - Publisher/Subscriber Pattern: Displaying Data On Multiple Forms

开发者 https://www.devze.com 2023-01-10 08:04 出处:网络
Scenario So I\'ve taken an example of a distributed application in WCF that implements the Observer pattern.

Scenario

  • So I've taken an example of a distributed application in WCF that implements the Observer pattern.
  • Ihave put the server-side into a windows service and added some classes to do some calculations, which I intend to pass the results back to the client.

The solution is made up of 3 projects.

  • Server-Side Windows Service - Processes all the data/Performs calculations.
  • Common Objects Class Library.
  • Client-Side Windows Forms Application.

They connect and exchange data using a WCF Service that implements the Observer pattern to allow for two-way data exchange.

On The Server-Side:

I've got 3 classes:

  1. Class one calculates X.
  2. Class two calculates Y.
  3. 开发者_JAVA技巧
  4. Class three calculates Z.

The data from these 3 calculations is then published via the WCF Service to the Client-Side.

On The Client-Side:

Here's the TRICKY part………..

I want each set of data from each of these 3 classes to be displayed on a GridControl on a DIFFERENT Form.

So it will be as follows:

  1. Class X data will be published to the MainForm.
  2. Class Y data will be published to the YForm.
  3. Class Z data will be published to the Zform.

The Client application, when first run, will show only the MainForm, but it will contain a menu bar to open the Yform and Zform.

Thoughts:

  • What do you think the best way to do this is?
  • Do I publish all the data to one central class and then invoke the dictionaries bound to the grid controls on each different form to update the data?
  • Or is there another way?

Appreciate the help.


Option 2 in your list would work, publish to a central class on the client and have the other forms query this class for the info. I would suggest a Publish/Subscribe pattern rather than the observer (I presume you will implement this with Duplex bindings), as it is a less coupled implementation which scales better.

This way the service will just publish info to subscribing clients.

You can implement this by hosting WCF response services on the client form, this way when one of your calculation services publishes a result, the service hosted on the WinForm can subscribe and receive this event.

If you have a requirement to keep the forms open and update them in real time you can have each form that needs data to host a service on the form, and just subscribe to different events. You can then synchronize (synchronization context) onto the UI thread and update the grid in real time i.e stock price updates.

I recommend Juval Lowy's book Programming WCF Services to help explain the concepts and provide a reference implementation of Publish/Subscribe, hosting services on forms and synchronization context.

0

精彩评论

暂无评论...
验证码 换一张
取 消