I have custom event that has several different subscribers who will all use the data passed in my CustomEventArgs and potentially change it for their own use. What is considered the best practice in this scenario to prevent one subscriber from polluting the data that another subscriber is using? Should the publisher create clones of the data and invoke each of the subscribers’ delegates separately? Should the subscriber be responsible for cloning the data if it is to be modified?
Thanks in advance.
Typically, I would recommend making the
EventArgsderived class immutable. The publisher can then set a single instance up and call all delegates normally without worry.Subscribers can always make their own class based on this data as needed.
The one exception to this would be situations where you want to have some form of
Handledproperty within the argument, in which case, you want subscribers to be able to overwrite this value. This is one case where proper documentation is typically enough to handle this correctly.I would rarely recommend calling subscriber delegates separately.