<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Aviblock.com &#187; command query separation</title>
	<atom:link href="http://www.aviblock.com/blog/tag/command-query-separation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aviblock.com/blog</link>
	<description>My musings on web development</description>
	<lastBuildDate>Thu, 03 Jun 2010 13:40:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CQRS (Command and Query Responsibility Segregation) in PHP</title>
		<link>http://www.aviblock.com/blog/2009/12/10/cqsr-in-php/</link>
		<comments>http://www.aviblock.com/blog/2009/12/10/cqsr-in-php/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 20:57:54 +0000</pubDate>
		<dc:creator>Avi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[command query separation]]></category>
		<category><![CDATA[ddd]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.aviblock.com/blog/?p=82</guid>
		<description><![CDATA[It always seems to me that the PHP world is one step behind the current trends in the &#8220;enterprise world&#8221;. For example, Rails popularized the &#8220;ActiveRecord&#8221; pattern a few years ago. Ever since then, there has been an explosion of ActiveRecord implementations in PHP. Many even mistakenly refer to them as an &#8220;ORM&#8221;, but I [...]]]></description>
			<content:encoded><![CDATA[<p>It always seems to me that the PHP world is one step behind the current trends in the &#8220;enterprise world&#8221;. For example, Rails popularized the &#8220;ActiveRecord&#8221; pattern a few years ago. Ever since then, there has been an explosion of ActiveRecord implementations in PHP. Many even mistakenly refer to them as an &#8220;ORM&#8221;, but I won&#8217;t beat a dead horse here. ActiveRecord is great for your basic run-of-the-mill address book/cookbook/blog type of app, where it basically data driven, and there&#8217;s not much &#8220;business logic&#8221; to go around. For the majority of PHP applications out there, these tools are a god send.<br />
As PHP matures, especially the object model, advanced developers have started to realize the limits of this pattern. Now the big rage is the DataMapper pattern, and DDD. We try to separate out the domain of application, and the holy grail is now transparent persistence. With great tools like Doctrine 2.0 and Object_Freezer, both of which are only possible with the new additions in PHP 5.3, these dreams are becoming a reality.<br />
<span id="more-82"></span><br />
With this approach, which I have tried to follow recently, there are a couple of pointed issues. The biggest is displaying your domain objects. Off the bat, we don&#8217;t want to &#8220;pollute&#8221; our domain with display concerns (although one could argue that displaying the object is a business concern). So if we want to &#8220;drag the data&#8221; out of our domain, we need a lot of ugly &#8220;getters&#8221;, or to leverage the __get magic method. Getters have long been shunned for breaking encapsulation, revealing the &#8220;shape&#8221; of an entity (ie., it should not be so important to us that a customer has a first name), and some other things that don&#8217;t really apply in PHP land. But besides all that, they&#8217;re just so annoying to write.<br />
Additionally, we don&#8217;t necessarily want to have our domain objects show up in our UI like that. The domain objects are not tailored made for consumption of our UI. There are many different presentation concerns such as, what I call the &#8220;Fowler Conundrum&#8221;, where do you specify that negative balances will show up in red, and positive in black? Do you create views which have all this ugly logic in them? Do you add it to your domain? Do you create a view helper for every little presentation concern? What about date formatting. Do you need to bring in a DateTime object in your view, and clutter it up with date formatting code? What if you only need a handful of fields to display&#8230;do we throw in the whole object to the UI and hope for the best?<br />
The answer to this is to add an additional layer. The ViewModel. The ViewModel is shaped for every view in your UI. It contains all the necessary presentation logic.<br />
The question still begs, how do you create these ViewModels? There&#8217;s basically three choices. Getters, displaying to an interface, and using Reflection. All of these feel like you&#8217;re compromising somewhere.<br />
Another problem that I came up with recently is, how do you manage something like an autocomplete field. An autocomplete field takes the letters which have been typed in, and searches the database. So do I now need to search the database, get a bunch of domain objects, translate them in a ViewModel and send it back to the UI?<br />
How about pagination? For efficiency purposes, we obviously want to paginate on the query level. We don&#8217;t want to bring in all our information, and then paginate that&#8230;it defeats (most of) the purpose. So do we have to pollute our repositories with findByPage methods? The other solution is almost as ugly (but it has a lot of elegance to it). In the .NET world, a proposed solution was to return an IQueryable&#8230;an interface that LINQ understands. The idea is, you&#8217;re repository returns an IQueryable (or even better, a special subset of IQueryable that you can call IPageable), and once you get that, you construct the rest of the information as gotten from the ui. This was debated upon for a long time whether 1) Whether this constitutes &#8220;pure DDD&#8221; and 2) Whether it is a good practice to begin with. The PHP version of this could be to return a Zend_Paginator_Adapter_Interface and basically do the same thing.<br />
The latest &#8220;trend&#8221;, of which I don&#8217;t really see anybody speaking about in the PHP world, is that of CQRS. CQRS stands for Command and Query Responsibility Segregation, and it was formally known as CQS, or command query separation. CQS is a basic tenet of OOP, put forth by Bertand Meyer. The idea is simple, every method on an object is either a command (ie., it does something), or it is a query (ie., it retrieves information). A method should not do both (Martin Fowler admits that there are some compelling reasons to violate this, like popping a value from a stack, but in that case, the convenience outweighs the strict and blind adherence to principles).<br />
In the past year of so, this pattern has been expanded on an architectural level. Meaning commands go to the domain, and queries go somewhere else. There are two parts of your system. The commands, which tell your system to do something, and the queries. The queries come from a query repository, which is a stale snapshot of the state of your entities. The commands go to a domain repository, which publish an internal event (which captures the business intent of the command), and an external event to update the data store.<br />
The beauty of this, is that your view models don&#8217;t come from your domain anymore. They come from your query repository. You could retrieve them anyway you want (ORM, TableDataGateway, ActiveRecord or even straight SQL). In fact, the various views on your data could actually be real views in your database, denormalized to flatten relationships, or with only the fields that you need. Also, the internal published events can be stored in an EventStore, and played back to maintain the current state of the entity. This removes almost entirely the &#8220;impendence mismatch&#8221; between objects and the relational database.<br />
I will hopefully right more on this pattern, and how it can be applied to PHP, as I learn more about it myself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aviblock.com/blog/2009/12/10/cqsr-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
