title: Implementing a message/subscription mechanism in C#
body: <p>I'm prototyping a WPF application with the MVVM pattern. Following an answer to <a href="https://stackoverflow.com/questions/14361687/interaction-of-view-models-in-mvvm">this question</a> I have set up a <code>ModelProviderService</code> which exposes models as properties. The consumers of the service are the viewmodels, i.e. they pull their models from the service instead of instantiating them theirselves.</p>
  
  <pre><code>class ModelProviderService {
  
    private LoginModel loginModel;
    public LoginModel LoginModel {
      get { return loginModel; }
      set { loginModel = value; }
    }
  
    private ProjectsModel projectsModel;
    public ProjectsModel ProjectsModel {
      get { return projectsModel; }
      set { projectsModel = value; }
    }
  
    public ModelProviderService() {
      loginModel = new LoginModel();
      projectsModel = new ProjectsModel();
    }    
  }
  </code></pre>
  
  <p>Now, here is what shall happen:  </p>
  
  <ul>
  <li>A viewmodel changes a property of e.g. the <code>LoginModel</code> property. </li>
  <li>The viewmodel pushes the model property back to the service by setting its property: <code>modelService.LoginModel.MyProperty = localLoginModel.MyProperty;</code></li>
  <li>The service shall publish a message: "Hey, my model of type <code>LoginModel</code> just changed."</li>
  <li>Any other viewmodel that has subscribed to this message will pull this changed model from the service.</li>
  </ul>
  
  <p>How can I implement:</p>
  
  <ol>
  <li>the "broadcast message"?</li>
  <li>the subscription to the message?</li>
  </ol>