﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com/blog/</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2012 (c) 2012</copyright><ttl>60</ttl><item><title>Security decisions: Separate Operations &amp;amp; Queries</title><description>&lt;p&gt;The question came up several times in the mailing list with regards to how the RavenDB Authorization Bundle operates, and I think it serves a broader discussion.&lt;/p&gt; &lt;p&gt;Let us imagine a system where we have contracts, which may be in several states: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;Mine – Contracts that an employee signed.  &lt;li&gt;Done – Standard users can view, Lawyers assigned to the company can sign.  &lt;li&gt;Draft – Lawyers can view / edit, Partners can approve.  &lt;li&gt;Proposed – Lawyers can create / edit, but only the lawyer that created it can view it, Partners can accept.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;So far, fairly simple, right? Except the pure &lt;em&gt;hell&lt;/em&gt; that you are going to get into when you are trying to show the users all of the contracts that they can see, sorted by edit date and in the NDA category.&lt;/p&gt; &lt;p&gt;Why am I being so negative here? Well, let us look at what we are going to have to do in the most trivial of cases:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Security_9400/image_4.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Security_9400/image_thumb_1.png" width="450" height="221"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;In this sort of system, we are going to have to show the user all of the contracts that they are allowed to see, and show them some indication what operations they can do on each.&lt;/p&gt; &lt;p&gt;The problem is that generating this sort of view is &lt;em&gt;expensive&lt;/em&gt;. Especially when you have large amount of data to work through. More interesting, from a UX perspective, it also doesn’t really work that well. Most users would want a better separation of the things that they can do, probably something like this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Security_9400/image_9.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Security_9400/image_thumb_3.png" width="450" height="221"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;This allows us to do a first level filtering on the data itself, rather than try to apply security rules to it.&lt;/p&gt; &lt;p&gt;In the first case, we need to get all the contracts that we are allowed to see. The security rules above are &lt;em&gt;really&lt;/em&gt; simple, mind. But trying to translate them into an efficient query is going to be pretty hard. Both in terms of the code requires and the cost to actually perform the query on the server. There are other things that are involved as well, such as paging and sorting in such an environment.&amp;nbsp; I have created several such systems in the past, Rhino Security is probably the most well known of them, and it gets really hard to optimize things and make sure that everything works when you start getting more complex security rules (especially when you have a user editable security system, which is a common request).&lt;/p&gt; &lt;p&gt;The second case is cheaper because we can limit the choices that we see in the query itself. We may still need to apply security concerns, but those goes through the query directly, rather than a security sub system. This kind of change usually force people to be more explicit in what they want, and it result in a system that tends to be simpler. The security rules aren’t just something arbitrary that can be defined, they are actually visible on the screen (My Contracts, Drafts, etc). Changing them isn’t something that is done on an administrator’s whim.&lt;/p&gt; &lt;p&gt;Yes, this is a way to manage the client and their expectations, but that is &lt;em&gt;important&lt;/em&gt;. But what about the complex security that they want? &lt;/p&gt; &lt;p&gt;That might still be there, certainly, but that would be active mostly for operations (stuff that happen on a single entity), not on things that happen over all entities. It is drastically easier to make a single entity security decisions work efficiently than make it work over the whole set inside the database.&lt;/p&gt;</description><link>http://ayende.com/blog/152929/security-decisions-separate-operations-amp-queries?key=dd5d38bf-cf0b-4233-b6c5-b75582e2e82f</link><guid>http://ayende.com/blog/152929/security-decisions-separate-operations-amp-queries?key=dd5d38bf-cf0b-4233-b6c5-b75582e2e82f</guid><pubDate>Tue, 10 Apr 2012 10:00:00 GMT</pubDate></item><item><title>Monika: A lesson in component based design</title><description>&lt;p&gt;I was giving a lecture on architecture recently, and the notion of components came in. The most important bit about that lecture was probably at the very end, when I discussed what it is that I consider to be a component. During that discussion, I introduced Monika, the payment processing component.&lt;/p&gt; &lt;p&gt;Monika has the following Service Level Agreement:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Payment initiation is done messages.&lt;/li&gt; &lt;li&gt;Notification about payment completion is handled via a callback REST call.&lt;/li&gt; &lt;li&gt;The SLA calls for 90% of all successful payments to be processed in 2 business days.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;So far, it doesn’t sound really complicated, right? And there isn’t even a hint of how Monika works in the SLA or the contracts.&lt;/p&gt; &lt;p&gt;This is Monica:&lt;/p&gt; &lt;p&gt;&lt;img src="http://blog.rounds.com/wp-content/uploads/2011/05/monica-friends.jpg"&gt;&lt;/p&gt;  &lt;p&gt;Well, not really, but it makes the point, doesn’t it.&lt;/p&gt; &lt;p&gt;Monica is a component in the system that respond to (SMTP) messages, does some work, and respond by clicking on a link in the email (REST call).&lt;/p&gt; &lt;p&gt;Monica has a really sucky SLA, since she has only 22% uptime over the course of the year, and then there are those two weeks when she has her yearly maintenance period (vacation), etc.&lt;/p&gt; &lt;p&gt;The most important thing about this is that we are able to abstract all of that away and treat this scenario as just another component in the system. &lt;/p&gt; &lt;p&gt;All too often, people hear components and they start thinking about things like this:&lt;/p&gt; &lt;p&gt;&lt;img src="http://www.agilemodeling.com/images/models/componentDesign.JPG"&gt;&lt;/p&gt; &lt;p&gt;A component in a system is usually something much larger than a single class or a set of classes. It is an independent agent in the system that has its own behavior, resources, dedicated team and deployment schedule separate from all other components.&lt;/p&gt;</description><link>http://ayende.com/blog/152961/monika-a-lesson-in-component-based-design?key=7e93ac65-10fb-4375-9771-4f958d8dab9a</link><guid>http://ayende.com/blog/152961/monika-a-lesson-in-component-based-design?key=7e93ac65-10fb-4375-9771-4f958d8dab9a</guid><pubDate>Fri, 06 Apr 2012 10:00:00 GMT</pubDate></item><item><title>Beware the common infrastructure</title><description>&lt;p&gt;One of the common problems that I run into when consulting with clients, or just whenever I am talking to developers in general is the notion of common infrastructure. “We are going to spend some time building a common infrastructure which we can then use on all of our applications.”&lt;/p&gt; &lt;p&gt;I made that mistake myself with Rhino Commons, and again very recently with RaccoonBlog (look at the code, you see the Loci stuff, that is stuff that is used from another project).&lt;/p&gt; &lt;p&gt;Why is that a problem? Well, for the simplest reason of all. Different projects have different needs. A common infrastructure that tries to accommodate them all is going to be much more complex. Not only that, it is going to be much more brittle. If I am modifying it in the context of project A, can I really say that I didn’t break something for project B?&lt;/p&gt; &lt;p&gt;Let us take a simple example, executing tasks. In RaccoonBlog, we need tasks merely to handle comments and email (long running background tasks). In another application, we need to do retries, and we need to get notifications if after N retries, the task have failed. In a third project, we need a way to specify dependencies between tasks.&lt;/p&gt; &lt;p&gt;Sure, you can build something that satisfy all three projects, but it would be drastically more complex than having to modify the original task executer for each project needs. And yes, I do mean copying the code and modifying it.&lt;/p&gt; &lt;p&gt;And no, it is not a horrible sin against the Little Endianness. Even duplicated N times, the code is going to be simpler to read, perform faster, easier to maintain and modify over time.&lt;/p&gt; &lt;p&gt;Nitpicker note: I am not talking about 3rd party libraries here. If you can find something that fits your needs that already exists, wonderful. I am talking about infrastructure that &lt;em&gt;you&lt;/em&gt; build, inside your organization.&lt;/p&gt;</description><link>http://ayende.com/blog/154753/beware-the-common-infrastructure?key=75a7bb94-d7a2-4074-8917-627ecb8c598b</link><guid>http://ayende.com/blog/154753/beware-the-common-infrastructure?key=75a7bb94-d7a2-4074-8917-627ecb8c598b</guid><pubDate>Thu, 05 Apr 2012 10:00:00 GMT</pubDate></item><item><title>Searching ain&amp;rsquo;t simple: solution</title><description>&lt;p&gt;On my last post, I descried the following problem:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Searching-aint-simple-solution_9321/image_thumb_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image_thumb" border="0" alt="image_thumb" src="http://ayende.com/blog/Images/Windows-Live-Writer/Searching-aint-simple-solution_9321/image_thumb_thumb.png" width="462" height="300"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;And stated that the following trivial solution is the wrong approach to the problem:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; d.* &lt;span class="kwrd"&gt;from&lt;/span&gt; Designs d 
 &lt;span class="kwrd"&gt;join&lt;/span&gt; ArchitectsDesigns da &lt;span class="kwrd"&gt;on&lt;/span&gt; d.Id = da.DesignId
 &lt;span class="kwrd"&gt;join&lt;/span&gt; Architects a &lt;span class="kwrd"&gt;on&lt;/span&gt; da.ArchitectId = a.Id
&lt;span class="kwrd"&gt;where&lt;/span&gt; a.Name = @name&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;The most obvious reason is actually that we are thinking too linearly. I intentionally showed the problem statement in terms of UI, not in terms of a document specifying what should be done.&lt;/p&gt;
&lt;p&gt;The reason for that is that in many cases, a spec document is making assumptions that the developer should not. When working on a system, I like to have drafts of the screens with rough ideas about what is supposed to happen, and not much more.&lt;/p&gt;
&lt;p&gt;In this case, let us consider the problem from the point of view of the user. Searching by the architect name makes sense to the user, that is usually how they think about it.&lt;/p&gt;
&lt;p&gt;But does it makes sense from the point of view of the &lt;em&gt;system&lt;/em&gt;? We want to provide good user experience, which means that we aren’t just going to provide the user with a text box to plug in some values. For one thing, they would have to put in the architect full name as it is stored in our system. That is going to be a tough call in many cases. Ask any architect what the first name of Gaudi is, and see what sort of response you’ll get. &lt;/p&gt;
&lt;p&gt;Another problem is how to deal with misspelling, partial names, and other information. What if we actually have the architect id, and are used to type that? I would &lt;em&gt;much&lt;/em&gt; rather type 1831 than Mies Van Der Rohe, and most users that work with the application day in and day out would agree.&lt;/p&gt;
&lt;p&gt;From the system perspective, we want to divide the problem into two separate issues, finding the architect and finding the appropriate designs. From a user experience perspective, that means that the text box is going to be an ajax suggest box, and the results would be loaded based on valid id.&lt;/p&gt;
&lt;p&gt;Using RavenDB and ASP.Net MVC, we would have the following solution. First, we need to define the search index:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Searching-aint-simple-solution_9321/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Searching-aint-simple-solution_9321/image_thumb_3.png" width="750" height="449"&gt;&lt;/a&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;This gives us the ability to search across both name and id easily, and it allows us to do full text searches as well. The next step is the actual querying for architect by name:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Searching-aint-simple-solution_9321/image_4.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Searching-aint-simple-solution_9321/image_thumb_4.png" width="827" height="535"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Looks complex, doesn’t it? Well, there is certainly a lot of code there, at least.&lt;/p&gt;
&lt;p&gt;First, we look for an a matching result in the index. If we find anything, we send just the name and the id of the matching documents to the user. that part is perfectly simple.&lt;/p&gt;
&lt;p&gt;The interesting bits happen when we can’t find anything at all. In that case, we ask RavenDB to find us results that might be the things that the user is looking for. It does that by running a string distance algorithm over the data in the database already and providing us with a list of suggestions about what the user might have meant.&lt;/p&gt;
&lt;p&gt;We take it one step further. If there is just one suggestion, we assume that this is what the user meant, and just return the results for that value. If there is more than that, we sent an empty result set to the client along with a list of alternatives that they can suggest to the user.&lt;/p&gt;
&lt;p&gt;From here, the actual task of getting the designs for this architect becomes as simple as:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Searching-aint-simple-solution_9321/image_6.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Searching-aint-simple-solution_9321/image_thumb_5.png" width="758" height="169"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;And it turns out that when you think about it right, searching &lt;em&gt;is &lt;/em&gt;simple.&lt;/p&gt;</description><link>http://ayende.com/blog/153698/searching-ainrsquo-t-simple-solution?key=223a9b68-41f5-4d83-ad31-e971851da973</link><guid>http://ayende.com/blog/153698/searching-ainrsquo-t-simple-solution?key=223a9b68-41f5-4d83-ad31-e971851da973</guid><pubDate>Fri, 23 Mar 2012 13:06:00 GMT</pubDate></item><item><title>Searching ain&amp;rsquo;t simple</title><description>&lt;p&gt;The problem statement is best described using:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Searches-across-aggregates-are-rare-and-_8E43/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Searches-across-aggregates-are-rare-and-_8E43/image_thumb.png" width="462" height="300"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;This seems like a nice and easy problem, right? We join the architects table to the designs table and we are done.&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; d.* &lt;span class="kwrd"&gt;from&lt;/span&gt; Designs d 
 &lt;span class="kwrd"&gt;join&lt;/span&gt; ArchitectsDesigns da &lt;span class="kwrd"&gt;on&lt;/span&gt; d.Id = da.DesignId
 &lt;span class="kwrd"&gt;join&lt;/span&gt; Architects a &lt;span class="kwrd"&gt;on&lt;/span&gt; da.ArchitectId = a.Id
&lt;span class="kwrd"&gt;where&lt;/span&gt; a.Name = @name&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is a trivial solution, and shouldn’t take a lot of time to build… &lt;/p&gt;
&lt;p&gt;It is also the entirely wrong approach for the problem, can you tell me why? &lt;/p&gt;</description><link>http://ayende.com/blog/153665/searching-ainrsquo-t-simple?key=3f500648-cafb-4efd-8ce5-550099886dfc</link><guid>http://ayende.com/blog/153665/searching-ainrsquo-t-simple?key=3f500648-cafb-4efd-8ce5-550099886dfc</guid><pubDate>Thu, 22 Mar 2012 10:00:00 GMT</pubDate></item><item><title>Composite entities</title><description>  &lt;p&gt;In my previous post, I discussed some of the problems that you run into when you try to have a single source of truth with regards to an entity definition. The question here is, how do we manage something like a Customer across multiple applications / modules.&lt;/p&gt; &lt;p&gt;For the purpose of discussion, I am going to assume that all of the data is either:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;All sitting in the same physical database (common if we are talking about different modules in the same application).&lt;/li&gt; &lt;li&gt;Spread across multiple databases with some data being replicate to all databases (common if we are talking about different applications).&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;We will focus on the customer entity as an example, and we will deal with billing and help desk modules / applications. There are some things that everyone can agree on with regards to the customer. Most often, a customer has a id, which is shared across the entire system, as well as some descriptive details, such as a name.&lt;/p&gt; &lt;p&gt;But even things that you would expect to be easily agreed upon aren’t really that easy. For example, what about contact information? The person handling billing at a customer is usually different than the person that we contact for help desk inquires. And that is the stuff that we are supposed to agree on. We have much bigger problems when we have to deal with things like customer’ payment status vs. outstanding helpdesk calls this month.&lt;/p&gt; &lt;p&gt;The way to resolve this is to forget about trying to shove everything into a single entity. Or, to be rather more exact, we need to forget about trying to thing about the Customer entity as a single physical thing. Instead, we are going to have the following:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Composite-entities_A800/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Composite-entities_A800/image_thumb.png" width="659" height="558"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;There are several things to note here:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;There is &lt;em&gt;no&lt;/em&gt; inheritance relationship between the different aspect of a customer.&lt;/li&gt; &lt;li&gt;We don’t give in and try to put what appears to be shared properties (ContactDetails) in the root Customer. Those details have different meaning for each entity.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;There are several ways to handle actually storing this information. If we are using a single database, then we will usually have something like:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Composite-entities_A800/image_4.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Composite-entities_A800/image_thumb_1.png" width="979" height="264"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;The advantage of that is that it makes it very easy to actually look at the entire customer entity &lt;em&gt;for debugging purposes&lt;/em&gt;. I say for debugging specifically because for production usage, there really isn’t anything that needs to look at the entire thing, every part of the system only care for its own details.&lt;/p&gt; &lt;p&gt;You can easily load the root customer document and your own customer document whenever you need to.&lt;/p&gt; &lt;p&gt;More to the point, because they are different physical things, that solves a lot of the problems that we had with the shared model.&lt;/p&gt; &lt;p&gt;Versioning is not an issue, if billing needs to make a change, they can just go ahead and change things. They don’t need to talk to anyone, because no one else is touching their data. &lt;/p&gt; &lt;p&gt;Concurrency is not an issue, if you make a concurrent modification to billing and help desk, that is not a problem, they are stored into two different locations. That is actually what you &lt;em&gt;want&lt;/em&gt;, since it is perfectly all right for having those concurrent changes.&lt;/p&gt; &lt;p&gt;It free us from having to have everyone’s acceptance on any change for everything except on the root document. But as you can probably guess, the amount of information that we put on the root is minimal, precisely to avoid those sort of situations.&lt;/p&gt; &lt;p&gt;This is how we handle things with a shared database, but what is going on when we have multiple applications, with multiple databases?&lt;/p&gt; &lt;p&gt;As you can expect, we are going to have &lt;em&gt;one&lt;/em&gt; database which contains all of the definitions of the root Customer (or other entities), and from there we &lt;em&gt;replicate&lt;/em&gt; that information to all of the other databases. Why not have them access two databases? Simple, it makes things so much harder. It is easier to have a single database to access to and have replication take care of that.&lt;/p&gt; &lt;p&gt;What about updates in that scenario? Well, updates to the local part is easy, you just do that, but updates to the root customer details have to be handled differently.&lt;/p&gt; &lt;p&gt;The first thing to ask is whatever there really is any need for any of the modules to actually update the root customer details. I can’t see any reason why you would want to do that (billing shouldn’t update the customer name, for example). But even if you have this, the way to handle that is to have a part of the system that is responsible for the root entities database, and have it do the update, from where it will replicate to all of the other databases.&lt;/p&gt;</description><link>http://ayende.com/blog/153704/composite-entities?key=370e63de-282e-4d1e-bbe3-6674aba2601e</link><guid>http://ayende.com/blog/153704/composite-entities?key=370e63de-282e-4d1e-bbe3-6674aba2601e</guid><pubDate>Fri, 16 Mar 2012 10:00:00 GMT</pubDate></item><item><title>There ain&amp;rsquo;t no such thing, the definitive entity definition</title><description>&lt;p&gt;I was at a customer site, and we were talking about a problem they had with modeling their domain. Actually, we were discussing a proposed solution, a central and definitive definition for all of their entities, so all of the applications could use that.&lt;/p&gt; &lt;p&gt;I had a minor seizure upon hearing that, but after I recovered, I was able to articulate my objections to this approach. &lt;/p&gt; &lt;p&gt;To start with, it breaks the Single Responsibility Principle, the Open Closed Principle and the Interface Segregation Principle. It also makes versioning hard, and introduce a central place where everyone must coordinate with. Think about the number of people that has to be involved whenever you make a change.&lt;/p&gt; &lt;p&gt;Let us take the customer as the representative entity for this discussion. We can all agree that a customer has to have a name, an email and an id. But billing also need to know his credit card information, help desk needs to track what support contracts he has and sales needs to know what sort of products we sold the guy, so we can sell him upgrades.&lt;/p&gt; &lt;p&gt;Now, would you care to be the guy who has to mediate between of all of those different concerns? &lt;/p&gt; &lt;p&gt;And what about changes and updates? Whenever you need to make a change, you have to wait for all of those teams and application to catch up and update and deploy their apps,.&lt;/p&gt; &lt;p&gt;And what about actual usage? You actually &lt;em&gt;don’t&lt;/em&gt; want the help desk system to be able to access the billing information, and you most certainly don’t want them to change anything there.&lt;/p&gt; &lt;p&gt;And does it matter if we have concurrent modifications to the entity by both help desk and billing? &lt;/p&gt; &lt;p&gt;All of those things argue very strongly against having a single source of truth about what an entity is. In my next post, I’ll discuss a solution for this problem, Composite Entities.&lt;/p&gt;</description><link>http://ayende.com/blog/153703/there-ainrsquo-t-no-such-thing-the-definitive-entity-definition?key=053a882a-c980-46d9-a1e1-bac7d032146d</link><guid>http://ayende.com/blog/153703/there-ainrsquo-t-no-such-thing-the-definitive-entity-definition?key=053a882a-c980-46d9-a1e1-bac7d032146d</guid><pubDate>Thu, 15 Mar 2012 10:00:00 GMT</pubDate></item><item><title>REST and Urls</title><description>&lt;p&gt;
	&lt;a href="http://wekeroad.com/2012/03/03/moving-the-philosophy-into-machinery/"&gt;Rob Conery has been talking about REST&lt;/a&gt; lately, and I think he perpetuate a common misconception. In particular, in the post I referenced, he is asking about ideas for URLs for doing things like logging in, working with productions and episodes, etc.&lt;/p&gt;
&lt;p&gt;
	The problem with that is that this has very little to do with REST. Now, I&amp;rsquo;ll be the first that will tell you that discussions about architectural purity bore me, and I really like the concept of nice URLs. But nice URLs are totally different from REST.&lt;/p&gt;
&lt;p&gt;
	&lt;a href="http://www.slideshare.net/kjbuckley/doing-rest-right-3385800"&gt;These slides&lt;/a&gt; do a really good work of describing what REST is and how to work with it.&lt;/p&gt;
&lt;p&gt;
	It wasn&amp;rsquo;t until I actually was called to do a code review on an application written by &lt;a href="http://www.bluespire.com/"&gt;Rob Eisenberg&lt;/a&gt; that I really got it. That application was a pretty simple UI (well, the UI logic was simple, the UI itself was pretty complex, but that was mostly because of the visualizations). The interesting thing is that most of the UI was completely driven by the response from the server.&lt;/p&gt;
&lt;p&gt;
	What I mean by that is that when you loaded an entity, it would load the appropriate view, and use information like this:&lt;/p&gt;
&lt;blockquote&gt;
	&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;method&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;DELETE&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Cancel&amp;quot;&amp;nbsp;&lt;/span&gt;&lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;rels/cancelOrder&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;/orders/1234&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
	&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;method&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;GET&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Shipping Details&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;rels/viewShipping&amp;quot; &lt;/span&gt;&lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;/orders/1234/shipping&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
	&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }	&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;
	To generate much of the actual behavior on the client side.&lt;/p&gt;
&lt;p&gt;
	The client was fairly stable, but modifying the server meant that you could get a lot more from the system.&lt;/p&gt;
&lt;p&gt;
	Human readable and hackable urls are nice, sure. But they have very little to do with REST.&lt;/p&gt;
</description><link>http://ayende.com/blog/155009/rest-and-urls?key=d24141cc-9e98-4e70-b97b-6cf16073130e</link><guid>http://ayende.com/blog/155009/rest-and-urls?key=d24141cc-9e98-4e70-b97b-6cf16073130e</guid><pubDate>Mon, 05 Mar 2012 07:53:00 GMT</pubDate></item><item><title>Do you monitor negative events?</title><description>&lt;p&gt;You are probably aware that you need to monitor your production systems for errors, and to add health monitoring for your servers.&lt;/p&gt; &lt;p&gt;But are you monitoring &lt;em&gt;negative&lt;/em&gt; events? What is a negative event, stuff that should have happened and didn’t.&lt;/p&gt; &lt;p&gt;For example, every week you have a process that runs to update the tax rates that applies to your customers. This is implemented as a scheduled process, but for some reason (computer was just being rebooted, the user’s password expire, etc) that process didn’t run. There isn’t an &lt;em&gt;error&lt;/em&gt;, pre se. You won’t get an error because nothing actually had a chance to actually happen.&lt;/p&gt; &lt;p&gt;Another example would be getting a callback confirmation that an order payment has been correctly processed. That usually happen within 1 – 5 minutes, and you get an OK/Fail notification. But what happens if that notification just never came?&lt;/p&gt; &lt;p&gt;This is a much more dangerous scenario, because you have to not only be prepared for handling errors, you have to be prepared for… nothing to happen.&lt;/p&gt; &lt;p&gt;What it means is that you have to have some way to setup expectations in the system, and act on them when you don’t get a confirmation (negative or positive) within a given time frame.&lt;/p&gt;</description><link>http://ayende.com/blog/153409/do-you-monitor-negative-events?key=9a1bc43b-45b8-40c3-983d-391de284122b</link><guid>http://ayende.com/blog/153409/do-you-monitor-negative-events?key=9a1bc43b-45b8-40c3-983d-391de284122b</guid><pubDate>Thu, 23 Feb 2012 10:00:00 GMT</pubDate></item><item><title>Limit your abstractions: And how do you handle testing?</title><description>&lt;p&gt;&lt;img style="display: inline; float: right" align="right" src="http://farm6.staticflickr.com/5086/5267223336_7e10f6f3c8_m.jpg"&gt;On my previous post, I explained about the value of pushing as much as possible to the infrastructure, and then show some code that showed how to do so. First, let us look at the business level code:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;[AcceptVerbs(HttpVerbs.Post)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Register(&lt;span class="kwrd"&gt;string&lt;/span&gt; originUnlocode, &lt;span class="kwrd"&gt;string&lt;/span&gt; destinationUnlocode, DateTime arrivalDeadline)
{
    var trackingId = &lt;font style="background-color: #ffff00"&gt;ExecuteCommand&lt;/font&gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; RegisterCargo
    {
        OriginCode = originUnlocode,
        DestinationCode = destinationUnlocode,
        ArrivalDeadline = arrivalDeadline
    });

    &lt;span class="kwrd"&gt;return&lt;/span&gt; RedirectToAction(ShowActionName, &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteValueDictionary(&lt;span class="kwrd"&gt;new&lt;/span&gt; { trackingId }));
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RegisterCargo : Command&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute()
    {
        var origin = Session.Load&amp;lt;Location&amp;gt;(OriginCode);
        var destination = Session.Load&amp;lt;Location&amp;gt;(DestinationCode);

        var trackingId = Query(&lt;span class="kwrd"&gt;new&lt;/span&gt; NextTrackingIdQuery());

        var routeSpecification = &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteSpecification(origin, destination, ArrivalDeadline);
        var cargo = &lt;span class="kwrd"&gt;new&lt;/span&gt; Cargo(trackingId, routeSpecification);
        Session.Save(cargo);

        Result = trackingId;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; OriginCode { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; DestinationCode { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime ArrivalDeadline { get; set; }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And the infrastructure code, now:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; &lt;font style="background-color: #ffff00"&gt;Default_&lt;/font&gt;&lt;font style="background-color: #ffff00"&gt;ExecuteCommand&lt;/font&gt;(Command cmd)
{
    cmd.Session = Session;
    cmd.Execute();
}

&lt;span class="kwrd"&gt;protected&lt;/span&gt; TResult &lt;font style="background-color: #ffff00"&gt;Default_ExecuteCommand&lt;/font&gt;&amp;lt;TResult&amp;gt;(Command&amp;lt;TResult&amp;gt; cmd)
{
    ExecuteCommand((Command) cmd);
    &lt;span class="kwrd"&gt;return&lt;/span&gt; cmd.Result;
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;You might have noticed a problem in the way we are named things, the names on the action and the infrastructure code do not match. What is going on?&lt;/p&gt;
&lt;p&gt;Well, the answer is quite simple. Let us look at how our controller looks like ( at least, the important parts ):&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AbstractController : Controller
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; ISession Session;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Action&amp;lt;Command&amp;gt; AlternativeExecuteCommand { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Func&amp;lt;Command, &lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; AlternativeExecuteCommandWithResult { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ExecuteCommand(Command cmd)
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (AlternativeExecuteCommand!= &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            AlternativeExecuteCommand(cmd);
        &lt;span class="kwrd"&gt;else&lt;/span&gt;
            Default_ExecuteCommand(cmd);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; TResult ExecuteCommand&amp;lt;TResult&amp;gt;(Command&amp;lt;TResult&amp;gt; cmd)
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (AlternativeExecuteCommandWithResult != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            &lt;span class="kwrd"&gt;return&lt;/span&gt; (TResult)AlternativeExecuteCommandWithResult(cmd);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; Default_ExecuteCommand(cmd);
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Default_ExecuteCommand(Command cmd)
    {
        cmd.Session = Session;
        cmd.Execute();
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; TResult Default_ExecuteCommand&amp;lt;TResult&amp;gt;(Command&amp;lt;TResult&amp;gt; cmd)
    {
        ExecuteCommand((Command)cmd);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; cmd.Result;
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;What?! You do mocking by hand and inject them like that? That is horrible! It is much easier to use a mocking framework and ….&lt;/p&gt;
&lt;p&gt;Yes, it would be, if I was trying to mocking different things all the time. But given that I have &lt;em&gt;very &lt;/em&gt;few abstractions, it make sense to not only build this sort of infrastructure, but to also build infrastructure for those things _in the tests_.&lt;/p&gt;
&lt;p&gt;For example, let us write the test for the action:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;[Fact]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; WillRegisterCargo()
{
  ExecuteAction&amp;lt;CargoAdminController&amp;gt;( c=&amp;gt; c.Register(&lt;span class="str"&gt;"US"&lt;/span&gt;, &lt;span class="str"&gt;"UK"&lt;/span&gt;, DateTime.Today) );
  
  Assert.IsType&amp;lt;RegisterCargo&amp;gt;( &lt;span class="kwrd"&gt;this&lt;/span&gt;.ExecutedCommands[0] );
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img style="display: inline; float: right" alt="Lego-mob 2" align="right" src="http://farm6.staticflickr.com/5249/5266605975_5d1f98d658_m.jpg"&gt;The ExecuteAction method belongs to the &lt;em&gt;test infrastructure&lt;/em&gt;, and it setups the controller to be run under the test scenario. Which allows me to &lt;em&gt;not &lt;/em&gt;execute the command, but to actually get it.&lt;/p&gt;
&lt;p&gt;From there, it is very easy to get to things like:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;[Fact]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; WillCreateNewCargoWithNewTrackingId()
{
  SetupQueryResponse&amp;lt;NextTrackingIdQuery&amp;gt;(&lt;span class="str"&gt;"abc"&lt;/span&gt;);
  ExecuteCommand&amp;lt;RegisterCargo&amp;gt;( &lt;span class="kwrd"&gt;new&lt;/span&gt; RegisterCargo
  {
    OriginCode = &lt;span class="str"&gt;"US"&lt;/span&gt;,
    DestinationCode= &lt;span class="str"&gt;"UK"&lt;/span&gt;,
    ArrivalDeadline = DateTime.Today
  });
  
  var cargo = Session.Load&amp;lt;Cargo&amp;gt;(&lt;span class="str"&gt;"cargos/1"&lt;/span&gt;);
  Assert.Equal(&lt;span class="str"&gt;"abc"&lt;/span&gt;, cargo.TrackingId);
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is important, because now what you are testing is the actual interaction. You don’t care about any of the actual dependencies, we just abstracted them out, but without creating ton of interfaces, abstractions on top of abstractions or any of that.&lt;/p&gt;
&lt;p&gt;In fact, we kept the number of abstractions to a minimum, and we can change pretty much every part of the system with very little fear of cascading change.&lt;/p&gt;
&lt;p&gt;We have similar lego pieces, all of them move together and interact with one another with complete freedom, and we don’t have to have a &lt;a href="http://discuss.joelonsoftware.com/default.asp?joel.3.219431"&gt;Abstract Factory Factory Façade Factory&lt;/a&gt;.&lt;/p&gt;</description><link>http://ayende.com/blog/154273/limit-your-abstractions-and-how-do-you-handle-testing?key=9d736120-389f-4c65-bdee-1bebc60cbd14</link><guid>http://ayende.com/blog/154273/limit-your-abstractions-and-how-do-you-handle-testing?key=9d736120-389f-4c65-bdee-1bebc60cbd14</guid><pubDate>Wed, 22 Feb 2012 07:49:00 GMT</pubDate></item><item><title>Limit your abstractions: The key is in the infrastructure&amp;hellip;</title><description>&lt;p&gt;In my previous post, I discussed actual refactoring to reduce abstraction, and I showed two very interesting methods, Query() and ExecuteCommand(). Here is the code in question:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;[AcceptVerbs(HttpVerbs.Post)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Register(&lt;span class="kwrd"&gt;string&lt;/span&gt; originUnlocode, &lt;span class="kwrd"&gt;string&lt;/span&gt; destinationUnlocode, DateTime arrivalDeadline)
{
    var trackingId = &lt;font style="background-color: #ffff00"&gt;ExecuteCommand&lt;/font&gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; RegisterCargo
    {
        OriginCode = originUnlocode,
        DestinationCode = destinationUnlocode,
        ArrivalDeadline = arrivalDeadline
    });

    &lt;span class="kwrd"&gt;return&lt;/span&gt; RedirectToAction(ShowActionName, &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteValueDictionary(&lt;span class="kwrd"&gt;new&lt;/span&gt; { trackingId }));
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RegisterCargo : Command&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute()
    {
        var origin = Session.Load&amp;lt;Location&amp;gt;(OriginCode);
        var destination = Session.Load&amp;lt;Location&amp;gt;(DestinationCode);

        var trackingId = &lt;font style="background-color: #ffff00"&gt;Query&lt;/font&gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; NextTrackingIdQuery());

        var routeSpecification = &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteSpecification(origin, destination, ArrivalDeadline);
        var cargo = &lt;span class="kwrd"&gt;new&lt;/span&gt; Cargo(trackingId, routeSpecification);
        Session.Save(cargo);

        Result = trackingId;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; OriginCode { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; DestinationCode { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime ArrivalDeadline { get; set; }
}
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;What are they so important? Mostly because those methods [and similar, like Raise(event) and ExecuteLater(task)] are actually the back bone of the application. They are the infrastructure on top of which everything rests.&lt;/p&gt;
&lt;p&gt;Those methods basically accept an argument (and optionally return a value). Their responsibility are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Setup the given argument so it can run.&lt;/li&gt;
&lt;li&gt;Execute it.&lt;/li&gt;
&lt;li&gt;Return the result (if there is one).&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Here is an example showing how to implement ExecuteCommand:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Default_ExecuteCommand(Command cmd)
{
    cmd.Session = Session;
    cmd.Execute();
}

&lt;span class="kwrd"&gt;protected&lt;/span&gt; TResult Default_ExecuteCommand&amp;lt;TResult&amp;gt;(Command&amp;lt;TResult&amp;gt; cmd)
{
    ExecuteCommand((Command) cmd);
    &lt;span class="kwrd"&gt;return&lt;/span&gt; cmd.Result;
}
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;I have code very much like that in &lt;em&gt;production&lt;/em&gt;, because I &lt;em&gt;know&lt;/em&gt; that in this system, there are actually only one or two dependencies that a command may want.&lt;/p&gt;
&lt;p&gt;There are very few other dependencies, because of the limited number of abstractions that we have. This makes things very simple to write and work with.&lt;/p&gt;
&lt;p&gt;Because we abstract away any dependency management, and because we allow only very small number of abstractions, this works very well. The amount of complexity that you have is way down, code reviewing this is very easy, because there isn’t much &lt;em&gt;to &lt;/em&gt;review, and it all follows the same structure. The implementation of the rest are pretty much the same thing.&lt;/p&gt;
&lt;p&gt;There is just one thing left to discuss, because it kept showing up on the comments for the other posts. How do you handle testing?&lt;/p&gt;</description><link>http://ayende.com/blog/154241/limit-your-abstractions-the-key-is-in-the-infrastructurehellip?key=b8321fd5-9738-4139-a8a4-f30f2f257692</link><guid>http://ayende.com/blog/154241/limit-your-abstractions-the-key-is-in-the-infrastructurehellip?key=b8321fd5-9738-4139-a8a4-f30f2f257692</guid><pubDate>Tue, 21 Feb 2012 07:28:00 GMT</pubDate></item><item><title>Limit your abstractions: Refactoring toward reduced abstractions</title><description>&lt;p&gt;So in my previous post I spoke about this code and the complexity behind it:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CargoAdminController : BaseController
{
  [AcceptVerbs(HttpVerbs.Post)]
  &lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Register(
      [ModelBinder(&lt;span class="kwrd"&gt;typeof&lt;/span&gt; (RegistrationCommandBinder))] RegistrationCommand registrationCommand)
  {
      DateTime arrivalDeadlineDateTime = DateTime.ParseExact(registrationCommand.ArrivalDeadline, RegisterDateFormat,
                                                             CultureInfo.InvariantCulture);

      &lt;span class="kwrd"&gt;string&lt;/span&gt; trackingId = BookingServiceFacade.BookNewCargo(
          registrationCommand.OriginUnlocode, registrationCommand.DestinationUnlocode, arrivalDeadlineDateTime
          );

      &lt;span class="kwrd"&gt;return&lt;/span&gt; RedirectToAction(ShowActionName, &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteValueDictionary(&lt;span class="kwrd"&gt;new&lt;/span&gt; {trackingId}));
  }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;In this post, I intend to show how we can refactor things. I am going to do that by flattening the architecture, removing useless abstractions and creating a simpler, easier to work with system.&lt;/p&gt;
&lt;p&gt;The first thing to do is to refactor the method signature:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;[AcceptVerbs(HttpVerbs.Post)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Register(&lt;span class="kwrd"&gt;string&lt;/span&gt; originUnlocode, &lt;span class="kwrd"&gt;string&lt;/span&gt; destinationUnlocode, DateTime arrivalDeadline)&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;Those are three parameters that we need, there is no need to create a model binder, custom command, etc just for this. For that matter, if you already have a model binder, why on earth do you store the date as a string, and not a date time. The framework is quite happy to do the conversion for me, and if it can’t, I can extend the &lt;em&gt;infrastructure to do so&lt;/em&gt;. I don’t need to patch this action with date parsing code.&lt;/p&gt;
&lt;p&gt;Next, we have this notion of booking a new cargo, looking at the service, that looks like:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; BookNewCargo(&lt;span class="kwrd"&gt;string&lt;/span&gt; origin, &lt;span class="kwrd"&gt;string&lt;/span&gt; destination, DateTime arrivalDeadline)
{
    &lt;span class="kwrd"&gt;try&lt;/span&gt;
    {
        TrackingId trackingId = BookingService.BookNewCargo(
            &lt;span class="kwrd"&gt;new&lt;/span&gt; UnLocode(origin),
            &lt;span class="kwrd"&gt;new&lt;/span&gt; UnLocode(destination),
            arrivalDeadline
            );
        &lt;span class="kwrd"&gt;return&lt;/span&gt; trackingId.IdString;
    }
    &lt;span class="kwrd"&gt;catch&lt;/span&gt; (Exception exception)
    {
        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NDDDRemoteBookingException(exception.Message);
    }
}
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;The error handling alone sets my teeth on edge. Also, note that we have a complex type for TrackingId, which contains just a string (there is a lot of code there for IValueObject&amp;lt;T&amp;gt;, comparison, etc), all of which basically go away if you use an actual string. The same is true for UnLocode (UN Location Code, I assume), but at least this one has some validation code in it.&lt;/p&gt;
&lt;p&gt;Then there is the lovely forwarding call, which translate to:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; TrackingId BookNewCargo(UnLocode originUnLocode,
                               UnLocode destinationUnLocode,
                               DateTime arrivalDeadline)
{
    &lt;span class="kwrd"&gt;using&lt;/span&gt; (var transactionScope = &lt;span class="kwrd"&gt;new&lt;/span&gt; TransactionScope())
    {
        TrackingId trackingId = cargoRepository.NextTrackingId();
        Location origin = locationRepository.Find(originUnLocode);
        Location destination = locationRepository.Find(destinationUnLocode);

        Cargo cargo = CargoFactory.NewCargo(trackingId, origin, destination, arrivalDeadline);

        cargoRepository.Store(cargo);
        logger.Info(&lt;span class="str"&gt;"Booked new cargo with tracking id "&lt;/span&gt; + cargo.TrackingId);

        transactionScope.Complete();
        &lt;span class="kwrd"&gt;return&lt;/span&gt; cargo.TrackingId;
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And now we got somewhere, we actually have something there that is actually meaningful. I’ll skip going deeper, I am pretty sure that you can understand what is going on. &lt;/p&gt;
&lt;p&gt;From my point of view of the common abstractions in an application:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Controllers 
&lt;li&gt;Views 
&lt;li&gt;Entities 
&lt;li&gt;Commands 
&lt;li&gt;Tasks 
&lt;li&gt;Events 
&lt;li&gt;Queries&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;Controllers are at the boundaries of the system, they orchestrate the entire system behavior. Note that I have no place for services or repositories in this list. That is quite intentional. Instead of going that route. &lt;/p&gt;
&lt;p&gt;Take a look at the code that I ended up with:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt; [AcceptVerbs(HttpVerbs.Post)]
 &lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Register(&lt;span class="kwrd"&gt;string&lt;/span&gt; originUnlocode, &lt;span class="kwrd"&gt;string&lt;/span&gt; destinationUnlocode, DateTime arrivalDeadline)
 {
     var origin = Session.Load&amp;lt;Location&amp;gt;(originUnlocode);
     var destination = Session.Load&amp;lt;Location&amp;gt;(destinationUnlocode);

     var trackingId = Query(&lt;span class="kwrd"&gt;new&lt;/span&gt; NextTrackingIdQuery());

     var routeSpecification = &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteSpecification(origin, destination, arrivalDeadline);
     var cargo = &lt;span class="kwrd"&gt;new&lt;/span&gt; Cargo(trackingId, routeSpecification);
     Session.Store(cargo);

     &lt;span class="kwrd"&gt;return&lt;/span&gt; RedirectToAction(ShowActionName, &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteValueDictionary(&lt;span class="kwrd"&gt;new&lt;/span&gt; {trackingId}));
 }&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;As you can see, the entire architecture was collapsed into a single method.&lt;/p&gt;
&lt;p&gt;And what kind of abstractions do we have here?&lt;/p&gt;
&lt;p&gt;Well, we have the usual things from MVC, Controller, Action, parameter binding. &lt;/p&gt;
&lt;p&gt;We have the session that we are using to load data by id, and to store the newly create cargo. &lt;/p&gt;
&lt;p&gt;And we have the notion of a query. Generating a new TrackingID is a query that happen on the database (actually implemented as a hilo sequence). That is something that is definitely not the responsibility of the controller action, so we moved it into a query. Note that we have the Query() method there. It is defined as:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; TResult Query&amp;lt;TResult&amp;gt;(Query&amp;lt;TResult&amp;gt; query)&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And NextTrackingIdQuery is defined as:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; NextTrackingIdQuery : Query&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;Pretty simple, overall. And I can hear the nitpickers climb over the fences, waving the pitchforks and torches. “What happen when you need to reuse this logic? It is not in the UI and …”&lt;/p&gt;
&lt;p&gt;There are a couple of things to note here. &lt;/p&gt;
&lt;p&gt;First, there &lt;em&gt;isn’t &lt;/em&gt;anywhere else that needs to book a cargo. And saying “and what happen when…” flies right into a wall of people shouting YAGNI.&lt;/p&gt;
&lt;p&gt;Second, let us assume that there &lt;em&gt;is&lt;/em&gt; such a need, to reuse the booking cargo scenario. How would we approach this?&lt;/p&gt;
&lt;p&gt;Well, we can encapsulate the logic for the controller inside a Command. Which gives us:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;[AcceptVerbs(HttpVerbs.Post)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Register(&lt;span class="kwrd"&gt;string&lt;/span&gt; originUnlocode, &lt;span class="kwrd"&gt;string&lt;/span&gt; destinationUnlocode, DateTime arrivalDeadline)
{
    var trackingId = ExecuteCommand(&lt;span class="kwrd"&gt;new&lt;/span&gt; RegisterCargo
    {
        OriginCode = originUnlocode,
        DestinationCode = destinationUnlocode,
        ArrivalDeadline = arrivalDeadline
    });

    &lt;span class="kwrd"&gt;return&lt;/span&gt; RedirectToAction(ShowActionName, &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteValueDictionary(&lt;span class="kwrd"&gt;new&lt;/span&gt; { trackingId }));
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And then we have the actual RegisterCargo command:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Command
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; IDocumentSession Session { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute();

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; TResult Query&amp;lt;TResult&amp;gt;(Query&amp;lt;TResult&amp;gt; query);
}
&lt;span class="kwrd"&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Command&amp;lt;T&amp;gt; : Command
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; T Result { get; &lt;span class="kwrd"&gt;protected&lt;/span&gt; set; }
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RegisterCargo : Command&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute()
    {
        var origin = Session.Load&amp;lt;Location&amp;gt;(OriginCode);
        var destination = Session.Load&amp;lt;Location&amp;gt;(DestinationCode);

        var trackingId = Query(&lt;span class="kwrd"&gt;new&lt;/span&gt; NextTrackingIdQuery());

        var routeSpecification = &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteSpecification(origin, destination, ArrivalDeadline);
        var cargo = &lt;span class="kwrd"&gt;new&lt;/span&gt; Cargo(trackingId, routeSpecification);
        Session.Save(cargo);

        Result = trackingId;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; OriginCode { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; DestinationCode { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime ArrivalDeadline { get; set; }
}
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;Note that the Command class also have a way to execute queries, in fact, it is the exact same way that we use when we had the code in the controller. We just moved stuff around, not really made any major change, but we can easily start using the same functionality in another location.&lt;/p&gt;
&lt;p&gt;I generally don’t like doing this because most functionality is &lt;em&gt;not&lt;/em&gt; reused, it is specific for a particular place and scenario, but I wanted to show how you can lift some part of the code and move it to a different location, otherwise people would complain about the “lack of reuse opportunities”.&lt;/p&gt;
&lt;p&gt;On my next post I am going to talk about the Query() and ExecuteCommand() methods, and why they are so important.&lt;/p&gt;</description><link>http://ayende.com/blog/154209/limit-your-abstractions-refactoring-toward-reduced-abstractions?key=af15e59a-985a-4bf2-a678-b25136153ed8</link><guid>http://ayende.com/blog/154209/limit-your-abstractions-refactoring-toward-reduced-abstractions?key=af15e59a-985a-4bf2-a678-b25136153ed8</guid><pubDate>Mon, 20 Feb 2012 07:02:00 GMT</pubDate></item><item><title>Limit your abstractions: So what is the whole big deal about?</title><description>&lt;p&gt;
	When I started out, I pointed out that I truly dislike this type of architecture:&lt;/p&gt;
&lt;p&gt;
	&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions-So-what-is-the-w_DAD0/image_2.png"&gt;&lt;img alt="image" border="0" height="605" src="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions-So-what-is-the-w_DAD0/image_thumb.png" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" width="1123" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;
	And I said that I much rather an architecture that has a far more limited set of abstractions, and I gave this example:&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;
		Controllers&lt;/li&gt;
	&lt;li&gt;
		Views&lt;/li&gt;
	&lt;li&gt;
		Entities&lt;/li&gt;
	&lt;li&gt;
		Commands&lt;/li&gt;
	&lt;li&gt;
		Tasks&lt;/li&gt;
	&lt;li&gt;
		Events&lt;/li&gt;
	&lt;li&gt;
		Queries&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
	That is all nice in theory, but let us talk in practice, shall we? How do we actually write code that actually uses this model?&lt;/p&gt;
&lt;p&gt;
	Let us show some code that uses this type of code, this type, this is from the C# port of the same codebase, &lt;a href="http://code.google.com/p/ndddsample/"&gt;available here&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;
	&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CargoAdminController : BaseController
{
  [AcceptVerbs(HttpVerbs.Post)]
  &lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Register(
      [ModelBinder(&lt;span class="kwrd"&gt;typeof&lt;/span&gt; (RegistrationCommandBinder))] RegistrationCommand registrationCommand)
  {
      DateTime arrivalDeadlineDateTime = DateTime.ParseExact(registrationCommand.ArrivalDeadline, RegisterDateFormat,
                                                             CultureInfo.InvariantCulture);

      &lt;span class="kwrd"&gt;string&lt;/span&gt; trackingId = BookingServiceFacade.BookNewCargo(
          registrationCommand.OriginUnlocode, registrationCommand.DestinationUnlocode, arrivalDeadlineDateTime
          );

      &lt;span class="kwrd"&gt;return&lt;/span&gt; RedirectToAction(ShowActionName, &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteValueDictionary(&lt;span class="kwrd"&gt;new&lt;/span&gt; {trackingId}));
  }
}&lt;/pre&gt;
	&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }	&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;
	Does this looks good to you? Here is what is actually going on here.&lt;/p&gt;
&lt;p&gt;
	&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions-So-what-is-the-w_DAD0/image_4.png"&gt;&lt;img alt="image" border="0" height="480" src="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions-So-what-is-the-w_DAD0/image_thumb_1.png" style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" width="452" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;
	You can click on &lt;a href="http://ayende-temp.s3.amazonaws.com/Blog-posts-154177-Diagram.pdf"&gt;this link&lt;/a&gt; look at what is going in here (and I removed some stuff for clarity&amp;rsquo;s sake.&lt;/p&gt;
&lt;p&gt;
	This stuff is &lt;em&gt;complex&lt;/em&gt;, more to the point, it doesn&amp;rsquo;t read naturally, it is hard to make a change without modifying a lot of code. This is scary.&lt;/p&gt;
&lt;p&gt;
	We have a lot of abstractions here, services and repositories and facades and what not. (Mind, each of those thing is an &lt;em&gt;independent abstraction&lt;/em&gt;, not a common one.)&lt;/p&gt;
&lt;p&gt;
	In my next post, I&amp;rsquo;ll show how to refactor this to a much saner model.&lt;/p&gt;
&lt;!--EndFragment--&gt;</description><link>http://ayende.com/blog/154177/limit-your-abstractions-so-what-is-the-whole-big-deal-about?key=c6cb2083-42a3-4eb2-87b9-3ddcd9025f5a</link><guid>http://ayende.com/blog/154177/limit-your-abstractions-so-what-is-the-whole-big-deal-about?key=c6cb2083-42a3-4eb2-87b9-3ddcd9025f5a</guid><pubDate>Thu, 16 Feb 2012 09:10:00 GMT</pubDate></item><item><title>Limit your abstractions: All cookies looks the same to the cookie cutter</title><description>&lt;p&gt;&lt;img style="display: inline; float: right" align="right" src="http://www.thetoyzone.com/wp-content/uploads/2009/02/minifig_wall.jpg"&gt;&lt;/p&gt; &lt;p&gt;One of the major advantages of limiting the number of abstractions you have is that you end up with a lot less “infrastructure” code. This is in quote because a lot of the time I see this type of code doing things like this:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; BookingServiceImpl : IBookingService  
{

  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; IList&amp;lt;Itinerary&amp;gt; RequestPossibleRoutesForCargo(TrackingId trackingId)
  {
    Cargo cargo = cargoRepository.Find(trackingId);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (cargo == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
    {
      &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Itinerary&amp;gt;();
    }

    &lt;span class="kwrd"&gt;return&lt;/span&gt; routingService.FetchRoutesForSpecification(cargo.routeSpecification());
  }
  
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;I don’t want to see stuff like that. Instead, I want to be able to go into any piece of code and figure out by what it &lt;em&gt;is&lt;/em&gt; what it must be &lt;em&gt;doing&lt;/em&gt;. All my code follow fairly similar patterns, and the only differences that I have are actual &lt;em&gt;business&lt;/em&gt; differences.&lt;/p&gt;
&lt;p&gt;Here is the list of common abstractions that I gave before, this time, I am going to go over each one and explain it. &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Controllers – Stand at the edge of the system and manage interaction with the outside world. Can be MVC controllers, MVVM models, WCF Services. 
&lt;li&gt;Views&amp;nbsp; - The actual UI logic that is being executed. Can be MVC views, XAML, or real UI code (you know, that old WinForms stuff &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions-Dealing-with-lim_6374/wlEmoticon-smile_2.png"&gt;).
&lt;li&gt;Entities – Data that is being persisted. 
&lt;li&gt;Commands – A packaged command to do something that will execute immediately. (Usually invoked by controllers).
&lt;li&gt;Tasks – A packaged execution that will be execute at a later point in time (usually async), after the current operation have completed.
&lt;li&gt;Events – Something that happened in the system that is interesting and require action. Common place for business logic and interaction. 
&lt;li&gt;Queries – Packaged query to be executed immediately. Usually only fairly complex ones gets promoted to an actual query object.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;There might be a few others in your system, but for the most part, you would see those types of things over and over and over again.&lt;/p&gt;
&lt;p&gt;Oh, sure, you might have other things as well, but those should be &lt;em&gt;rare&lt;/em&gt;. If you need to display things in multiple currency interacting with a currency service is something that you would need to often, by all means, make it easy to do (how you do &lt;em&gt;that&lt;/em&gt; is usually not important), but the important thing to remember is that those sort of things are one off, and they should &lt;em&gt;remain&lt;/em&gt; one off, not the way you structure the entire app.&lt;/p&gt;
&lt;p&gt;The reason this is important is that once you have this common infrastructure and shape (for lack of a better word), you can start working in a very rapid pace, without being distracted, and making &lt;em&gt;changes&lt;/em&gt; becomes easy. All of your architecture is going through the same central pipes, shifting where they are going is easy to do. You don’t have to drag a rigid system made of a lot of small individual pieces, after all.&lt;!--EndFragment--&gt;&lt;/p&gt;</description><link>http://ayende.com/blog/154145/limit-your-abstractions-all-cookies-looks-the-same-to-the-cookie-cutter?key=91727ff8-a817-4d05-978a-0b8a979f0b88</link><guid>http://ayende.com/blog/154145/limit-your-abstractions-all-cookies-looks-the-same-to-the-cookie-cutter?key=91727ff8-a817-4d05-978a-0b8a979f0b88</guid><pubDate>Wed, 15 Feb 2012 07:22:00 GMT</pubDate></item><item><title>Limit your abstractions: Commands vs. Tasks, did you forget the workflow?</title><description>&lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Limit.-Tasks-did-you-forget-the-workflow_5CAE/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="right" src="http://ayende.com/blog/Images/Windows-Live-Writer/Limit.-Tasks-did-you-forget-the-workflow_5CAE/image_thumb.png" width="238" height="223"&gt;&lt;/a&gt;On my last post, I outlined the major abstractions that I tend to use in my applications.&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Controllers  &lt;li&gt;Views  &lt;li&gt;Entities  &lt;li&gt;&lt;strike&gt;Commands &lt;/strike&gt; &lt;li&gt;Tasks  &lt;li&gt;Events  &lt;li&gt;Queries&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;I also said that I like Tasks much more than Commands and I’ll explain that in the future. When talking about tasks, I usually talk about something that is based on &lt;a href="https://github.com/ayende/RaccoonBlog/tree/master/HibernatingRhinos.Loci.Common/Tasks"&gt;this code&lt;/a&gt;. This give us the ability to write code such as this:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AssignCargoToRoute : BackgroundTask
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; Itinerary Itinerary { get;set; }
  TrackingId TrackingId { get;set; }

  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute()
  {
    
  }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;On the face of it, this is a &lt;em&gt;very&lt;/em&gt; &lt;em&gt;similar &lt;/em&gt;to what we have had before. So why am I so much in favor of tasks rather than commands?&lt;/p&gt;
&lt;p&gt;Put simply, the promises that they make are different.&amp;nbsp; A command will execute immediately, this is good when we are encapsulating common or complex piece of logic. We give it a meaningful name and move on with our lives.&lt;/p&gt;
&lt;p&gt;The problem is that in many cases, executing immediately is something that we &lt;em&gt;don’t &lt;/em&gt;want. Why is that?&lt;/p&gt;
&lt;p&gt;Well, what happen if this can take a while? What if this requires touching a remote resource (one that can’t take part of our transaction)? What happen if we want this to execute, but only if the entire operation have been successful? How do we handle errors? What happen when the scenario calls for a complex workflow? Can I partially succeed in what I am doing? Can you have a compensating action if some part fail? All of those scenarios basically boil down to “I don’t want to execute it now, I want the execution to be managed for me”.&lt;/p&gt;
&lt;p&gt;Thing about the scenario that we actually have here. We need to assign a cargo to a route. But what does that &lt;em&gt;means&lt;/em&gt;? In the trivial example, we do that by updating some data in our local database. But in real world scenario, something like that tends to be much more complex. We need to calculate shipping charges, check manifest, verify that we have all the proper permits, etc. All of that takes time, and usually collaboration with external systems. &lt;/p&gt;
&lt;p&gt;For the most part, I find that real world systems requires a lot more tasks than commands. Mostly because it is actually rare to have complex interaction inside your own system. If you do, you have to be cautious that you aren’t adding too much complexity. It is the external interactions that tends to makes life… interesting.&lt;/p&gt;
&lt;p&gt;This has implications on how we are building the system, because we don’t assume immediate execution and the temporal coupling that comes with it.&lt;/p&gt;</description><link>http://ayende.com/blog/154113/limit-your-abstractions-commands-vs-tasks-did-you-forget-the-workflow?key=7e476126-c7d0-4a63-b5d0-ed8e3fb5a0bc</link><guid>http://ayende.com/blog/154113/limit-your-abstractions-commands-vs-tasks-did-you-forget-the-workflow?key=7e476126-c7d0-4a63-b5d0-ed8e3fb5a0bc</guid><pubDate>Tue, 14 Feb 2012 07:35:00 GMT</pubDate></item><item><title>Limit your abstractions: You only get six to a dozen in the entire app</title><description>&lt;p&gt;
	Let us take a look at another part of the &lt;a href="http://dddsample.sourceforge.net/index.html"&gt;DDD sample application&lt;/a&gt;. This time, the booking service.&lt;/p&gt;
&lt;p&gt;
	&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions_4CC1/image_2.png"&gt;&lt;img alt="image" border="0" height="180" src="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions_4CC1/image_thumb.png" style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" width="595" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;
	One thing that is really glaring at me is that we have a mix of both commands and queries in this interface. Also, just consider the name. It is a Booking service, but it doesn&amp;rsquo;t actually seem to have an actual meaning in the application itself. There is no entity named Booking, and except for the BookNewCargo, there is no mention of booking anywhere in the application.&lt;/p&gt;
&lt;p&gt;
	You know what, maybe there is logic in the RequestPossibleRoutesForCargo that is actually meaningful beyond a simple query. Such as charging the customer for the calculation, or reserving space on the route that we selected, etc.&lt;/p&gt;
&lt;p&gt;
	At any rate, I don&amp;rsquo;t like this service at all. In fact, any time that you have something that is called XyzService, you ought to suspect it.&amp;nbsp; Let me take this one step further. I don&amp;rsquo;t like that it is an interface, and I don&amp;rsquo;t like how it is composed. I don&amp;rsquo;t see it as an independent thing. Going further than that, I don&amp;rsquo;t really see a reason why we would need an interface here. You might have noticed what the title of this series is. I want to limit abstraction, and IBookingService is an abstraction, one that I don&amp;rsquo;t see any value in.&lt;/p&gt;
&lt;p&gt;
	In most applications, I like to have a &lt;em&gt;very&lt;/em&gt; small number of abstractions. Usually in the order of half a dozen to a dozen (top!). I usually think about them like this:&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;
		Controllers&lt;/li&gt;
	&lt;li&gt;
		Views&lt;/li&gt;
	&lt;li&gt;
		Entities&lt;/li&gt;
	&lt;li&gt;
		Commands&lt;/li&gt;
	&lt;li&gt;
		Tasks&lt;/li&gt;
	&lt;li&gt;
		Events&lt;/li&gt;
	&lt;li&gt;
		Queries&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
	Sometimes you have a few more, but those are the major ones. Controllers,Views and Entities are fairly obvious, I would imagine. But what about the rest?&lt;/p&gt;
&lt;p&gt;
	Command is a package &amp;ldquo;thing&amp;rdquo; that happen immediately. Tasks are very like commands, except that they don&amp;rsquo;t have an explicit execution date and Events allow you to build smarts into the system. We have already seen how I handle events, in the previous posts in this series. And Queries should be fairly obvious as well.&lt;/p&gt;
&lt;p&gt;
	Let us tackle Commands now, and then explain why they are bad later on.&lt;/p&gt;
&lt;p&gt;
	When I said that I don&amp;rsquo;t want abstractions, I meant that I don&amp;rsquo;t want an interface and an implementation, and some way to connect the two, etc. I don&amp;rsquo;t really see a lot of value in that for the common case.&lt;/p&gt;
&lt;p&gt;
	Let us break it apart into commands, which would give us this:&lt;/p&gt;
&lt;blockquote&gt;
	&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AssignCargoToRoute : Command
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; Itinerary Itinerary { get;set; }
  TrackingId TrackingId { get;set; }

  &lt;span class="kwrd"&gt;public&lt;/span&gt; override &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute()
  {
    
  }
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; BookNewCargo : Command
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; UnLocode Origin {get;set;}
  &lt;span class="kwrd"&gt;public&lt;/span&gt; UnLocode Destination { get;set; }
  &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime ArrivalDeadline {get;set;}

  &lt;span class="kwrd"&gt;public&lt;/span&gt; TrackingId Result {get;set;}&lt;/pre&gt;
	&lt;pre class="csharpcode"&gt;
  &lt;span class="kwrd"&gt;public&lt;/span&gt; override void Execute()
  {
    
  }
}&lt;/pre&gt;
	&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }	&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;
	I am very fond in having a base class for those types of things. The base class provide me with the infrastructure support for the command in question.&lt;/p&gt;
&lt;p&gt;
	In my next post, I&amp;rsquo;ll go over why I don&amp;rsquo;t like this approach, and discuss other ways to structure things so it is more suitable for an actual application.&lt;/p&gt;
</description><link>http://ayende.com/blog/154081/limit-your-abstractions-you-only-get-six-to-a-dozen-in-the-entire-app?key=60fd31cb-4b9b-47f4-b357-6bc09b858581</link><guid>http://ayende.com/blog/154081/limit-your-abstractions-you-only-get-six-to-a-dozen-in-the-entire-app?key=60fd31cb-4b9b-47f4-b357-6bc09b858581</guid><pubDate>Mon, 13 Feb 2012 10:00:00 GMT</pubDate></item><item><title>Limit your abstractions: Application Events&amp;ndash;event processing and RX</title><description>&lt;p&gt;In my last post, I mentioned that this is actually an event processing system, so we might as well use actual event processing and see what we can gain out of this. I chose to use RX (reactive extensions), which can turn a series of events into a linq statement. This is incredibly powerful, and has some interesting implications when you combine this with your architecture. In particular, let us see what we can get when we set out to replace this with RX based event processing style.&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions-Application-Even_44F1/image_thumb3_thumb_thumb_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image_thumb3_thumb_thumb" border="0" alt="image_thumb3_thumb_thumb" src="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions-Application-Even_44F1/image_thumb3_thumb_thumb_thumb.png" width="907" height="180"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;We can get to something like this very easily:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CargoProcessor : EventsProcessor
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; CargoProcessor()
    {
        On&amp;lt;Cargo&amp;gt;(cargos =&amp;gt;
            from cargo &lt;span class="kwrd"&gt;in&lt;/span&gt; cargos
            &lt;span class="kwrd"&gt;where&lt;/span&gt; cargo.Delivery.Misdirected
            select MisdirectedCargo(cargo)
            );

        On&amp;lt;Cargo&amp;gt;(cargos =&amp;gt;
            from cargo &lt;span class="kwrd"&gt;in&lt;/span&gt; cargos
            &lt;span class="kwrd"&gt;where&lt;/span&gt; cargo.Delivery.UnloadedAtDestination
            select CaroArrived(cargo)
        );
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; CaroArrived(Cargo cargo)
    {
        &lt;span class="rem"&gt;// handle event&lt;/span&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; MisdirectedCargo(Cargo cargo)
    {
        &lt;span class="rem"&gt;// handle event&lt;/span&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;We use RX to handle the linq processing over the events, and in EventsProcessor we have very little code, probably just:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EventsProcessor
    {
        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; List&amp;lt;Func&amp;lt;IObservable&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;, IObservable&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;&amp;gt;  actions = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Func&amp;lt;IObservable&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;, IObservable&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;&amp;gt;();

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; On&amp;lt;T&amp;gt;(Func&amp;lt;IObservable&amp;lt;T&amp;gt;, IObservable&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; action)
        {
            actions.Add(observable =&amp;gt; action(observable.OfType&amp;lt;T&amp;gt;()));
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute(IObservable&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; observable)
        {
            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var action &lt;span class="kwrd"&gt;in&lt;/span&gt; actions)
            {
                action(observable).Subscribe();
            }
        }
    }&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Elsewhere in the code we setup the actual Obsersable that we pass to all the EventsProcessors. The major advantages that we have with this style is that we have a natural syntax to do selection on the events that interest us, including fairly complex one. We still have easy time of creating new EventsProcessors if we want, but because the code for defining the selection is so compact, we can usually put related stuff together, which is going to be very helpful for making sure that the codebase is readable.&lt;/p&gt;
&lt;p&gt;And, naturally, this method extends itself to handling events of multiple types in the same place. For example, if we want to also handle the HandlingEvent, we can do it in place, because it is very much related to the Cargo, it seems.&lt;/p&gt;</description><link>http://ayende.com/blog/154049/limit-your-abstractions-application-eventsndash-event-processing-and-rx?key=f971dbd5-bdaa-4fd1-bd0f-518b105d0509</link><guid>http://ayende.com/blog/154049/limit-your-abstractions-application-eventsndash-event-processing-and-rx?key=f971dbd5-bdaa-4fd1-bd0f-518b105d0509</guid><pubDate>Fri, 10 Feb 2012 07:55:00 GMT</pubDate></item><item><title>Limit your abstractions: Application Events&amp;ndash;Proposed Solution #2&amp;ndash;Cohesion</title><description>&lt;p&gt;In my previous post, I spoke about ISP and how we can replace the following code with something that is easier to follow:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions_41F4/image_thumb3_thumb_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image_thumb3_thumb" border="0" alt="image_thumb3_thumb" src="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions_41F4/image_thumb3_thumb_thumb.png" width="907" height="180"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;I proposed something like:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IHappenOn&amp;lt;T&amp;gt;
{
   &lt;span class="kwrd"&gt;void&lt;/span&gt; Inspect(T item);
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;Which would be invoked using:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;container.ExecuteAll&amp;lt;IHappenOn&amp;lt;Cargo&amp;gt;&amp;gt;(i=&amp;gt;i.Inspect(cargo));&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;Or something like that. &lt;/p&gt;
&lt;p&gt;Which lead us to the following code:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CargoArrived : IHappenedOn&amp;lt;Cargo&amp;gt;
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Inspect(Cargo cargo)
  {
    &lt;span class="kwrd"&gt;if&lt;/span&gt;(cargo.Delivery.UnloadedAtDestination == &lt;span class="kwrd"&gt;false&lt;/span&gt;)
      &lt;span class="kwrd"&gt;return&lt;/span&gt;;
      
    &lt;span class="rem"&gt;// handle event&lt;/span&gt;
  }
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CargoMisdirected : IHappenedOn&amp;lt;Cargo&amp;gt;
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Inspect(Cargo cargo)
  {
    &lt;span class="kwrd"&gt;if&lt;/span&gt;(cargo.Delivery.Misdirected == &lt;span class="kwrd"&gt;false&lt;/span&gt;)
      &lt;span class="kwrd"&gt;return&lt;/span&gt;;
      
    &lt;span class="rem"&gt;// handle event&lt;/span&gt;
  }
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CargoHandled : IHappenOn&amp;lt;HandlingEvent&amp;gt;
{
   &lt;span class="rem"&gt;// etc&lt;/span&gt;
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EventRegistrationAttempt : IHappenedOn&amp;lt;HandlingEventRegistrationAttempt&amp;gt;
{
  &lt;span class="rem"&gt;// etc&lt;/span&gt;
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;But I don’t really like this code, to be perfectly frank. It seems to me like there isn’t really a good reason why CargoArrived and CargoMisdirected are located in different classes. It is likely that there is going to be a lot of commonalities between the different types of handling events on cargo. We might as well merge them together for now, giving us:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CargoHappened : IHappenedOn&amp;lt;Cargo&amp;gt;
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Inspect(Cargo cargo)
  {
    &lt;span class="kwrd"&gt;if&lt;/span&gt;(cargo.Delivery.UnloadedAtDestination)
      CargoArrived(cargo);
      
    
    &lt;span class="kwrd"&gt;if&lt;/span&gt;(cargo.Delivery.Misdirected)
      CargoMisdirected(cargo);
      
  }
  
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CargoArrived(Cargo cargo)
  {
    &lt;span class="rem"&gt;// handle event&lt;/span&gt;
  }
  
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CargoMisdirected(Cargo cargo)
  {
    &lt;span class="rem"&gt;//handle event&lt;/span&gt;
  }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;This code put a lot of the cargo handling in one place, making it easier to follow and understand. At the same time, the &lt;em&gt;architecture&lt;/em&gt; gives us the option to split it to different classes at any time. We aren’t going to end up with a God class for Cargo handling. But as long as it make sense, we can keep them together. &lt;/p&gt;
&lt;p&gt;I like this style of event processing, but we can probably do better job at if if we actually used event processing semantics here. I’ll discuss that in my next post.&lt;/p&gt;</description><link>http://ayende.com/blog/154017/limit-your-abstractions-application-eventsndash-proposed-solution-2ndash-cohesion?key=31944b56-4603-43ec-aff0-f84329802cce</link><guid>http://ayende.com/blog/154017/limit-your-abstractions-application-eventsndash-proposed-solution-2ndash-cohesion?key=31944b56-4603-43ec-aff0-f84329802cce</guid><pubDate>Thu, 09 Feb 2012 00:41:00 GMT</pubDate></item><item><title>Limiting your abstractions: Reflections on the Interface Segregation Principle</title><description>&lt;p&gt;I have found two definitions for ISP (in this post, I assume that you know what ISP is, if you don’t, look at the links below).&lt;/p&gt; &lt;p&gt;&lt;a href="http://www.objectmentor.com/resources/articles/isp.pdf"&gt;This one&lt;/a&gt; I can fully agree with:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Clients should not be forced to depend upon interfaces that they don't use.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;And &lt;a href="http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf"&gt;this one&lt;/a&gt; I strongly disagree with: &lt;blockquote&gt; &lt;p&gt;Many client specific interfaces are better than one general purpose interface&lt;br&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;For fun, both of those links are from Object Mentor.  &lt;p&gt;It would be more accurate to say that I don’t so much disagree with the &lt;em&gt;intent&lt;/em&gt; of the second statement, but that I disagree with the wording. One (literal) way of understanding the second statement is to say that we want specific interfaces over generic ones. And I would strongly disagree. An interface is an abstraction (for the most part), and I want to reduce the amount that I have in my system.  &lt;p&gt;Let us look at an example, based on my recent posts in this series. &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Reflections-on-the-Interface-Segregation_388F/image_thumb%5B3%5D_thumb_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image_thumb[3]_thumb" border="0" alt="image_thumb[3]_thumb" src="http://ayende.com/blog/Images/Windows-Live-Writer/Reflections-on-the-Interface-Segregation_388F/image_thumb%5B3%5D_thumb_thumb.png" width="907" height="180"&gt;&lt;/a&gt; &lt;p&gt;In my previous post, I said that I want to remove both CargoWasHandled and CargoWasMisdirected in favor of IHappenOnCargoInspection. But what about the two other methods?&lt;/p&gt; &lt;p&gt;Would ISP force me to have IHappenOnEventRegistrationAttempt and IHappenOnCargoHandling?&lt;/p&gt; &lt;p&gt;Sure, those are many specific interfaces, but are they really better than something like IHappenOn&amp;lt;T&amp;gt;? Is there something truly meaningful that gets lost when we have the generic interface?&lt;/p&gt; &lt;p&gt;I would say that this isn’t the case, further more, I would actually state that having a single interface will lead to more maintainable code, because there are less abstractions in the code. There is a codebase that is more cohesive and easier to understand.&lt;/p&gt; &lt;p&gt;But I’ll talk more about cohesion on my next post.&lt;/p&gt;</description><link>http://ayende.com/blog/153985/limiting-your-abstractions-reflections-on-the-interface-segregation-principle?key=89e01e5d-16c1-488e-a70e-477241373220</link><guid>http://ayende.com/blog/153985/limiting-your-abstractions-reflections-on-the-interface-segregation-principle?key=89e01e5d-16c1-488e-a70e-477241373220</guid><pubDate>Wed, 08 Feb 2012 07:35:00 GMT</pubDate></item><item><title>Limit your abstractions: Application Events&amp;ndash;Proposed Solution #1</title><description>&lt;p&gt;In my previous post, I explained why I really don’t the following. &lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions-Application-Even_3856/image_thumb%5B3%5D_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image_thumb[3]" border="0" alt="image_thumb[3]" src="http://ayende.com/blog/Images/Windows-Live-Writer/Limit-your-abstractions-Application-Even_3856/image_thumb%5B3%5D_thumb.png" width="907" height="180"&gt;&lt;/a&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CargoInspectionServiceImpl : ICargoInspectionService 
{
  &lt;span class="rem"&gt;// code redacted for simplicity&lt;/span&gt;

 &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; InspectCargo(TrackingId trackingId)
 {
    Validate.NotNull(trackingId, &lt;span class="str"&gt;"Tracking ID is required"&lt;/span&gt;);

    Cargo cargo = cargoRepository.Find(trackingId);
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (cargo == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
    {
      logger.Warn(&lt;span class="str"&gt;"Can't inspect non-existing cargo "&lt;/span&gt; + trackingId);
      &lt;span class="kwrd"&gt;return&lt;/span&gt;;
    }

    HandlingHistory handlingHistory = handlingEventRepository.LookupHandlingHistoryOfCargo(trackingId);

    cargo.DeriveDeliveryProgress(handlingHistory);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (cargo.Delivery.Misdirected)
    {
      applicationEvents.CargoWasMisdirected(cargo);
    }

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (cargo.Delivery.UnloadedAtDestination)
    {
      applicationEvents.CargoHasArrived(cargo);
    }

    cargoRepository.Store(cargo);
 }
}&lt;/pre&gt;
&lt;p&gt;Now, let us see one proposed solution for that. We can drop the IApplicationEvents.CargoHasArrived and IApplicationEvents.CargoWasMisdirected, instead creating the following:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IHappenOnCargoInspection
{
   &lt;span class="kwrd"&gt;void&lt;/span&gt; Inspect(Cargo cargo);
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;We can have multiple implementations of this interface, such as this one:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MidirectedCargo : IHappenOnCargoInspection
{
   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Inspect(Cargo cargo)
   {
        &lt;span class="kwrd"&gt;if&lt;/span&gt;(cargo.Delivery.Misdirected == &lt;span class="kwrd"&gt;false&lt;/span&gt;)
             &lt;span class="kwrd"&gt;return&lt;/span&gt;;

        &lt;span class="rem"&gt;// code to handle misdirected cargo.&lt;/span&gt;
   }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;In a similar fashion, we would have a CargoArrived implementation, and the ICargoInspectionService would be tasked with managing the implementation of IHappenOnCargoInspection, probably through a container. Although I would probably replace it with something like:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;container.ExecuteOnAll&amp;lt;IHappenOnCargoInspection&amp;gt;(i=&amp;gt;i.Inspect(cargo));&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;All in all, it is a simple method, but it means that now the responsibility to detect and act is centralized in each cargo inspector implementation. If the detection of misdirected cargo is changed, we know that there is just one place to make that change. If we need a new behavior, for example, for late cargo, we can do that by introducing a new class, which implement the interface. That gives us the Open Closed Principle. &lt;/p&gt;
&lt;p&gt;This is better, but I still don’t like it. There are better methods than that, but we will discuss them in another post.&lt;/p&gt;</description><link>http://ayende.com/blog/153953/limit-your-abstractions-application-eventsndash-proposed-solution-1?key=662dc140-bdc3-4774-b9dd-0e435daf3cb0</link><guid>http://ayende.com/blog/153953/limit-your-abstractions-application-eventsndash-proposed-solution-1?key=662dc140-bdc3-4774-b9dd-0e435daf3cb0</guid><pubDate>Tue, 07 Feb 2012 07:30:00 GMT</pubDate></item></channel></rss>
