<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Mike Nichols - Son Of Nun Technology : Messaging</title><link>http://devlicio.us/blogs/mike_nichols/archive/tags/Messaging/default.aspx</link><description>Tags: Messaging</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Notes From A Project Applying Domain-Driven Design, Event Sourcing and CQRS : Part 3 – Messaging In Web Applications</title><link>http://devlicio.us/blogs/mike_nichols/archive/2010/06/24/notes-from-a-project-applying-domain-driven-design-event-sourcing-and-cqrs-part-3-messaging-in-web-applications.aspx</link><pubDate>Thu, 24 Jun 2010 23:00:08 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:60728</guid><dc:creator>Michael Nichols</dc:creator><slash:comments>8</slash:comments><comments>http://devlicio.us/blogs/mike_nichols/archive/2010/06/24/notes-from-a-project-applying-domain-driven-design-event-sourcing-and-cqrs-part-3-messaging-in-web-applications.aspx#comments</comments><description>&lt;p&gt;Today I am not strictly mouthing off strictly about the three things in the title, but more about the side-effects of their joy in a web application.&lt;/p&gt;  &lt;p&gt;Two things have made me feel dirty while implementing a messaging solution in a web application while adhering strictly to Command-Query Separation. &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Why do I have a single controller SENDING messages (commands), and READING (reporting) data? &lt;/li&gt;    &lt;li&gt;What patterns exist beyond just a correlation id for notifying the user when their commands’ processing are completed? &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The first one is the one on my mind today as I am testing the bits that send the messages to my Win service hosting my domain. The second one I will deal with in the next post.&lt;/p&gt;  &lt;p&gt;My controllers look pretty typical:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d7e983ca-15a7-40e4-a04c-4494ff631f49" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProjectController : Controller
{
    IExecuteCommands bus;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProjectController(IExecuteCommands bus)
    {
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.bus&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;bus;
    }
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; ActionResult New()
    {
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; View(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; NewProjectViewModel() };
    }
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; ActionResult Create(CreateProjectCommand project)
    {
        bus.Send(project);
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.RedirectToAction&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;MessagingController&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(c&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;c.Wait(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.ConversationId));
    }
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;My controllers are either reading or writing…why do I have the same controller doing both? The semi-optional service dependencies are a smell that I am putting too many responsibilities here. I already adhere to POST-REDIRECT-GET convention, so it seems unnecessary.&lt;/p&gt;

&lt;p&gt;Why not split this into :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;ProjectController = Only GET requests &lt;/li&gt;

  &lt;li&gt;ProjectCommandController = Only POST requests &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since the majority of actions are reads and we want take of advantage of conventions in frameworks like MVC for urls, we can just pull our state-changing actions into a separate controller and append the name with ‘Command’.&lt;/p&gt;

&lt;p&gt;Commands abiding by some kind of naming convention, perhaps ‘Execute’ prefix, would simply be routed to the [ControllerName] + ‘Command’ Controller. &lt;/p&gt;

&lt;p&gt;This would make testing simpler since I don’t have to satisfy unneeded dependencies for action testing. &lt;/p&gt;

&lt;p&gt;What pitfalls do you see in making this a standard practice? Have you tried this and found problems? I’d like to hear any criticisms and comments you might have.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=60728" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/mike_nichols/archive/tags/CQRS/default.aspx">CQRS</category><category domain="http://devlicio.us/blogs/mike_nichols/archive/tags/Messaging/default.aspx">Messaging</category></item></channel></rss>