Delegates and events, in the interface

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello.

I need to implement the publish/subscribe architecture where class B
(server) does not have a reference to class A (which is in a client), but
class B still has to subscribe to the event raised by the class A.

Here's the problem: Class A implements the interface IClient that is on the
server, everything's fine. When the method Foo of the class A is executed,
the event is raised, passing SomeData as a reference. Now, in order to
subscribe to the event from the server, I need to do something like this:

Foo f = new Foo();
IClient icl = f as IClient();

and then:

icl.SomeEvent += new ... bla, bla...

but I DO NOT HAVE a reference to class A, method Foo, which is the whole
point from the very beginning. Is there another way to subscribe to the event
that is wrapped in the interface without having to know the class that's
implementing it on the client side?

Thanks.
 
No.

You can always bind to interfaces, but there must be an actual object to
work against, otherwise, how would any code get executed since interfaces
have no implementation.
 
It works, I already sorted it out. The class A would pass the reference of
itself to class B, which would then do something like:

public void ConnectToServer( IClient client )
{
client.someEvent += client.someEventHandler( methodThatImplementsIt );
}

when the client would do:

ConnectToServer( this );

in order to publish the event.

Just it was not that obvious how the server would subscribe to the event
using the client object from the interface, but there's the solution.

Cheers.
 
Back
Top