Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've run into a similar situation a lot when dealing with MVC. The way that I like to handle it is to use a mediator design pattern in the controller.</p> <p>Basically, you have a class that has a register function and a notify function. The register function takes an object that implements a listener interface and a messageId. It stores these in a dictionary. The notify function takes a messageId for the event that needs to be sent to the listeners and notifies the appropriate ones that the event has occurred.</p> <p>So maybe something along the lines of</p> <pre><code>public interface IListener { void MessageRaised(int messageId, params object[] arguments); } public class Mediator { public void Register(IListener listener, int messageId) { //... add to dictionary of arrays for each messageId } public void NotifyListeners(int messageId, params object[] arguments) { //... loop through listeners for the messageId and call the MessageRaised function for each one } } </code></pre> <p>Now normally I have a base controller and it implements a static Mediator object. Then all my other controllers inherit from it. If you are using the code behind and cannot inherit, then you might try using a singleton pattern. .Net static classes are pretty great too since they have a constructor so you could use that as well.</p> <p>So in your case, I would have the code behind for each control implement IListener and then in the constructor for each one, have something like Mediator.GetInstance().Register(this, Messages.MyEvent). That is kind of a quick and dirty way which can be refactored a bit in the future to make it a bit more reusable.</p> <p>Some resources from a quick google search</p> <p><a href="http://www.avajava.com/tutorials/lessons/mediator-pattern.html" rel="nofollow">http://www.avajava.com/tutorials/lessons/mediator-pattern.html</a></p> <p><a href="http://sourcemaking.com/design_patterns/mediator" rel="nofollow">http://sourcemaking.com/design_patterns/mediator</a></p> <p>Good luck</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload