开发者

Disconnected Event registration and subscription

开发者 https://www.devze.com 2023-01-23 22:35 出处:网络
The publisher exposes an event to which multiple subscribers can hook up to on subscriber side -> publisher.OnSomeEvent += subscriber.CallMe()

The publisher exposes an event to which multiple subscribers can hook up to

on subscriber side -> publisher.OnSomeEvent += subscriber.CallMe()

Later the subscriber subscribes based on some dynamic arguments

publisher.Subscribe(arguments)

publisher.Unsubscribe(arguments)

Depending on what arguments are passed by the subscriber, I want publisher to update subscribers for the content they have subscribed.

Internally i can maintain a map of subscriber to arguments but my problem is that as event registration and subscription are disconnected i can't find a good way to pass the callee handle (so i can maintain a map subscriber->arguments) when calling Subscribe/Unsubscribe call.

To keep my API simple i don't want callee to pass "this", also as StackFrame and diagnostics API does not work in release mode because of inlining issue, i can't think of a better design.

Appreciate your help if you can suggest a better way to a开发者_JS百科chieve this?


This isn't the most elegant solution, but you could have the publisher deal with the subscriber through an interface which would decouple the publisher from needing to know about any specific subscriber implementation.

For example

interface ISubscriber
{
object Arguments{get;}
}

Which would make the client API signature:

void Subscribe(ISubscriber subscriber)

Of course you may want to implement the interface on a seperate type if you don't want your subscriber types having an Arguments property.


If you want to filter the publications, I say ditch the event and instead use something like

publisher.Subscribe(arguments, subscriber.CallMe)

To unsubscribe you must have some dort of identifier, could be a GUID, string, int, this (not sure why you want to avoid it)...

0

精彩评论

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