Question

Let me introduce this question by an example:

We have UserNotification class. It has the information about notification, possible user options.

public class UserNotification
{
    // Event fired on notification broadcast
    public event EventHandler<NotifyEventArgs> Notification;

    public string Title { get; set; }
    public string Description { get; set; }
    public List<NotificationResults> Options { get; set; }
    // ...

    // This function is called to raise notification 
    public void Notify()
    {
        if (Notification != null) Notification(null, new NotifyEventArgs());
    }
}

So we have a bunch of notifications. Each time when some part of the program needs to raise a notification (for the user) it takes some notification object and calls Notify() method. Then Notification event is fired and all listeners may process this notification (display it somewhere in gui, show dialog box etc.)

Finally there are the number of such notifications from different parts of the software and even plugins. And notification listeners, most probably, would love to listem them all. So the next thing that comes in mind is NotificationManager. It has collection of UserNotification and NotificationRaised event which is fired when any of registered notifications fires its Notification event

public class NotificationManager
{
    public event EventHandler NotificationRaised;

    private List<UserNotification> _notifications;

    public void AddNotification(UserNotification notification)
    {
        _notifications.Add(notification);
        notification.Notification += () => 
                  { 
                      if (NotificationRaised != null) NotificationRaised(???, ???) 
                  };
    }

And here comes the question. Look at NotificationRaised(???, someEventArgs). At this point we need to pass pointer to the source notification so that listener could work with this particular notification. The NotificationManager's event handler has 'source' field that looks exactly as what we need... The question is:

Is it correct to put the Notification pointer as source? Or by design and some good reasons the source MUST be the object which is firing the event?

In other words, if one looks at the example, would it be 'as intended behavior'?

No correct solution

OTHER TIPS

If you are forwarding an event, I see no problem with forwarding the source from the original event.

IMHO it is okay to replace the sender with something new. The most obvious example where i already did this was a proxy object for something else. There i subscribed to every event of the real class and forwarded the event with a replaced sender (the proxy itself).

But maybe this change to the implementation will give you enough freedom for the future:

public void AddNotification(UserNotification notification)
{
    AddNotification(notification, false);
}

public void AddNotification(UserNotification notification, bool useOriginalSender)
{
    _notifications.Add(notification);
    notification.Notification += (originalSender, e) =>
                {
                    var temp = NotificationRaised;

                    if (temp != null)
                    {
                        var sender = useOriginalSender ? originalSender : this;
                        temp(sender, NotifyEventArgs.Empty);
                    }
                };
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top