{"id":863,"date":"2014-01-13T20:58:28","date_gmt":"2014-01-13T20:58:28","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=863"},"modified":"2014-01-13T20:58:28","modified_gmt":"2014-01-13T20:58:28","slug":"caliburn-micros-message-bus-event-aggregator","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/caliburn-micros-message-bus-event-aggregator\/","title":{"rendered":"Caliburn Micro&#8217;s message bus (event aggregator)"},"content":{"rendered":"<p>Most MVVM frameworks come with some form of message bus. A means to send messages from one class to any classes wishing to listen for such messages. Ofcourse in a MVVM world those classes might be view models also.<\/p>\n<p>In Caliburn Micro this message bus implementation is known as the event aggregator which is defined by the IEventAgreggator interface.<\/p>\n<p>A class wishing to subscribe to events simple get passed an IEventAggregator as calls the subscribe method as follows<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic MyViewModel(IEventAggregator eventAggregator)\r\n{\r\n   eventAggregator.Subscribe(this);\r\n}\r\n<\/pre>\n<p>In this example we&#8217;re using the constructor and in this case, I&#8217;m using the Ninject bootstrapper which we looked at in a previous post, to inject the IEventAggregator into the constructor.<\/p>\n<p>Obviously we need to add some code to actually handle any events as this solely subscribes to the aggregator, so for the sake of argument we&#8217;ll assume we have an event class which looks like the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyMessageEvent\r\n{\r\n   public MyMessageEvent(string message)\r\n   {\r\n      Message = message;\r\n   }\r\n\r\n   public string Message { get; provate set; }\r\n}\r\n<\/pre>\n<p>And now on our view model we implement IHandle<MyMessageEvent><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyViewModel : PropertyChangedBase, IHandle&lt;MyMessageEvent&gt;\r\n{\r\n   public void Handle(MyMessageEvent message)\r\n   {\r\n      \/\/ do something with the event\r\n   }\r\n}\r\n<\/pre>\n<p>Now we need something to publish events&#8230;<\/p>\n<p>If we create another view model and again pass the IEventAggregator into the constructor as per<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyViewModel : PropertyChangedBase\r\n{\r\n   private IEventAggregator eventAggregator;\r\n\r\n   public MyViewModel(IEventAggregator eventAggregator)\r\n   {\r\n      this.eventAggregator = eventAggregator;\r\n   }\r\n\r\n   public void PublishMessage()\r\n   {\r\n      event.Publish(new MyMessageEvent(&quot;Hello&quot;));\r\n   }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Most MVVM frameworks come with some form of message bus. A means to send messages from one class to any classes wishing to listen for such messages. Ofcourse in a MVVM world those classes might be view models also. In Caliburn Micro this message bus implementation is known as the event aggregator which is defined [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61],"tags":[],"class_list":["post-863","post","type-post","status-publish","format-standard","hentry","category-caliburn-micro"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=863"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/863\/revisions"}],"predecessor-version":[{"id":1072,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/863\/revisions\/1072"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}