0
votes

I'm having a bit of a problem with implementing an event architecture in C#. The basic layout is like this:

We have a network-layer(dll) that communicates with a server. This layer has made several interfaces for events of changes within the database. A database-field changes and the dll calls my implementation of the appropriate interface. I have a GUI, and within it's main method a data-object, which stores lists of temporary data I display until something gets changed by my user, in which case I'll send the changes to the database.

The problem is now that I can't implement an event-handler within the network-layer, or my implementation of it's interfaces, because my data-object(which should get the results of the events) only exists within my GUI, and it's main method, and as such is not known to the appropriate objects.

I'll make an example as pseudo-code:

namespace ClientConnection
{
    public class DataListener : IDataListener
    {
        public delegate void SomethingReceivedHandler(object sender, SomethingData packet);
        public event SomethingReceivedHandler somethingRecievedHandler;

        public void SomethingReceived(SomethingData packet)
        {
            if (SomethingRecievedHandler != null)
            {
                SomethingRecievedHandler(this, packet);
            }
        }

Is my current implementation of the interface. The Layer has something akin to:

 private void ProcessPacket(SomethingData packet)
        {
            if (packet == null)
                return;
            try
            {
                if (packet is SomethingData)
                    DataListener.SomethingReceived(packet as SomethingData);
//snip

And my main-method is:

public partial class Main : FormMain
    {
        Data ClientData; //Contains all temporary data within the GUI

        public frmMain()
        {
            ClientData = new Data();
            DataListener dataListener = newDataListener();
            InitializeComponent();
        }

And if the event fires I want to do something like: ClientData.SomeList.Add(packet) depending on the context.

1

1 Answers

1
votes

The problem is now that I can't implement an event-handler within the network-layer, or my implementation of it's interfaces, because my data-object(which should get the results of the events) only exists within my GUI, and it's main method, and as such is not known to the appropriate objects.

Not sure I see the problem. The publisher of the event doesn't need to know anything about the subscriber of the event - that's what makes them useful.

Based on your pseudo-code, it should be as simple as:

public partial class Main : FormMain
{
    Data ClientData; //Contains all temporary data within the GUI

    public frmMain()
    {
         ClientData = new Data();
         DataListener dataListener = new DataListener();
         // Add an event handler
         dataListener.somethingReceivedHandler += 
             (object sender, SomethingData packet)              
             {
                if (someContext) ClientData.SomeList.Add(packet); 
             };
         InitializeComponent();
    }

You probably want to elevate dataListener to a field (instead of a local) so that it stays alive outside of the ctor (and you can unsubscribe when your form closes).