4

I have a class that Handles send & receive over a socket between my application and the network. This class uses other classes, including a low level sockket connection class and a PDU handler class that creates the messages to send and handles received data.

Now i use an event to signal my class that the low level connection class has data for it and i need to send that data to the PDU handler to convert to information the application can use and then hand the data to the application.

For future usage, i am trying to get the class to be as generic as possible, so that on future Server/Client projects i will need only to change the PDU handler to take under consideration the new operations availlable and how to handle the data.

All that is well underway and now i am facing the isssue of handing the data back to the app. For that, my logical approach is an event letting the app know data is ready for collection. For that i can either:

a) have one event and let the app sort out what kind of message it is through the operation code (doable) b) Have one event per operation code and have the app subscribe to all of them and thus know at start what it is getting

Considering the idea of making things generic, and the approach stated in b, is there a way to dinamicly create events based on a given delegate signature at runtime?

e. g. imagine you have opcodes in an enum called MyOperation1 and MyOperation2 and you have defined a delegate like:

public delegate void PDUEventHandler(ParamType Param, [...]);

and i want to define events called:

public event PDUEventHandler MyOperation1;
public event PDUEventHandler MyOperation2;

But if i add a new operation code i will need an event for it.

Can this events be created dinamicly or do i need to do it by hand? If i need to do it by hand then i guess a single event would be better and handle things app side.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
537mfb
  • 1,374
  • 1
  • 16
  • 32
  • I think you should go with option A and use a pattern like Chain of Responsibility (http://www.dofactory.com/Patterns/PatternChain.aspx) to handle the events, maybe using MEF (http://msdn.microsoft.com/en-us/library/dd460648.aspx) to load the objects that will handle each kind of operation codes. – Fabio Mar 16 '12 at 13:56
  • Interesting framework - didn't know about it - will have to look into it - although having 1 class per handler (in my case operation) seems overkill - will have too look deeper into that - thanks – 537mfb Mar 16 '12 at 15:20

1 Answers1

1

Perhaps what you need is a callback - essentially you pass to the event handler a delegate for it to execute when the handler is done. Here's a stackoverflow thread to give you an idea

In terms of event handlers & re-useability, perhaps you can extend EventArgs and have that delegate as a property.

EDIT:

I was thinking a single PDUEventHandler having common code and a "hole" where custom code is run. That custom code is passed to the handler as a delegate (i.e. a method) or even a class instance. But let's change that a little...

Sounds like you need a factory. In fact you're practically describing a factory.

Conceptually let go of the idea of passing special opcodes to an EventHandler per se, or having multi-signature PDUEventHandlers.

Create a PDUHandlerFactoryclass. The factory returns a customized instance as a general PDUHandler class reference. Then instead of a PDUEventHander you caller has a PDUHandler reference that points to the factory-custom instance.

Community
  • 1
  • 1
radarbob
  • 4,964
  • 2
  • 23
  • 36
  • sorry - didn't quite understand what you mean - in essence, c# events are callbacks - so that's what i stated upfront - if you mean send a delegate to the event that is raised than we are back to the need of the app to register n events with my main class and that class need to know wich operation to connect it to - or am understanding you wrong? – 537mfb Mar 16 '12 at 15:26
  • that factory idea might be the way to go - will have to look into it - havent worked with factories in soooo long - thanks – 537mfb Mar 21 '12 at 12:50