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

<channel>
	<title>Udi Dahan - The Software Simplist &#187; Business Rules</title>
	<atom:link href="http://www.udidahan.com/category/business-rules/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.udidahan.com</link>
	<description>Enterprise Development Expert &#38; SOA Specialist</description>
	<lastBuildDate>Mon, 08 Mar 2010 14:34:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Clarified CQRS</title>
		<link>http://www.udidahan.com/2009/12/09/clarified-cqrs/</link>
		<comments>http://www.udidahan.com/2009/12/09/clarified-cqrs/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 14:57:19 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1149</guid>
		<description><![CDATA[
After listening how the community has interpreted Command-Query Responsibility Segregation I think that the time has come for some clarification. Some have been tying it together to Event Sourcing. Most have been overlaying their previous layered architecture assumptions on it. Here I hope to identify CQRS itself, and describe in which places it can connect [...]]]></description>
			<content:encoded><![CDATA[<p><img src="/wp-content/uploads/clarification.png" style="float:right; margin-left:10px; margin-bottom:10px" alt="clarification" title="clarification" /><br />
After listening how the community has interpreted Command-Query Responsibility Segregation I think that the time has come for some clarification. Some have been tying it together to Event Sourcing. Most have been overlaying their previous layered architecture assumptions on it. Here I hope to identify CQRS itself, and describe in which places it can connect to other patterns.</p>
<p><a href="/wp-content/uploads/Clarified_CQRS.pdf">Download as PDF</a> &#8211; this is quite a long post.</p>
<h3>Why CQRS</h3>
<p>Before describing the details of CQRS we need to understand the two main driving forces behind it: collaboration and staleness.</p>
<p>Collaboration refers to circumstances under which multiple actors will be using/modifying the same set of data &#8211; whether or not the intention of the actors is actually to collaborate with each other. There are often rules which indicate which user can perform which kind of modification and modifications that may have been acceptable in one case may not be acceptable in others. We&#8217;ll give some examples shortly. Actors can be human like normal users, or automated like software. </p>
<p>Staleness refers to the fact that in a collaborative environment, once data has been shown to a user, that same data may have been changed by another actor &#8211; it is stale. Almost any system which makes use of a cache is serving stale data &#8211; often for performance reasons. What this means is that we cannot entirely trust our users decisions, as they could have been made based on out-of-date information.</p>
<p>Standard layered architectures don&#8217;t explicitly deal with either of these issues. While putting everything in the same database may be one step in the direction of handling collaboration, staleness is usually exacerbated in those architectures by the use of caches as a performance-improving afterthought.</p>
<h3>A picture for reference</h3>
<p>I&#8217;ve given some talks about CQRS using this diagram to explain it:</p>
<p><img src="/wp-content/uploads/cqrs.png" width="500" height="319" alt="CQRS" title="CQRS" /></p>
<p>The boxes named AC are Autonomous Components. We&#8217;ll describe what makes them autonomous when discussing commands. But before we go into the complicated parts, let&#8217;s start with queries:</p>
<h3>Queries</h3>
<p>If the data we&#8217;re going to be showing users is stale anyway, is it really necessary to go to the master database and get it from there? Why transform those 3rd normal form structures to domain objects if we just want data &#8211; not any rule-preserving behaviors? Why transform those domain objects to DTOs to transfer them across a wire, and who said that wire has to be exactly there? Why transform those DTOs to view model objects?</p>
<p>In short, it looks like we&#8217;re doing a heck of a lot of unnecessary work based on the assumption that reusing code that has already been written will be easier than just solving the problem at hand. Let&#8217;s try a different approach:</p>
<p>How about we create an additional data store whose data can be a bit out of sync with the master database &#8211; I mean, the data we&#8217;re showing the user is stale anyway, so why not reflect in the data store itself. We&#8217;ll come up with an approach later to keep this data store more or less in sync.</p>
<p>Now, what would be the correct structure for this data store? How about just like the view model? One table for each view. Then our client could simply SELECT * FROM MyViewTable (or possibly pass in an ID in a where clause), and bind the result to the screen. That would be just as simple as can be. You could wrap that up with a thin facade if you feel the need, or with stored procedures, or using <a href="http://automapper.codeplex.com/">AutoMapper</a> which can simply map from a data reader to your view model class. The thing is that the view model structures are already wire-friendly, so you don&#8217;t need to transform them to anything else.</p>
<p>You could even consider taking that data store and putting it in your web tier. It&#8217;s just as secure as an in-memory cache in your web tier. Give your web servers SELECT only permissions on those tables and you should be fine.</p>
<h3>Query Data Storage</h3>
<p>While you can use a regular database as your query data store it isn&#8217;t the only option. Consider that the query schema is in essence identical to your view model. You don&#8217;t have any relationships between your various view model classes, so you shouldn&#8217;t need any relationships between the tables in the query data store.</p>
<p>So do you actually need a <i>relational</i> database?</p>
<p>The answer is no, but for all practical purposes and due to organizational inertia, it is probably your best choice (for now).</p>
<h3>Scaling Queries</h3>
<p>Since your queries are now being performed off of a separate data store than your master database, and there is no assumption that the data that&#8217;s being served is 100% up to date, you can easily add more instances of these stores without worrying that they don&#8217;t contain the exact same data. The same mechanism that updates one instance can be used for many instances, as we&#8217;ll see later.</p>
<p>This gives you cheap horizontal scaling for your queries. Also, since your not doing nearly as much transformation, the latency per query goes down as well. Simple code is fast code.</p>
<h3>Data modifications</h3>
<p>Since our users are making decisions based on stale data, we need to be more discerning about which things we let through. Here&#8217;s a scenario explaining why:</p>
<p>Let&#8217;s say we have a customer service representative who is one the phone with a customer. This user is looking at the customer&#8217;s details on the screen and wants to make them a &#8216;preferred&#8217; customer, as well as modifying their address, changing their title from Ms to Mrs, changing their last name, and indicating that they&#8217;re now married. What the user doesn&#8217;t know is that after opening the screen, an event arrived from the billing department indicating that this same customer doesn&#8217;t pay their bills &#8211; they&#8217;re delinquent. At this point, our user submits their changes.</p>
<p>Should we accept their changes?</p>
<p>Well, we should accept some of them, but not the change to &#8216;preferred&#8217;, since the customer is delinquent. But writing those kinds of checks is a pain &#8211; we need to do a diff on the data, infer what the changes mean, which ones are related to each other (name change, title change) and which are separate, identify which data to check against &#8211; not just compared to the data the user retrieved, but compared to the current state in the database, and then reject or accept. </p>
<p>Unfortunately for our users, we tend to reject the whole thing if any part of it is off. At that point, our users have to refresh their screen to get the up-to-date data, and retype in all the previous changes, hoping that this time we won&#8217;t yell at them because of an optimistic concurrency conflict.</p>
<p>As we get larger entities with more fields on them, we also get more actors working with those same entities, and the higher the likelihood that something will touch some attribute of them at any given time, increasing the number of concurrency conflicts. </p>
<p>If only there was some way for our users to provide us with the right level of granularity and intent when modifying data. That&#8217;s what commands are all about.</p>
<h3>Commands</h3>
<p>A core element of CQRS is rethinking the design of the user interface to enable us to capture our users&#8217; intent such that making a customer preferred is a different unit of work for the user than indicating that the customer has moved or that they&#8217;ve gotten married. Using an Excel-like UI for data changes doesn&#8217;t capture intent, as we saw above.</p>
<p>We could even consider allowing our users to submit a new command even before they&#8217;ve received confirmation on the previous one. We could have a little widget on the side showing the user their pending commands, checking them off asynchronously as we receive confirmation from the server, or marking them with an X if they fail. The user could then double-click that failed task to find information about what happened.</p>
<p>Note that the client <i>sends</i> commands to the server &#8211; it doesn&#8217;t publish them. Publishing is reserved for events which state a fact &#8211; that something has happened, and that the publisher has no concern about what receivers of that event do with it.</p>
<h3>Commands and Validation</h3>
<p>In thinking through what could make a command fail, one topic that comes up is validation. Validation is different from business rules in that it states a context-independent fact about a command. Either a command is valid, or it isn&#8217;t. Business rules on the other hand are context dependent.</p>
<p>In the example we saw before, the data our customer service rep submitted was valid, it was only due to the billing event arriving earlier which required the command to be rejected. Had that billing event not arrived, the data would have been accepted.</p>
<p>Even though a command may be valid, there still may be reasons to reject it.</p>
<p>As such, validation can be performed on the client, checking that all fields required for that command are there, number and date ranges are OK, that kind of thing. The server would still validate all commands that arrive, not trusting clients to do the validation.</p>
<h3>Rethinking UIs and commands in light of validation</h3>
<p>The client can make of the query data store when validating commands. For example, before submitting a command that the customer has moved, we can check that the street name exists in the query data store.</p>
<p>At that point, we may rethink the UI and have an auto-completing text box for the street name, thus ensuring that the street name we&#8217;ll pass in the command will be valid. But why not take things a step further? Why not pass in the street ID instead of its name? Have the command represent the street not as a string, but as an ID (int, guid, whatever).</p>
<p>On the server side, the only reason that such a command would fail would be due to concurrency &#8211; that someone had deleted that street and that that hadn&#8217;t been reflected in the query store yet; a fairly exceptional set of circumstances. </p>
<h3>Reasons valid commands fail and what to do about it</h3>
<p>So we&#8217;ve got a well-behaved client that is sending valid commands, yet the server still decides to reject them. Often the circumstances for the rejection are related to other actors changing state relevant to the processing of that command.</p>
<p>In the CRM example above, it is only because the billing event arrived first. But &#8220;first&#8221; could be a millisecond before our command. What if our user pressed the button a millisecond earlier? Should that actually change the <b>business outcome</b>? Shouldn&#8217;t we expect our system to behave the same when observed from the outside?</p>
<p>So, if the billing event arrived second, shouldn&#8217;t that revert preferred customers to regular ones? Not only that, but shouldn&#8217;t the customer be notified of this, like by sending them an email? In which case, why not have this be the behavior for the case where the billing event arrives first? And if we&#8217;ve already got a notification model set up, do we really need to return an error to the customer service rep? I mean, it&#8217;s not like they can do anything about it <b>other than notifying the customer</b>.</p>
<p>So, if we&#8217;re not returning errors to the client (who is already sending us valid commands), maybe all we need to do on the client when sending a command is to tell the user &#8220;thank you, you will receive confirmation via email shortly&#8221;. We don&#8217;t even need the UI widget showing pending commands. </p>
<h3>Commands and Autonomy</h3>
<p>What we see is that in this model, commands don&#8217;t need to be processed immediately &#8211; they can be queued. How fast they get processed is a question of Service-Level Agreement (SLA) and not architecturally significant. This is one of the things that makes that node that processes commands autonomous from a runtime perspective &#8211; we don&#8217;t require an always-on connection to the client.</p>
<p>Also, we shouldn&#8217;t need to access the query store to process commands &#8211; any state that is needed should be managed by the autonomous component &#8211; that&#8217;s part of the meaning of autonomy.</p>
<p>Another part is the issue of failed message processing due to the database being down or hitting a deadlock. There is no reason that such errors should be returned to the client &#8211; we can just rollback and try again. When an administrator brings the database back up, all the message waiting in the queue will then be processed successfully and our users receive confirmation.</p>
<p>The system as a whole is quite a bit more robust to any error conditions.</p>
<p>Also, since we don&#8217;t have queries going through this database any more, the database itself is able to keep more rows/pages in memory which serve commands, improving performance. When both commands and queries were being served off of the same tables, the database server was always juggling rows between the two.</p>
<h3>Autonomous Components</h3>
<p>While in the picture above we see all commands going to the same AC, we could logically have each command processed by a different AC, each with it&#8217;s own queue. That would give us visibility into which queue was the longest, letting us see very easily which part of the system was the bottleneck. While this is interesting for developers, it is critical for system administrators.</p>
<p>Since commands wait in queues, we can now add more processing nodes behind those queues (using the distributor with NServiceBus) so that we&#8217;re only scaling the part of the system that&#8217;s slow. No need to waste servers on any other requests.</p>
<h3>Service Layers</h3>
<p>Our command processing objects in the various autonomous components actually make up our service layer. The reason you don&#8217;t see this layer explicitly represented in CQRS is that it isn&#8217;t really there, at least not as an identifiable logical collection of related objects &#8211; here&#8217;s why:</p>
<p>In the <a href="http://en.wikipedia.org/wiki/Multitier_architecture">layered architecture</a> (AKA 3-Tier) approach, there is no statement about dependencies between objects within a layer, or rather it is implied to be allowed. However, when taking a command-oriented view on the service layer, what we see are objects handling different types of commands. Each command is independent of the other, so why should we allow the objects which handle them to depend on each other?</p>
<p>Dependencies are things which should be avoided, unless there is good reason for them.</p>
<p>Keeping the command handling objects independent of each other will allow us to more easily version our system, one command at a time, not needing even to bring down the entire system, given that the new version is backwards compatible with the previous one.</p>
<p>Therefore, keep each command handler in its own VS project, or possibly even in its own solution, thus guiding developers away from introducing dependencies in the name of reuse (it&#8217;s a <a href="http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/">fallacy</a>). If you do decide <b>as a deployment concern</b>, that you want to put them all in the same process feeding off of the same queue, you can ILMerge those assemblies and host them together, but understand that you will be undoing much of the benefits of your autonomous components.</p>
<h3>Whither the domain model?</h3>
<p>Although in the diagram above you can see the domain model beside the command-processing autonomous components, it&#8217;s actually an implementation detail. There is nothing that states that all commands <i>must</i> be processed by the same domain model. Arguably, you could have some commands be processed by <a href="http://martinfowler.com/eaaCatalog/transactionScript.html">transaction script</a>, others using <a href="http://martinfowler.com/eaaCatalog/tableModule.html">table module</a> (AKA active record), as well as those using the <a href="http://martinfowler.com/eaaCatalog/domainModel.html">domain model</a>. Event-sourcing is another possible implementation.</p>
<p>Another thing to understand about the domain model is that it now isn&#8217;t used to serve queries. So the question is, why do you need to have so many relationships between entities in your domain model?</p>
<p>(You may want to take a second to let that sink in.)</p>
<p>Do we really need a collection of orders on the customer entity? In what command would we need to navigate that collection? In fact, what kind of command would need <i>any</i> one-to-many relationship? And if that&#8217;s the case for one-to-many, many-to-many would definitely be out as well. I mean, most commands only contain one or two IDs in them anyway.</p>
<p>Any aggregate operations that may have been calculated by looping over child entities could be pre-calculated and stored as properties on the parent entity. Following this process across all the entities in our domain would result in isolated entities needing nothing more than a couple of properties for the IDs of their related entities &#8211; &#8220;children&#8221; holding the parent ID, like in databases.</p>
<p>In this form, commands could be entirely processed by a single entity &#8211; viola, an aggregate root that is a consistency boundary.</p>
<h3>Persistence for command processing</h3>
<p>Given that the database used for command processing is not used for querying, and that most (if not all) commands contain the IDs of the rows they&#8217;re going to affect, do we really need to have a column for every single domain object property? What if we just serialized the domain entity and put it into a single column, and had another column containing the ID? This sounds quite similar to key-value storage that is available in the various cloud providers. In which case, would you really need an object-relational mapper to persist to this kind of storage? </p>
<p>You could also pull out an additional property per piece of data where you&#8217;d want the &#8220;database&#8221; to enforce uniqueness. </p>
<p>I&#8217;m not suggesting that you do this in all cases &#8211; rather just trying to get you to rethink some basic assumptions.</p>
<h3>Let me reiterate</h3>
<p>How you process the commands is an implementation detail of CQRS.</p>
<h3>Keeping the query store in sync</h3>
<p>After the command-processing autonomous component has decided to accept a command, modifying its persistent store as needed, it publishes an event notifying the world about it. This event often is the &#8220;past tense&#8221; of the command submitted:</p>
<p>MakeCustomerPerferredCommand -> CustomerHasBeenMadePerferredEvent</p>
<p>The publishing of the event is done transactionally together with the processing of the command and the changes to its database. That way, any kind of failure on commit will result in the event not being sent. This is something that should be handled by default by your message bus, and if you&#8217;re using MSMQ as your underlying transport, requires the use of transactional queues.</p>
<p>The autonomous component which processes those events and updates the query data store is fairly simple, translating from the event structure to the persistent view model structure. I suggest having an event handler per view model class (AKA per table). </p>
<p>Here&#8217;s the picture of all the pieces again:</p>
<p><img src="/wp-content/uploads/cqrs.png" width="500" height="319" alt="CQRS" title="CQRS" /></p>
<h3>Bounded Contexts</h3>
<p>While CQRS touches on many pieces of software architecture, it is still not at the top of the food chain. CQRS if used is employed within a bounded context (DDD) or a business component (SOA) &#8211; a cohesive piece of the problem domain. The events published by one BC are subscribed to by other BCs, each updating their query and command data stores as needed.</p>
<p>UI&#8217;s from the CQRS found in each BC can be &#8220;mashed up&#8221; in a single application, providing users a single composite view on all parts of the problem domain. Composite UI frameworks are very useful for these cases.</p>
<h3>Summary</h3>
<p>CQRS is about coming up with an appropriate architecture for multi-user collaborative applications. It explicitly takes into account factors like data staleness and volatility and exploits those characteristics for creating simpler and more scalable constructs.</p>
<p>One cannot truly enjoy the benefits of CQRS without considering the user-interface, making it capture user intent explicitly. When taking into account client-side validation, command structures may be somewhat adjusted. Thinking through the order in which commands and events are processed can lead to notification patterns which make returning errors unnecessary.</p>
<p>While the result of applying CQRS to a given project is a more maintainable and performant code base, this simplicity and scalability require understanding the detailed business requirements and are not the result of any technical &#8220;best practice&#8221;. If anything, we can see a plethora of approaches to apparently similar problems being used together &#8211; data readers and domain models, one-way messaging and synchronous calls.</p>
<p>Although this blog post is over 3000 words (a record for this blog), I know that it doesn&#8217;t go into enough depth on the topic (it takes about 3 days out of the 5 of my <a href="http://www.udidahan.com/training/">Advanced Distributed Systems Design course</a> to cover everything in enough depth). Still, I hope it has given you the understanding of why CQRS is the way it is and possibly opened your eyes to other ways of looking at the design of distributed systems.</p>
<p>Questions and comments are most welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/12/09/clarified-cqrs/feed/</wfw:commentRss>
		<slash:comments>69</slash:comments>
		</item>
		<item>
		<title>Progressive .NET Wrap-up</title>
		<link>http://www.udidahan.com/2009/09/07/progressive-net-wrap-up/</link>
		<comments>http://www.udidahan.com/2009/09/07/progressive-net-wrap-up/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 06:06:30 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Pub/Sub]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1103</guid>
		<description><![CDATA[So, I&#8217;ve gotten back from a most enjoyable couple of days in Sweden where I gave two half-day tutorials, the first being the SOA and UI composition talk I gave at the European Virtual ALT.NET meeting (which you can find online here) and the other on DDD in enterprise apps (the first time I&#8217;ve done [...]]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;ve gotten back from a most enjoyable couple of days in Sweden where I gave two half-day tutorials, the first being the SOA and UI composition talk I gave at the European Virtual ALT.NET meeting (which you can find online <a href="http://www.vimeo.com/5022174">here</a>) and the other on DDD in enterprise apps (the first time I&#8217;ve done this talk).</p>
<p>I&#8217;ve gotten some questions about my DDD presentation there based on <a href="http://codebetter.com/blogs/aaron.jensen/">Aaron Jensen&#8217;s</a> pictures:</p>
<p><img src="http://www.udidahan.com/wp-content/uploads/cqs_udi_dahan_presentation.jpg" alt="cqs_udi_dahan_presentation" title="cqs_udi_dahan_presentation" width="500" height="332" class="alignnone size-full wp-image-1104" /></p>
<p>Yes &#8211; I talk with my hands. All the time.</p>
<p>That slide is quite an important one &#8211; I talked about it for at least 2 hours.</p>
<p>Here it is again, this time in full:</p>
<p><img src="http://www.udidahan.com/wp-content/uploads/cqs.jpg" alt="cqs" title="cqs" width="500" height="374" class="alignnone size-full wp-image-1107" /></p>
<p>You may notice that the nice clean layered abstraction that the industry has gotten so comfortable with doesn&#8217;t quite sit right when looking at it from this perspective. The reason for that is that this perspective takes into account physical distribution while layers don&#8217;t.</p>
<p>I&#8217;ll have some more posts on this topic as well as giving a session in TechEd Europe this November.</p>
<p>Oh &#8211; and please do feel free to already send your questions in.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/09/07/progressive-net-wrap-up/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Delete &#8211; Just Don&#8217;t</title>
		<link>http://www.udidahan.com/2009/09/01/dont-delete-just-dont/</link>
		<comments>http://www.udidahan.com/2009/09/01/dont-delete-just-dont/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 12:04:48 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1097</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/no_delete.png" style="float:right; margin-left:10px; margin-bottom:10px; alt="no deletes" title="no deletes" /><br />
After reading Ayende&#8217;s <a href="http://ayende.com/Blog/archive/2009/08/30/avoid-soft-deletes.aspx">post</a> advocating against &#8220;soft deletes&#8221; I felt that I should add a bit more to the topic as there were some important business semantics missing. As developers discuss the pertinence of using an IsDeleted column in the database to mark deletion, and the way this relates to reporting and auditing concerns is weighed, the core domain concepts rarely get a mention. Let&#8217;s first understand the business scenarios we&#8217;re modeling, the why behind them, before delving into the how of implementation.</p>
<h3>The real world doesn&#8217;t cascade</h3>
<p>Let&#8217;s say our marketing department decides to delete an item from the catalog. Should all previous orders containing that item just disappear? And cascading farther, should all invoices for those orders be deleted as well? Going on, would we have to redo the company&#8217;s profit and loss statements?</p>
<p>Heaven forbid.</p>
<p>So, is Ayende wrong? Do we really need soft deletes after all?</p>
<p>On the one hand, we don&#8217;t want to leave our database in an inconsistent state with invoices pointing to non-existent orders, but on the other hand, our users did ask us to delete an entity.</p>
<p>Or did they?</p>
<h3>When all you have is a hammer&#8230;</h3>
<p>We&#8217;ve been exposing users to entity-based interfaces with &#8220;create, read, update, delete&#8221; semantics in them for so long that they have started presenting us requirements using that same language, even though it&#8217;s an extremely poor fit.</p>
<p>Instead of accepting &#8220;delete&#8221; as a normal user action, let&#8217;s go into why users &#8220;delete&#8221; stuff, and what they actually intend to do.</p>
<p>The guys in marketing can&#8217;t actually make all physical instances of a product disappear &#8211; nor would they want to. In talking with these users, we might discover that their intent is quite different:</p>
<blockquote><p>“What I mean by &#8216;delete&#8217; is that the product should be discontinued. We don&#8217;t want to sell this line of product anymore. We want to get rid of the inventory we have, but not order any more from our supplier. The product shouldn&#8217;t appear any more when customers do a product search or category listing, but the guys in the warehouse will still need to manage these items in the interim. It&#8217;s much shorter to just say &#8216;delete&#8217; though.”</p></blockquote>
<p>There seem to be quite a few interesting business rules and processes there, but nothing that looks like it could be solved by a single database column.</p>
<h3>Model the task, not the data</h3>
<p>Looking back at the story our friend from marketing told us, his intent is to discontinue the product &#8211; not to delete it in any technical sense of the word. As such, we probably should provide a more explicit representation of this task in the user interface than just selecting a row in some grid and clicking the &#8216;delete&#8217; button (and &#8220;Are you sure?&#8221; isn&#8217;t it).</p>
<p>As we broaden our perspective to more parts of the system, we see this same pattern repeating:</p>
<blockquote><p>
Orders aren&#8217;t deleted &#8211; they&#8217;re cancelled. There may also be fees incurred if the order is canceled too late.</p>
<p>Employees aren&#8217;t deleted &#8211; they&#8217;re fired (or possibly retired). A compensation package often needs to be handled.</p>
<p>Jobs aren&#8217;t deleted &#8211; they&#8217;re filled (or their requisition is revoked).
</p></blockquote>
<p>In all cases, the thing we should focus on is the task the user wishes to perform, rather than on the technical action to be performed on one entity or another. In almost all cases, more than one entity needs to be considered.</p>
<h3>Statuses</h3>
<p>In all the examples above, what we see is a replacement of the technical action &#8216;delete&#8217; with a relevant business action. At the entity level, instead of having a (hidden) technical WasDeleted status, we see an explicit business status that users need to be aware of.</p>
<p>The manager of the warehouse needs to know that a product is discontinued so that they don&#8217;t order any more stock from the supplier. In today&#8217;s world of retail with Vendor Managed Inventory, this often happens together with a modification to an agreement with the vendor, or possibly a cancellation of that agreement. </p>
<p>This isn&#8217;t just a case of transactional or reporting boundaries &#8211; users in different contexts need to see different things at different times as the status changes to reflect the entity&#8217;s place in the business lifecycle. Customers shouldn&#8217;t see discontinued products at all. Warehouse workers should, that is, until the corresponding Stock Keeping Unit (SKU) has been revoked (another status) after we&#8217;ve sold all the inventory we wanted (and maybe returned the rest back to the supplier).</p>
<h3>Rules and Validation</h3>
<p>When looking at the world through over-simplified-delete-glasses, we may consider the logic dictating when we can delete to be quite simple: do some role-based-security checks, check that the entity exists, delete. Piece of cake.</p>
<p>The real world is a bigger, more complicated cake.</p>
<p>Let&#8217;s consider deleting an order, or rather, canceling it. On top of the regular security checks, we&#8217;ve got some rules to consider:</p>
<blockquote><p>
If the order has already been delivered, check if the customer isn&#8217;t happy with what they got, and go about <b>returning</b> the order. </p>
<p>If the order contained products &#8220;made to order&#8221;, charge the customer for a portion (or all) of the order (based on other rules).</p>
<p>And more&#8230;
</p></blockquote>
<p>Deciding what the next status should be may very well depend on the current business status of the entity. Deciding if that change of state is allowed is context and time specific &#8211; at one point in time the task may have been allowed, but later not. The logic here is not necessarily entirely related to the entity being &#8220;deleted&#8221; &#8211; there may be other entities which need to be checked, and whose status may also need  to be changed as well.</p>
<h3>Summary</h3>
<p>I know that some of you are thinking, &#8220;my system isn&#8217;t that complex &#8211; we can just delete and be done with it&#8221;.</p>
<p>My question to you would be, have you asked your users <b>why</b> they&#8217;re deleting things? Have you asked them about additional statuses and rules dictating how entities move as groups between them? You don&#8217;t want the success of your project to be undermined by that kind of unfounded assumption, do you?</p>
<p>The reason we&#8217;re given budgets to build business applications is because of the richness in business rules and statuses that ultimately provide value to users and a competitive advantage to the business. If that value wasn&#8217;t there, wouldn&#8217;t we be serving our users better by just giving them Microsoft Access?</p>
<p>In closing, given that you&#8217;re not giving your users MS Access, don&#8217;t think about deleting entities. Look for the reason why. Understand the different statuses that entities move between. Ask which users need to care about which status. I know it doesn&#8217;t show up as nicely on your resume as &#8220;3 years WXF&#8221;, but &#8220;saved the company $4 million in wasted inventory&#8221; does speak volumes.</p>
<p>One last sentence: Don&#8217;t delete. Just don&#8217;t.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/09/01/dont-delete-just-dont/feed/</wfw:commentRss>
		<slash:comments>56</slash:comments>
		</item>
		<item>
		<title>Object Relational Mapping Sucks!</title>
		<link>http://www.udidahan.com/2008/06/25/object-relational-mapping-sucks/</link>
		<comments>http://www.udidahan.com/2008/06/25/object-relational-mapping-sucks/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 11:32:06 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2008/06/25/object-relational-mapping-sucks/</guid>
		<description><![CDATA[For reporting, that is. 
And doesn&#8217;t handle concurrency!
Unless you don&#8217;t expose setters.
I guess it depends, doesn&#8217;t it? 
Well, that was Ted&#8217;s assertion in his recent Pragmatic Architecture column on data access.
But, &#8220;it depends&#8221; doesn&#8217;t get the system built, does it?
So, here are some rules for using o/r mapping that will get you 99% of the [...]]]></description>
			<content:encoded><![CDATA[<p>For reporting, that is.<a href="http://udidahan.weblogs.us/wp-content/uploads/image26.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 10px 10px; border-right-width: 0px" height="160" alt="image" src="http://udidahan.weblogs.us/wp-content/uploads/image-thumb22.png" width="244" align="right" border="0"></a> </p>
<p>And doesn&#8217;t handle concurrency!</p>
<p>Unless you don&#8217;t expose setters.</p>
<p>I guess <em>it depends</em>, doesn&#8217;t it? </p>
<p>Well, that was Ted&#8217;s assertion in his <a href="http://msdn2.microsoft.com/en-us/library/cc178936.aspx">recent Pragmatic Architecture column on data access</a>.</p>
<p>But, &#8220;it depends&#8221; doesn&#8217;t get the system built, does it?</p>
<p>So, here are some rules for using o/r mapping that will get you 99% of the way there. </p>
<p>Yes, you heard me. </p>
<p><strong>Rules</strong>. </p>
<p>They do not depend. </p>
<p>If you&#8217;re doing something significantly bigger than enterprise-scale development, and you are already doing this, and it isn&#8217;t enough, <a href="mailto:consulting@UdiDahan.com">give me a call</a>. Here we go.</p>
<ol>
<li>No reporting.<br />
<blockquote>
<p>I mean it. Don&#8217;t report off of live data. <br />This isn&#8217;t just a o/r mapping thing. <br />Users can tolerate some, if not quite a lot of latency.</p>
<p>And it&#8217;s not like <em>objects</em> are even used. It&#8217;s just rolled up data. Not a single behaviour for miles.</p>
</blockquote>
<li>Don&#8217;t expose setters<br />
<blockquote>
<p>You want multiple users sharing and collaborating on data, right? Then don&#8217;t force them to either overwrite each others data, or throw away their own. There is one simple way to avoid that: Get an object, call a method. Once the object has the most up to date data, pass all the client data in via a method call. The object will decide if its valid, from a business perspective as well, and then update the appropriate fields. </p>
<p>Now your DBAs can vertically partition tables accordingly, and improve throughput. After that, you can increase the isolation level, to improve safety, without hurting throughput. </p>
<p>This will also keep your logic encapsulated, bringing you closer to a true Domain Model.</p>
<p>If your O/R mapping tool requires you to have setters on your domain classes, hide those from your service layer behind an interface. </p>
</blockquote>
<li>Grids are like reports.<br />
<blockquote>
<p>No o/r mapping required there either. While you probably won&#8217;t be showing grids of yesterday&#8217;s data to users in an interactive environment, it&#8217;s still just data &#8211; no behaviour.</p>
<p>However, users should NOT update data in those grids. This gets back to rule 2. Have users select a specific task they want to perform, pop open a window, and have them do it there. Change customer address. Discount order. You get the picture. That way you&#8217;ll know what method to call on those objects you designed in rule 2.</p>
</blockquote>
</li>
</ol>
<p>Before wrapping up, one small thing.</p>
<p>You <em>can</em> use an O/R mapping tool to do reporting, just, for the love of Bill, don&#8217;t use the same classes you designed for your OLTP domain model. But, just because you can, doesn&#8217;t necessarily mean you should. <strike>Datasets</strike> datatables are probably just as viable a solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/06/25/object-relational-mapping-sucks/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>7 Simple Questions for Service Selection</title>
		<link>http://www.udidahan.com/2008/05/16/7-simple-questions-for-service-selection/</link>
		<comments>http://www.udidahan.com/2008/05/16/7-simple-questions-for-service-selection/#comments</comments>
		<pubDate>Fri, 16 May 2008 22:21:09 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2008/05/16/7-simple-questions-for-service-selection/</guid>
		<description><![CDATA[&#8220;So, which services do I need?&#8221;
This innocuous question comes up a lot. Usually I get this question after a short problem domain description. One of these came up on the nServiceBus discussion groups. Ayende took it and ran with it turning it into a nice blog post, An exercise in designing SOA systems. I&#8217;ve been [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;So, which services do I need?&#8221;</p>
<p>This innocuous question comes up a lot. Usually I get this question after a short problem domain description. One of these came up on the nServiceBus discussion groups. Ayende took it and ran with it turning it into a nice blog post, <a href="http://ayende.com/Blog/archive/2008/04/08/An-exercise-in-designing-SOA-systems.aspx">An exercise in designing SOA systems</a>. I&#8217;ve been meaning to write something myself. Bill put up a response already in his <a href="http://bill-poole.blogspot.com/2008/05/service-granularity-example.html">Service Granularity Example</a>. So, I&#8217;m late to the party, again, but here we go.</p>
<p>It&#8217;s almost impossible to know, right away, which services are appropriate.</p>
<p>So, I&#8217;m going to focus more on the process of getting there, rather than describing the solution itself.</p>
<p>The domain deals with a placement agency placing physicians in positions at hospitals. <a href="http://udidahan.weblogs.us/wp-content/uploads/doctor.png"><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="244" alt="doctor" src="http://udidahan.weblogs.us/wp-content/uploads/doctor-thumb.png" width="225" align="right" border="0"></a> </p>
<h3>1. So, what does it actually <em>do</em>?</h3>
<p>In Ayende&#8217;s post, he describes several services, but I&#8217;d rather look at them as use cases: registering an open position, registering a candidate, verifying their credentials, etc. It&#8217;s worth going through this <em>requirements</em> process. It doesn&#8217;t necessarily translate immediately to services, but there&#8217;s value in it.</p>
<h3>2. What does it do it <em>to</em>?</h3>
<p>We should also be looking at the data model, an entity relationship diagram (ERD) , where we see that we may have placed a certain physician at a number of positions. It&#8217;s also important for us to know about under which circumstances a physician finished their employment at a previous position before, say, trying to place them at a position in the same hospital or chain of hospitals. Don&#8217;t go thinking that this what the database schema will look like, it&#8217;s all about understanding connections between various bits of data.</p>
<h3>3. When does that happen?</h3>
<p>The next step is to map the uses cases above to the entities in the ERD, which entity is used in which use case. It&#8217;s also important to differentiate between entities (or even more importantly, specific fields of entities) that are used in a read-only fashion within a given use case. For instance, when registering a new position, we&#8217;ll want to check that against other open positions in the same hospital so we don&#8217;t end up registering the same position twice. Also, we might want to suggest verified physicians whose credentials match the position&#8217;s requirements. Data we wouldn&#8217;t be interested in might be which other physicians we placed at that hospital.</p>
<h3>4. What just happened?</h3>
<p>Another valuable perspective on the problem domain is the business process view &#8211; what are the interesting business events in the system and how they unfold over time. For instance, physician registered, position opened, physician&#8217;s credentials verified, and physician placed in position (or position filled by physician) are events that describe a different business perspective than use cases.<a href="http://udidahan.weblogs.us/wp-content/uploads/image20.png"><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 0px 10px; border-left: 0px; border-bottom: 0px" height="241" alt="image" src="http://udidahan.weblogs.us/wp-content/uploads/image-thumb17.png" width="244" align="right" border="0"></a></p>
<h3>5. How do I decide? </h3>
<p>Once we know what events there are, we can start looking at what kind of decisions we might want to make when those events occur and what data we&#8217;d need to make those decisions. These decisions may be as simple as updating a database or sending an email to a user. They also may include more advanced logic like when the profitability of an agreement with a specific hospital chain changes, prefer placing physicians in positions in that chain over others.</p>
<h3>6. How do I deal with all this information?</h3>
<p>After we have all of this information, we can start looking for cohesive bunching across all of these axes using these rules:</p>
<ul>
<li>Data that is modified by a use case gets published as an event.</li>
<li>Data that is required by a use case for read-only purposes, arrives as the result of subscribing to some event.</li>
</ul>
<p>Look for rules that differentiate behaviour based on the properties of data. Look for a correlation to some business concept. For instance, physicians probably won&#8217;t be changing their specialization, and open positions often deal with a certain specialization. Therefore, specific data instances tied to two different specializations can be said to be loosely coupled.</p>
<h3><strong>7. Which property slices across the domain?<a href="http://udidahan.weblogs.us/wp-content/uploads/image21.png"><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="161" alt="image" src="http://udidahan.weblogs.us/wp-content/uploads/image-thumb18.png" width="244" align="right" border="0"></a> </strong></h3>
<p>Even though the ERD may not have made it clear, and the use cases didn&#8217;t show any particular break-down, nor did the events call out this point, the key to finding the way a business domain decomposes into services lies in decoupling specific data instances.</p>
<p>Actually, at this point we can clump autonomous components (mere technical bits) that handle a single message, into more granular business components.</p>
<blockquote><p>If you think about it, it makes a lot of sense. The kind of credential checking you&#8217;d do for physicians specializing in brain surgery would likely be different than for general practitioners. The kind of information you&#8217;d store would, therefore, also be different.</p>
</blockquote>
<h3>But, which services do I need?</h3>
<p>Quite frankly, I don&#8217;t have enough information to know. </p>
<p>But if we had continued this conversation, going through issues like transactional consistency, availability requirements, and other non-functional issues we could have&nbsp; gotten there. </p>
<p>If there&#8217;s one thing that I hope you got out of this, it&#8217;s that the questions are what&#8217;s important. The iterative process of looking at the problem domain from various perspectives, incorporating the new-found knowledge, and asking more questions is what leads us to a solution. But we don&#8217;t stop there. We keep looking for characteristics which split services apart into business components, and for consistency requirements that brings autonomous components together into services.</p>
<p>It&#8217;s not easy, but by focusing on these simple questions, you can get to a coherent service oriented architecture.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/05/16/7-simple-questions-for-service-selection/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to create fully encapsulated Domain Models</title>
		<link>http://www.udidahan.com/2008/02/29/how-to-create-fully-encapsulated-domain-models/</link>
		<comments>http://www.udidahan.com/2008/02/29/how-to-create-fully-encapsulated-domain-models/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 14:40:33 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Simplicity]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2008/02/29/how-to-create-fully-encapsulated-domain-models/</guid>
		<description><![CDATA[ Update: The new and improved solution is now available: Domain Events, Take 2.
Most people getting started with DDD and the Domain Model pattern get stuck on this. For a while I tried answering this on the discussion groups, but here we have a nice example that I can point to next time.
The underlying problem [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://udidahan.weblogs.us/wp-content/uploads/image8.png"><img style="border-right: 0px; border-top: 0px; margin: 0px 10px; border-left: 0px; border-bottom: 0px" height="192" alt="image" src="http://udidahan.weblogs.us/wp-content/uploads/image-thumb6.png" width="150" align="right" border="0"></a> <b>Update:</b> The new and <i>improved</i> solution is now available: <a href="http://www.udidahan.com/2008/08/25/domain-events-take-2/">Domain Events, Take 2</a>.</p>
<p>Most people getting started with DDD and the Domain Model pattern get stuck on this. For a while I tried answering this on the discussion groups, but here we have a nice example that I can point to next time.</p>
<p>The underlying problem I&#8217;ve noticed over the past few years is that developers are still thinking in terms of querying when they need more data. When moving to the Domain Model pattern, you have to &#8220;simply&#8221; represent the domain concepts in code &#8211; in other words, see things you aren&#8217;t used to seeing. I&#8217;ll highlight that part in the question below so that you can see where I&#8217;m going to go with this in my answer:</p>
<blockquote><p>I have an instance where I believe I need access to a service or repository from my entity to evaluate a business rule but I&#8217;m using NHibernate for persistence so I don&#8217;t have a real good way to inject services into my entity. Can I get some viewpoints on just passing the services to my entity vs. using a facade?</p>
<p>Let me explain my problem to provide more context to the problem.</p>
<p>The core domain revolves around renting video games. I am working on a new feature to allow customers to trade in old video games. <font color="#ff0000">Customers</font> can trade in multiple games at a time so we have a TradeInCart entity that works similar to most shopping carts that everybody is familiar with. However there are several rules that limit the items that can be placed into the TradeInCart. The core rules are:</p>
<p>1. Only 3 games of the same title can be added to the cart.<br />2. The total number of items in the cart cannot exceed 10.<br />3. No games can be added to the cart that the <font color="#ff0000">customer had previously reported lost</font> with regards to their rental membership.<br />&nbsp;&nbsp;&nbsp; a. If an attempt is made to add a previously reported lost game, then we need to log a BadQueueStatusAddAttempt to the persistence store.</p>
<p>So the first 2 rules are easily handled internally by the cart through an Add operation. Sample cart interface is below.</p>
<p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">class</span> TradeInCart{</pre>
<pre><span class="lnum">   2:  </span>    Account Account{get;}</pre>
<pre class="alt"><span class="lnum">   3:  </span>    LineItem Add(Game game);</pre>
<pre><span class="lnum">   4:  </span>    ValidationResult CanAdd(Game game);</pre>
<pre class="alt"><span class="lnum">   5:  </span>    IList&lt;LineItems&gt; LineItems{get;}</pre>
<pre><span class="lnum">   6:  </span>}</pre>
</div>
<p>However the #3 rule is much more complicated and can&#8217;t be handled internally by the cart, so I have to depend on external services. Splitting up the validation logic for a cart add operation doesn&#8217;t seem very appealing to me at all. So I have the option of passing in a repository to get the previously reported lost games and a service to log bad attempts. This makes my cart interface ugly real quick.</p>
<p><div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">class</span> TradeInCart{</pre>
<pre><span class="lnum">   2:  </span>    Account Account{get;}</pre>
<pre class="alt"><span class="lnum">   3:  </span>    LineItem Add(</pre>
<pre><span class="lnum">   4:  </span>        Game game, </pre>
<pre class="alt"><span class="lnum">   5:  </span>        IRepository&lt;QueueHistory&gt; repository, </pre>
<pre><span class="lnum">   6:  </span>        LoggingService service);</pre>
<pre class="alt"><span class="lnum">   7:  </span>&nbsp;</pre>
<pre><span class="lnum">   8:  </span>    ValidationResult CanAdd(</pre>
<pre class="alt"><span class="lnum">   9:  </span>        Game game, </pre>
<pre><span class="lnum">  10:  </span>        IRepository&lt;QueueHistory&gt; repository, </pre>
<pre class="alt"><span class="lnum">  11:  </span>        LoggingService service);</pre>
<pre><span class="lnum">  12:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  13:  </span>    IList&lt;LineItems&gt; LineItems{get;}</pre>
<pre><span class="lnum">  14:  </span>}</pre>
</div>
<p>The alternative option is to have a TradeInCartFacade that handles the validations and adding the items to the cart. The façade can have the repository and services injected though DI which is nice, but the big negative is that the cart ends up totally anemic.</p>
<p>Any thought on this would be greatly appreciated.</p>
<p>Thanks,<br />Jesse</p>
</blockquote>
<p>As I highlighted above, the thing that will help you with your business rules is to introduce the Customer object (that you probably already have) with the property GamesReportedLost (an IList&lt;Game&gt;). Your TradeInCart would have a reference to the Customer object and could then check the rule in the Add method.</p>
<p>Before I go into the code, it looks like your Account object might be used the same way, but your description of the domain doesn&#8217;t mention accounts, so I&#8217;m going to assume that that&#8217;s unrelated for now:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">public class</span> Customer{</pre>
<pre><span class="lnum">   2:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="rem">/* other properties and methods */</span></pre>
<pre><span class="lnum">   4:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   5:  </span>    <span class="kwrd">private</span> IList&lt;Game&gt; gamesReportedLost;</pre>
<pre><span class="lnum">   6:  </span>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;Game&gt; GamesReportedLost </pre>
<pre class="alt"><span class="lnum">   7:  </span>    { </pre>
<pre><span class="lnum">   8:  </span>        get</pre>
<pre class="alt"><span class="lnum">   9:  </span>        {</pre>
<pre><span class="lnum">  10:  </span>            <span class="kwrd">return</span> gamesReportedLost;</pre>
<pre class="alt"><span class="lnum">  11:  </span>        }</pre>
<pre><span class="lnum">  12:  </span>        set</pre>
<pre class="alt"><span class="lnum">  13:  </span>        {</pre>
<pre><span class="lnum">  14:  </span>            gamesReportedLost = <span class="kwrd">value</span>;</pre>
<pre class="alt"><span class="lnum">  15:  </span>        }</pre>
<pre><span class="lnum">  16:  </span>    }</pre>
<pre class="alt"><span class="lnum">  17:  </span>}</pre>
</div>
<p>Keep in mind that the GamesReportedLost is a persistent property of Customer. Every time a customer reports a game lost, this list needs to be kept up to date. Here&#8217;s the TradeInCart now:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">class</span> TradeInCart</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="rem">/* other properties and methods */</span></pre>
<pre><span class="lnum">   4:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   5:  </span>    <span class="kwrd">private</span> Customer customer;</pre>
<pre><span class="lnum">   6:  </span>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> Customer Customer</pre>
<pre class="alt"><span class="lnum">   7:  </span>    { </pre>
<pre><span class="lnum">   8:  </span>        get { <span class="kwrd">return</span> customer; }</pre>
<pre class="alt"><span class="lnum">   9:  </span>        set { customer = <span class="kwrd">value</span>; }</pre>
<pre><span class="lnum">  10:  </span>    }</pre>
<pre class="alt"><span class="lnum">  11:  </span>&nbsp;</pre>
<pre><span class="lnum">  12:  </span>    <span class="kwrd">private</span> IList&lt;LineItem&gt; lineItems;</pre>
<pre class="alt"><span class="lnum">  13:  </span>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;LineItem&gt; LineItems</pre>
<pre><span class="lnum">  14:  </span>    {</pre>
<pre class="alt"><span class="lnum">  15:  </span>        get { <span class="kwrd">return</span> lineItems; }</pre>
<pre><span class="lnum">  16:  </span>        set { lineItems = <span class="kwrd">value</span>; }</pre>
<pre class="alt"><span class="lnum">  17:  </span>    }</pre>
<pre><span class="lnum">  18:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  19:  </span>    <span class="kwrd">public</span> <span class="kwrd">void</span> Add(Game game)</pre>
<pre><span class="lnum">  20:  </span>    {</pre>
<pre class="alt"><span class="lnum">  21:  </span>        <span class="kwrd">if</span> (lineItems.Count &gt;= CONSTANTS.MaxItemsPerCart)</pre>
<pre><span class="lnum">  22:  </span>        {</pre>
<pre class="alt"><span class="lnum">  23:  </span>            FailureEvents.RaiseCartIsFullEvent();</pre>
<pre><span class="lnum">  24:  </span>            <span class="kwrd">return</span>;</pre>
<pre class="alt"><span class="lnum">  25:  </span>        }</pre>
<pre><span class="lnum">  26:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  27:  </span>        <span class="kwrd">if</span> (NumberOfGameAlreadyInCart(game) &gt;=</pre>
<pre><span class="lnum">  28:  </span>            CONSTANTS.MaxNumberOfSameGamePerCart)</pre>
<pre class="alt"><span class="lnum">  29:  </span>        {</pre>
<pre><span class="lnum">  30:  </span>            FailureEvents</pre>
<pre class="alt"><span class="lnum">  31:  </span>              .RaiseMaxNumberOfSameGamePerCartReachedEvent();</pre>
<pre><span class="lnum">  32:  </span>            <span class="kwrd">return</span>;</pre>
<pre class="alt"><span class="lnum">  33:  </span>        }</pre>
<pre><span class="lnum">  34:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  35:  </span>        <span class="kwrd">if</span> (customer.GamesReportedLost.Contains(game))</pre>
<pre><span class="lnum">  36:  </span>            FailureEvents.RaiseGameReportedLostEvent();</pre>
<pre class="alt"><span class="lnum">  37:  </span>        <span class="kwrd">else</span></pre>
<pre><span class="lnum">  38:  </span>            <span class="kwrd">this</span>.lineItems.Add(<span class="kwrd">new</span> LineItem(game));</pre>
<pre class="alt"><span class="lnum">  39:  </span>    }</pre>
<pre><span class="lnum">  40:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  41:  </span>    <span class="kwrd">private</span> <span class="kwrd">int</span> NumberOfGameAlreadyInCart(Game game)</pre>
<pre><span class="lnum">  42:  </span>    {</pre>
<pre class="alt"><span class="lnum">  43:  </span>        <span class="kwrd">int</span> result = 0;</pre>
<pre><span class="lnum">  44:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  45:  </span>        <span class="kwrd">foreach</span>(LineItem li <span class="kwrd">in</span> <span class="kwrd">this</span>.lineItems)</pre>
<pre><span class="lnum">  46:  </span>            <span class="kwrd">if</span> (li.Game == game)</pre>
<pre class="alt"><span class="lnum">  47:  </span>                result++;</pre>
<pre><span class="lnum">  48:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  49:  </span>        <span class="kwrd">return</span> result;</pre>
<pre><span class="lnum">  50:  </span>    }</pre>
<pre class="alt"><span class="lnum">  51:  </span>}</pre>
<pre><span class="lnum">  52:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  53:  </span><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> FailureEvents</pre>
<pre><span class="lnum">  54:  </span>{</pre>
<pre class="alt"><span class="lnum">  55:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">event</span> EventHandler GameReportedLost;</pre>
<pre><span class="lnum">  56:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> RaiseGameReportedLostEvent()</pre>
<pre class="alt"><span class="lnum">  57:  </span>    {</pre>
<pre><span class="lnum">  58:  </span>         <span class="kwrd">if</span> (GameReportedLost != <span class="kwrd">null</span>)</pre>
<pre class="alt"><span class="lnum">  59:  </span>             GameReportedLost(<span class="kwrd">null</span>, <span class="kwrd">null</span>);</pre>
<pre><span class="lnum">  60:  </span>    }</pre>
<pre class="alt"><span class="lnum">  61:  </span>&nbsp;</pre>
<pre><span class="lnum">  62:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">event</span> EventHandler CartIsFull;</pre>
<pre class="alt"><span class="lnum">  63:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> RaiseCartIsFullEvent()</pre>
<pre><span class="lnum">  64:  </span>    {</pre>
<pre class="alt"><span class="lnum">  65:  </span>         <span class="kwrd">if</span> (CartIsFull != <span class="kwrd">null</span>)</pre>
<pre><span class="lnum">  66:  </span>             CartIsFull(<span class="kwrd">null</span>, <span class="kwrd">null</span>);</pre>
<pre class="alt"><span class="lnum">  67:  </span>    }</pre>
<pre><span class="lnum">  68:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  69:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">event</span> EventHandler MaxNumberOfSameGamePerCartReached;</pre>
<pre><span class="lnum">  70:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> RaiseMaxNumberOfSameGamePerCartReachedEvent()</pre>
<pre class="alt"><span class="lnum">  71:  </span>    {</pre>
<pre><span class="lnum">  72:  </span>         <span class="kwrd">if</span> (MaxNumberOfSameGamePerCartReached != <span class="kwrd">null</span>)</pre>
<pre class="alt"><span class="lnum">  73:  </span>             MaxNumberOfSameGamePerCartReached(<span class="kwrd">null</span>, <span class="kwrd">null</span>);</pre>
<pre><span class="lnum">  74:  </span>    }</pre>
<pre class="alt"><span class="lnum">  75:  </span>}</pre>
</div>
<p><a href="http://udidahan.weblogs.us/wp-content/uploads/image9.png"><img style="border-right: 0px; border-top: 0px; margin: 0px 10px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://udidahan.weblogs.us/wp-content/uploads/image-thumb7.png" width="184" align="right" border="0"></a> Your service layer class that calls the Add method of TradeInCart would first subscribe to the relevant events in FailureEvents. If one of those events is raised, it would do the necessary logging, external system calls, etc.</p>
<p>As you can see, the API of TradeInCart doesn&#8217;t need to make use of any external repositories, nor do you need to inject any other external dependencies in.</p>
<p>One thing I didn&#8217;t do in the above code to keep it &#8220;short&#8221; is to define the relevant custom EventArgs for bubbling up the information as to <em>which</em> game was reported lost or already have 3 of those in the cart. That is something that definitely should be done so that the service layer can pass this information back to the client.</p>
<p>Here&#8217;s a look at Service Layer code:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">class</span> AddGameToCartMessageHandler :</pre>
<pre><span class="lnum">   2:  </span>    BaseMessageHandler&lt;AddGameToCartMessage&gt;</pre>
<pre class="alt"><span class="lnum">   3:  </span>{</pre>
<pre><span class="lnum">   4:  </span>    <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Handle(AddGameToCartMessage m)</pre>
<pre class="alt"><span class="lnum">   5:  </span>    {</pre>
<pre><span class="lnum">   6:  </span>        <span class="kwrd">using</span> (ISession session = SessionFactory.OpenSession())</pre>
<pre class="alt"><span class="lnum">   7:  </span>        <span class="kwrd">using</span> (ITransaction tx = session.BeginTransaction())</pre>
<pre><span class="lnum">   8:  </span>        {</pre>
<pre class="alt"><span class="lnum">   9:  </span>            TradeInCart cart = session.Get&lt;TradeInCart&gt;(m.CartId);</pre>
<pre><span class="lnum">  10:  </span>            Game g = session.Get&lt;Game&gt;(m.GameId);</pre>
<pre class="alt"><span class="lnum">  11:  </span>&nbsp;</pre>
<pre><span class="lnum">  12:  </span>            Domain.FailureEvents.GameReportedLost +=</pre>
<pre class="alt"><span class="lnum">  13:  </span>              gameReportedLost;</pre>
<pre><span class="lnum">  14:  </span>            Domain.FailureEvents.CartIsFull +=</pre>
<pre class="alt"><span class="lnum">  15:  </span>              cartIsFull;</pre>
<pre><span class="lnum">  16:  </span>            Domain.FailureEvents.MaxNumberOfSameGamePerCartReached +=</pre>
<pre class="alt"><span class="lnum">  17:  </span>              maxNumberOfSameGamePerCartReached;</pre>
<pre><span class="lnum">  18:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  19:  </span>            cart.Add(g);</pre>
<pre><span class="lnum">  20:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  21:  </span>            Domain.FailureEvents.GameReportedLost -=</pre>
<pre><span class="lnum">  22:  </span>              gameReportedLost;</pre>
<pre class="alt"><span class="lnum">  23:  </span>            Domain.FailureEvents.CartIsFull -=</pre>
<pre><span class="lnum">  24:  </span>              cartIsFull;</pre>
<pre class="alt"><span class="lnum">  25:  </span>            Domain.FailureEvents.MaxNumberOfSameGamePerCartReached -=</pre>
<pre><span class="lnum">  26:  </span>              maxNumberOfSameGamePerCartReached;</pre>
<pre class="alt"><span class="lnum">  27:  </span>&nbsp;</pre>
<pre><span class="lnum">  28:  </span>            tx.Commit();</pre>
<pre class="alt"><span class="lnum">  29:  </span>        }</pre>
<pre><span class="lnum">  30:  </span>    }</pre>
<pre class="alt"><span class="lnum">  31:  </span>&nbsp;</pre>
<pre><span class="lnum">  32:  </span>    <span class="kwrd">private</span> EventHandler gameReportedLost = <span class="kwrd">delegate</span> { </pre>
<pre class="alt"><span class="lnum">  33:  </span>          Bus.Return((<span class="kwrd">int</span>)ErrorCodes.GameReportedLost);</pre>
<pre><span class="lnum">  34:  </span>        };</pre>
<pre class="alt"><span class="lnum">  35:  </span>&nbsp;</pre>
<pre><span class="lnum">  36:  </span>    <span class="kwrd">private</span> EventHandler cartIsFull = <span class="kwrd">delegate</span> { </pre>
<pre class="alt"><span class="lnum">  37:  </span>          Bus.Return((<span class="kwrd">int</span>)ErrorCodes.CartIsFull);</pre>
<pre><span class="lnum">  38:  </span>        };</pre>
<pre class="alt"><span class="lnum">  39:  </span>&nbsp;</pre>
<pre><span class="lnum">  40:  </span>    <span class="kwrd">private</span> EventHandler maxNumberOfSameGamePerCartReached = <span class="kwrd">delegate</span> { </pre>
<pre class="alt"><span class="lnum">  41:  </span>          Bus.Return((<span class="kwrd">int</span>)ErrorCodes.MaxNumberOfSameGamePerCartReached);</pre>
<pre><span class="lnum">  42:  </span>        };</pre>
<pre class="alt"><span class="lnum">  43:  </span>    }</pre>
<pre><span class="lnum">  44:  </span>}</pre>
</div>
<p><img style="margin: 0px 10px 5px" src="http://www.familyportraitpainting.com/images/grandfather_sample.jpg" align="right">It&#8217;s important to remember to clean up your event subscriptions so that your Service Layer objects get garbage collected. This is one of the primary causes of memory leaks when using static events in your Domain Model. I&#8217;m hoping to find ways to use lambdas to decrease this repetitive coding pattern. You might be thinking to yourself that non-static events on your Domain Model objects would be easier, since those objects would get collected, freeing up the service layer objects for collection as well. There&#8217;s just on small problem:</p>
<p>The problem is that if an event is raised by a child (or grandchild object), the service layer object may not even know that that grandchild was involved and, as such, would not have subscribed to that event. The only way the service layer could work was by knowing how the Domain Model worked internally &#8211; in essence, breaking encapsulation.</p>
<p>If you&#8217;re thinking that using exceptions would be better, you&#8217;d be right in thinking that that won&#8217;t break encapsulation, <em>and</em> that you wouldn&#8217;t need all that subscribe/unsubscribe code in the service layer. The only problem is that the Domain Model needs to know that the service layer had a default catch clause so that it wouldn&#8217;t blow up. Otherwise, the service layer (or WCF, or nServiceBus) may end up flagging that message as a poison message (<a href="http://udidahan.weblogs.us/2007/02/15/it%e2%80%99s-poison-i-tell-you-poison/">Read more about poison messages</a>). You&#8217;d also have to be extremely careful about in which environments you used your Domain Model &#8211; in other words, your reuse is shot. </p>
<h3>Conclusion</h3>
<p>I never said it would be easy <img src='http://www.udidahan.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>However, the solution is simple (not complex). The same patterns occur over and over. The design is consistent. By focusing on the dependencies we now have a domain model that is reusable across many environments (server, client, sql clr, silverlight). The domain model is also testable without resorting to any fancy mock objects.</p>
<p>One closing comment &#8211; while I do my best to write code that is consistent with production quality environments, this code is more about demonstrating design principles. As such, I focus more on the self-documenting aspects of the code and have elided many production concerns. </p>
<p>Do you have a better solution? </p>
<p>Something that I haven&#8217;t considered?</p>
<p>Do me a favour &#8211; leave me a comment. Tell me what you think.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/02/29/how-to-create-fully-encapsulated-domain-models/feed/</wfw:commentRss>
		<slash:comments>59</slash:comments>
		</item>
		<item>
		<title>Sagas and Unit Testing &#8211; Business Process Verification Made Easy</title>
		<link>http://www.udidahan.com/2008/02/04/sagas-and-unit-testing-business-process-verification-made-easy/</link>
		<comments>http://www.udidahan.com/2008/02/04/sagas-and-unit-testing-business-process-verification-made-easy/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 13:09:51 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Workflow]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2008/02/04/sagas-and-unit-testing-business-process-verification-made-easy/</guid>
		<description><![CDATA[Sagas have always been designed with unit testing in mind. By keeping them disconnected from any communications or persistence technology, it was my belief that it should be fairly easy to use mock objects to test them. I&#8217;ve heard back from projects using nServiceBus this way that they were pleased with their ability to test [...]]]></description>
			<content:encoded><![CDATA[<p>Sagas have always been designed with unit testing in mind. By keeping them disconnected from any communications or persistence technology, it was my belief that it should be fairly easy to use mock objects to test them. I&#8217;ve heard back from projects using nServiceBus this way that they were pleased with their ability to test them, and thought all was well.</p>
<p>Not so.</p>
<p>The other day I sat down to implement and test a non-trivial business process, and the testing was far from easy. Now as developers go, I&#8217;m not great, or an expert on unit testing or TDD, but I&#8217;m above average. It should not have been this hard. And I tried doing it with <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino.Mocks</a>, <a href="http://www.typemock.com/">TypeMock</a>, and finally <a href="http://code.google.com/p/moq/">Moq</a>. It seemed like I was in a no-mans-land, between trying to do state-based testing, and setting expectations on the messages being sent (as well as correct values in those messages), nothing flowed.</p>
<p>Until I finally stopped trying to figure out how to test, and focused on what needed to be tested. I mean, it&#8217;s not like I was trying to build a generic mocking framework like <a href="http://feeds.feedburner.com/~r/DanielCazzulino/~3/228130195/NewMoqfeaturesformockverificationandcreation.aspx">Daniel</a>.</p>
<p>Here&#8217;s an example business process, or actually, part of one, and then we&#8217;ll see how that can be tested. By the way, there will be a post coming soon which describes how we go about analysing a system, coming up with these message types, and how these sagas come into being, so stay tuned. Either that, or just come to <a href="http://qcon.infoq.com/london/presentation/Build+Scalable%2C+Maintainable%2C+Distributed+Enterprise+.NET+Solutions+with+nServiceBus">my tutorial at QCon.</a></p>
<p>On with the process:</p>
<blockquote><p>1. When we receive a CreateOrderMessage, whose “Completed” flag is true, we’ll send 2 AuthorizationRequestMessages to internal systems (for managers to authorize the order), one OrderStatusUpdatedMessage to the caller with a status “Received”, and a TimeoutMessage to the TimeoutManager requesting to be notified – so that the process doesn’t get stuck if one or both messages don’t get a response.</p>
<p>2. When we receive the first AuthorizationResponseMessage, we notify the initiator of the Order by sending them a OrderStatusUpdatedMessage with a status “Authorized1”.</p>
<p>3. When we get “timed out” from the TimeoutManager, we check if at least one AuthorizationResponseMessage has arrived, and if so, publish an OrderAcceptedMessage, and notify the initator (again via the OrderStatusUpdatedMessage) this time with a status of “Accepted”.</p></blockquote>
<p>And here&#8217;s the test:</p>
<div style="overflow: scroll; width: 95%"><!-- code formatted by http://manoli.net/csharpformat/ --><br />
<style type="text/css">            .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; }</style>
<pre class="csharpcode">    <span class="kwrd">public</span> <span class="kwrd">class</span> OrderSagaTests
    {
        <span class="kwrd">private</span> OrderSaga orderSaga = <span class="kwrd">null</span>;
        <span class="kwrd">private</span> <span class="kwrd">string</span> timeoutAddress;
        <span class="kwrd">private</span> Saga Saga;     

        [SetUp]
        <span class="kwrd">public</span> <span class="kwrd">void</span> Setup()
        {
            timeoutAddress = <span class="str">"timeout"</span>;
            Saga = Saga.Test(<span class="kwrd">out</span> orderSaga, timeoutAddress);
        }     

        [Test]
        <span class="kwrd">public</span> <span class="kwrd">void</span> OrderProcessingShouldCompleteAfterOneAuthorizationAndOneTimeout()
        {
            Guid externalOrderId = Guid.NewGuid();
            Guid customerId = Guid.NewGuid();
            <span class="kwrd">string</span> clientAddress = <span class="str">"client"</span>;     

            CreateOrderMessage createOrderMsg = <span class="kwrd">new</span> CreateOrderMessage();
            createOrderMsg.OrderId = externalOrderId;
            createOrderMsg.CustomerId = customerId;
            createOrderMsg.Products = <span class="kwrd">new</span> List&lt;Guid&gt;(<span class="kwrd">new</span> Guid[] { Guid.NewGuid() });
            createOrderMsg.Amounts = <span class="kwrd">new</span> List&lt;<span class="kwrd">float</span>&gt;(<span class="kwrd">new</span> <span class="kwrd">float</span>[] { 10.0F });
            createOrderMsg.Completed = <span class="kwrd">true</span>;     

            TimeoutMessage timeoutMessage = <span class="kwrd">null</span>;     

            Saga.WhenReceivesMessageFrom(clientAddress)
                .ExpectSend&lt;AuthorizeOrderRequestMessage&gt;(
                    <span class="kwrd">delegate</span>(AuthorizeOrderRequestMessage m)
                    {
                        <span class="kwrd">return</span> m.SagaId == orderSaga.Id;
                    })
                .ExpectSend&lt;AuthorizeOrderRequestMessage&gt;(
                    <span class="kwrd">delegate</span>(AuthorizeOrderRequestMessage m)
                    {
                        <span class="kwrd">return</span> m.SagaId == orderSaga.Id;
                    })
                .ExpectSendToDestination&lt;OrderStatusUpdatedMessage&gt;(
                    <span class="kwrd">delegate</span>(<span class="kwrd">string</span> destination, OrderStatusUpdatedMessage m)
                    {
                        <span class="kwrd">return</span> m.OrderId == externalOrderId &amp;&amp; destination == clientAddress;
                    })
                .ExpectSendToDestination&lt;TimeoutMessage&gt;(
                    <span class="kwrd">delegate</span>(<span class="kwrd">string</span> destination, TimeoutMessage m)
                    {
                        timeoutMessage = m;
                        <span class="kwrd">return</span> m.SagaId == orderSaga.Id &amp;&amp; destination == timeoutAddress;
                    })
                .When(<span class="kwrd">delegate</span> { orderSaga.Handle(createOrderMsg); });     

            Assert.IsFalse(orderSaga.Completed);     

            AuthorizeOrderResponseMessage response = <span class="kwrd">new</span> AuthorizeOrderResponseMessage();
            response.ManagerId = Guid.NewGuid();
            response.Authorized = <span class="kwrd">true</span>;
            response.SagaId = orderSaga.Id;     

            Saga.ExpectSendToDestination&lt;OrderStatusUpdatedMessage&gt;(
                    <span class="kwrd">delegate</span>(<span class="kwrd">string</span> destination, OrderStatusUpdatedMessage m)
                    {
                        <span class="kwrd">return</span> (destination == clientAddress &amp;&amp;
                                m.OrderId == externalOrderId &amp;&amp;
                                m.Status == OrderStatus.Authorized1);
                    })
                .When(<span class="kwrd">delegate</span> { orderSaga.Handle(response); });     

            Assert.IsFalse(orderSaga.Completed);     

            Saga.ExpectSendToDestination&lt;OrderStatusUpdatedMessage&gt;(
                    <span class="kwrd">delegate</span>(<span class="kwrd">string</span> destination, OrderStatusUpdatedMessage m)
                    {
                        <span class="kwrd">return</span> (destination == clientAddress &amp;&amp;
                                m.OrderId == externalOrderId &amp;&amp;
                                m.Status == OrderStatus.Accepted);
                    })
                .ExpectPublish&lt;OrderAcceptedMessage&gt;(
                    <span class="kwrd">delegate</span>(OrderAcceptedMessage m)
                    {
                        <span class="kwrd">return</span> (m.CustomerId == customerId);
                    })
                .When(<span class="kwrd">delegate</span> { orderSaga.Timeout(timeoutMessage.State); });     

            Assert.IsTrue(orderSaga.Completed);
        }
    }</pre>
</div>
<p>You might notice that this style is a bit similar to the fluent testing found in Rhino Mocks. That&#8217;s not coincidence. It actually makes use of Rhino Mocks internally. The thing that I discovered was that in order to test these sagas, you don&#8217;t need to actually see a mocking framework. All you should have to do is express how messages get sent, and under what criteria those messages are valid.</p>
<p>If you&#8217;re wondering what the OrderSaga looks like, you can find the code right here. It&#8217;s not a complete business process implementation, but its enough to understand how one would look like:</p>
<div style="overflow: scroll; width: 95%"><!-- code formatted by http://manoli.net/csharpformat/ --><br />
<style type="text/css">            .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; }</style>
<pre class="csharpcode"><span class="kwrd">using</span> System;
<span class="kwrd">using</span> System.Collections.Generic;
<span class="kwrd">using</span> ExternalOrderMessages;
<span class="kwrd">using</span> NServiceBus.Saga;
<span class="kwrd">using</span> NServiceBus;
<span class="kwrd">using</span> InternalOrderMessages;     

<span class="kwrd">namespace</span> ProcessingLogic
{
    [Serializable]
    <span class="kwrd">public</span> <span class="kwrd">class</span> OrderSaga : ISaga&lt;CreateOrderMessage&gt;,
        ISaga&lt;AuthorizeOrderResponseMessage&gt;,
        ISaga&lt;CancelOrderMessage&gt;
    {
        <span class="preproc">#region</span> config info     

        [NonSerialized]
        <span class="kwrd">private</span> IBus bus;
        <span class="kwrd">public</span> IBus Bus
        {
            set { <span class="kwrd">this</span>.bus = <span class="kwrd">value</span>; }
        }     

        [NonSerialized]
        <span class="kwrd">private</span> Reminder reminder;
        <span class="kwrd">public</span> Reminder Reminder
        {
            set { <span class="kwrd">this</span>.reminder = <span class="kwrd">value</span>; }
        }     

        <span class="preproc">#endregion</span>     

        <span class="kwrd">private</span> Guid id;
        <span class="kwrd">private</span> <span class="kwrd">bool</span> completed;
        <span class="kwrd">public</span> <span class="kwrd">string</span> clientAddress;
        <span class="kwrd">public</span> Guid externalOrderId;
        <span class="kwrd">public</span> <span class="kwrd">int</span> numberOfPendingAuthorizations = 2;
        <span class="kwrd">public</span> List&lt;CreateOrderMessage&gt; orderItems = <span class="kwrd">new</span> List&lt;CreateOrderMessage&gt;();     

        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(CreateOrderMessage message)
        {
            <span class="kwrd">this</span>.clientAddress = <span class="kwrd">this</span>.bus.SourceOfMessageBeingHandled;
            <span class="kwrd">this</span>.externalOrderId = message.OrderId;     

            <span class="kwrd">this</span>.orderItems.Add(message);     

            <span class="kwrd">if</span> (message.Completed)
            {
                <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i &lt; <span class="kwrd">this</span>.numberOfPendingAuthorizations; i++)
                {
                    AuthorizeOrderRequestMessage req = <span class="kwrd">new</span> AuthorizeOrderRequestMessage();
                    req.SagaId = <span class="kwrd">this</span>.id;
                    req.OrderData = orderItems;     

                    <span class="kwrd">this</span>.bus.Send(req);
                }
            }     

            <span class="kwrd">this</span>.SendUpdate(OrderStatus.Recieved);     

            <span class="kwrd">this</span>.reminder.ExpireIn(message.ProvideBy - DateTime.Now, <span class="kwrd">this</span>, <span class="kwrd">null</span>);
        }     

        <span class="kwrd">public</span> <span class="kwrd">void</span> Timeout(<span class="kwrd">object</span> state)
        {
            <span class="kwrd">if</span> (<span class="kwrd">this</span>.numberOfPendingAuthorizations &lt;= 1)
                <span class="kwrd">this</span>.Complete();
        }     

        <span class="kwrd">public</span> Guid Id
        {
            get { <span class="kwrd">return</span> id; }
            set { id = <span class="kwrd">value</span>; }
        }     

        <span class="kwrd">public</span> <span class="kwrd">bool</span> Completed
        {
            get { <span class="kwrd">return</span> completed; }
        }     

        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(AuthorizeOrderResponseMessage message)
        {
            <span class="kwrd">if</span> (message.Authorized)
            {
                <span class="kwrd">this</span>.numberOfPendingAuthorizations--;     

                <span class="kwrd">if</span> (<span class="kwrd">this</span>.numberOfPendingAuthorizations == 1)
                    <span class="kwrd">this</span>.SendUpdate(OrderStatus.Authorized1);
                <span class="kwrd">else</span>
                {
                    <span class="kwrd">this</span>.SendUpdate(OrderStatus.Authorized2);
                    <span class="kwrd">this</span>.Complete();
                }
            }
            <span class="kwrd">else</span>
            {
                <span class="kwrd">this</span>.SendUpdate(OrderStatus.Rejected);
                <span class="kwrd">this</span>.Complete();
            }
        }     

        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(CancelOrderMessage message)
        {     

        }     

        <span class="kwrd">private</span> <span class="kwrd">void</span> SendUpdate(OrderStatus status)
        {
            OrderStatusUpdatedMessage update = <span class="kwrd">new</span> OrderStatusUpdatedMessage();
            update.OrderId = <span class="kwrd">this</span>.externalOrderId;
            update.Status = status;     

            <span class="kwrd">this</span>.bus.Send(<span class="kwrd">this</span>.clientAddress, update);
        }     

        <span class="kwrd">private</span> <span class="kwrd">void</span> Complete()
        {
            <span class="kwrd">this</span>.completed = <span class="kwrd">true</span>;     

            <span class="kwrd">this</span>.SendUpdate(OrderStatus.Accepted);     

            OrderAcceptedMessage accepted = <span class="kwrd">new</span> OrderAcceptedMessage();
            accepted.Products = <span class="kwrd">new</span> List&lt;Guid&gt;(<span class="kwrd">this</span>.orderItems.Count);
            accepted.Amounts = <span class="kwrd">new</span> List&lt;<span class="kwrd">float</span>&gt;(<span class="kwrd">this</span>.orderItems.Count);     

            <span class="kwrd">this</span>.orderItems.ForEach(<span class="kwrd">delegate</span>(CreateOrderMessage m)
                                        {
                                            accepted.Products.AddRange(m.Products);
                                            accepted.Amounts.AddRange(m.Amounts);
                                            accepted.CustomerId = m.CustomerId;
                                        });     

            <span class="kwrd">this</span>.bus.Publish(accepted);
        }
    }
}</pre>
</div>
<p>All this code is online in the subversion repository under /Samples/Saga.</p>
<p>Questions, comments, and general thoughts are always appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/02/04/sagas-and-unit-testing-business-process-verification-made-easy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Performant and Explicit Domain Models</title>
		<link>http://www.udidahan.com/2007/06/04/performant-and-explicit-domain-models/</link>
		<comments>http://www.udidahan.com/2007/06/04/performant-and-explicit-domain-models/#comments</comments>
		<pubDate>Mon, 04 Jun 2007 20:31:34 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Simplicity]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2007/06/04/performant-and-explicit-domain-models/</guid>
		<description><![CDATA[Some Technical Difficulties

Ayende and I had an email conversation that started with me asking what would happen if I added an Order to a Customer’s &#8220;Orders&#8221; collection, when that collection was lazy loaded. My question was whether the addition of an element would result in NHibernate hitting the database to fill that collection. His answer [...]]]></description>
			<content:encoded><![CDATA[<h4>Some Technical Difficulties</h4>
<p style="padding: 0em 1em;">
Ayende and I had an email conversation that started with me asking what would happen if I added an Order to a Customer’s &#8220;Orders&#8221; collection, when that collection was lazy loaded. My question was whether the addition of an element would result in NHibernate hitting the database to fill that collection. His answer was a simple &#8220;yes&#8221;. In the case where a customer can have many (millions) of Orders, that’s just not a feasible solution. The technical solution was simple – just define the Orders collection on the Customer as &#8220;inverse=true&#8221;, and then to save a new Order, just write:
</p>
<div style="border: solid black 1px; padding: 0em 1em; background-color:beige; font-family:courier; ">session.Save( new Order(myCustomer) );</div>
<p style="padding: 0em 1em;">
Although it works, it’s not &#8220;DDD compliant&#8221; <img src='http://www.udidahan.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
<p style="padding: 0em 1em;">
In Ayende’s post <a href="http://ayende.com/Blog/archive/2007/05/29/Architecting-for-Performance.aspx">Architecting for Performance</a> he quoted a part of our email conversation.  The conclusion I reached was that in order to design performant domain models, you need to know the kinds of data volumes you’re dealing with. It affects both internals and the API of the model – when can you assume cascade, and when not. It’s important to make these kinds of things explicit in the Domain Model’s API.
</p>
<h4>How do you make &#8220;transparent persistence&#8221; explicit?</h4>
<p style="padding: 0em 1em;">
The problem occurs around &#8220;transparent persistence&#8221;. If we were to assume that the Customer object added the Order object to its Orders collection, then we wouldn’t have to explicitly save orders it creates, so we would write service layer code like this:
</p>
<div style="border: solid black 1px; background-color:beige; padding: 0em 1em; overflow:auto; width:600; font-family:courier">
using (IDBScope scope = this.DbServices.GetScope(TransactionOption.On))<br />
{<br />
	IOrderCreatingCustomer c = this.DbServices.Get&lt;IOrderCreatingCustomer&gt;(msg.CustomerId);<br />
	c.CreateOrder(message.OrderAmount);</p>
<p>	scope.Complete();<br />
}
</p></div>
<p style="padding: 0em 1em;">
On the other hand, if we designed our Domain Model around the million orders constraint, we would need to explicitly save the order, so we would write service layer code like this:
</p>
<div style="border: solid black 1px; background-color:beige; padding: 0em 1em; overflow:auto; width:600; font-family:courier">
using (IDBScope scope = this.DbServices.GetScope(TransactionOption.On))<br />
{<br />
	IOrderCreatingCustomer c = this.DbServices.Get&lt;IOrderCreatingCustomer&gt;(msg.CustomerId);<br />
	IOrder o = c.CreateOrder(message.OrderAmount);<br />
	this.DbServices.Save(o);</p>
<p>	scope.Complete();<br />
}
</p></div>
<p style="padding: 0em 1em;">
But the question remains, how do we communicate these guidelines to service layer developers from the Domain Model? There are a number of ways, but it’s important to decide on one and use it consistently. Performance and correctness require it.
</p>
<h4>Solution 1: Explicitness via Return Type</h4>
<p style="padding: 0em 1em;">
The first way is a little subtle, but you can do it with the return type of the &#8220;CreateOrder&#8221; method call. In the case where the Domain Model wishes to communicate that it handles transparent persistence by itself, have the method return &#8220;void&#8221;. Where the Domain Model wishes to communicate that it will not handle transparent persistence, have the method return the Order object created.
</p>
<p style="padding: 0em 1em;">
Another way to communicate the fact that an Order has been created that needs to be saved is with events. There are two sub-ways to do so:
</p>
<h4>Solution 2: Explicitness via Events on Domain Objects</h4>
<p style="padding: 0em 1em;">
The first is to just define the event on the customer object and have the service layer subscribe to it. It’s pretty clear that when the service layer receives a &#8220;OrderCreatedThatRequiresSaving&#8221; event, it should save the order passed in the event arguments.
</p>
<p style="padding: 0em 1em;">
The second realizes that the call to the customer object may come from some other domain object and that the service layer doesn’t necessarily know what can happen as the result of calling some method on the aggregate root. The change of state as the result of that method call may permeate the entire object graph. If each object in the graph raises its own events, its calling object will have to propagate that event to its parent – resulting in defining the same events  in multiple places, and each object being aware of all things possible with its great-grandchild objects. That is clearly bad.
</p>
<h4>What [ThreadStatic] is for</h4>
<p style="padding: 0em 1em;">
So, the solution is to use thread-static events.
</p>
<p style="padding: 0em 1em;">
[Sidebar] Thread-static events are just static events defined on a static class, where each event has the ThreadStaticAttribute applied to it. This attribute is important for server-side scenarios where multiple threads will be running through the Domain Model at the same time. The easiest thread-safe way to use static data is to apply the ThreadStaticAttribute.
</p>
<h4>Solution 3: Explicitness via Static Events</h4>
<p style="padding: 0em 1em;">
Each object raises the appropriate static event according to its logic. In our example, Customer would call:
</p>
<div style="border: solid black 1px; background-color:beige; padding: 0em 1em; font-family:courier">
DomainModelEvents.RaiseOrderCreatedThatRequiresSavingEvent(newOrder);
</div>
<p style="padding: 0em 1em;">And the service layer would write:</p>
<div style="border: solid black 1px; background-color:beige; padding: 0em 1em; font-family:courier">
DomainModelEvents.OrderCreatedThatRequiresSaving +=<br />
	delegate(object sender, OrderEventArgs e) { this.DbServices.Save(e.Order); };
</div>
<p style="padding: 0em 1em;">
The advantage of this solution is that it requires minimal knowledge of the Domain Model for the Service Layer to correctly work with it. It also communicates that anything that doesn’t raise an event will be persisted transparently behind the appropriate root object.
</p>
<h4>Statics and Testability</h4>
<p style="padding: 0em 1em;">
I know that many of you are wondering if I am really advocating the use of statics. The problem with most static classes is that they hurt testability because they are difficult to mock out. Often statics are used as Facades to hide some technological implementation detail. In this case, the static class is an inherent part of the Domain Model and does not serve as a Facade for anything.
</p>
<p style="padding: 0em 1em;">
When it comes to testing the Domain Model, we don’t have to mock anything out since the Domain Model is independent of all other concerns. This leaves us with unit testing at the single Domain Class level, which is pretty useless unless we’re TDD-ing the design of the Domain Model, in which case we’ll still be fiddling around with a bunch of classes at a time. Domain Models are best tested using State-Based Testing; get the objects into a given state, call a method on one of them, assert the resulting state. The static events don’t impede that kind of testing at all.
</p>
<h4>What if we used Injection instead of Statics?</h4>
<p style="padding: 0em 1em;">
Also, you’ll find that each Service Layer class will need to subscribe to all the Domain Model’s events, something that is easily handled by a base class. I will state that I have tried doing this without a static class, and injecting that singleton object into the Service Layer classes, and in that setter having them subscribe to its events. This was also pulled into a base class. The main difference was that the Dependency Injection solution required injecting that object into Domain Objects as well. Personally, I’m against injection for domain objects. So all in all, the static solution comes with less overhead than that based on injection.
</p>
<h4>Summary</h4>
<p style="padding: 0em 1em;">
In summary, beyond the &#8220;technical basics&#8221; of being aware of your data volumes and designing your Domain Model to handle each use case performantly, I’ve found these techniques useful for designing its API as well as communicating my intent around persistence transparency. So give it a try. I’d be grateful to hear your thoughts on the matter as well as what else you’ve found that works.
</p>
<p><u style="padding: 0em 1em;">Related posts:</u></p>
<ul style="padding: 0em 1em;">
<li>
<a href="http://udidahan.weblogs.us/2007/04/23/fetching-strategy-design/">Fetching Strategy Design</a> &#8211; showing how to separate the concern of eager loading from both your Domain Model and your Service Layer.
</li>
<li>
<a href="http://udidahan.weblogs.us/2007/03/06/better-domain-driven-design-implementation/">Better Domain-Driven Design Implementation</a> &#8211; showing the basics of how valuable interfaces between your Domain Model and the Service Layer can be.
</li>
<li>
<a href="http://udidahan.weblogs.us/2007/04/15/lazy-loading-and-how-messaging-fixes-everything-again/">Lazy Loading, and how messaging fixes everything again</a> &#8211; describing the advantage of O/R mapping your message classes as well.
</li>
<li>
<a href="http://udidahan.weblogs.us/2007/03/28/query-objects-vs-methods-on-a-repository/">Query Objects vs Methods on a Repository</a> &#8211; discussing the scalability (in terms of number of developers and queries) of Query Objects.
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2007/06/04/performant-and-explicit-domain-models/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Layering &#8211; too simplistic to actually work</title>
		<link>http://www.udidahan.com/2007/06/03/layering-too-simplistic-to-actually-work/</link>
		<comments>http://www.udidahan.com/2007/06/03/layering-too-simplistic-to-actually-work/#comments</comments>
		<pubDate>Sun, 03 Jun 2007 21:35:34 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Simplicity]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2007/06/03/layering-too-simplistic-to-actually-work/</guid>
		<description><![CDATA[After seeing Mark&#8217;s post on Reasons for Isolation describing the ways Layered Architectures break down, and the ways making it more testable can change it, I&#8217;ve got to wonder &#8211; is Layering just too simplistic to actually work?
Just the other day I was doing a design review for a fairly simple Smart Client whose design [...]]]></description>
			<content:encoded><![CDATA[<p>After seeing Mark&#8217;s post on <a href="http://blogs.msdn.com/ploeh/archive/2007/05/30/ReasonsForIsolation.aspx">Reasons for Isolation</a> describing the ways Layered Architectures break down, and the ways making it more testable can change it, I&#8217;ve got to wonder &#8211; is Layering just too simplistic to actually work?</p>
<p>Just the other day I was doing a design review for a fairly simple <a href="http://udidahan.weblogs.us/category/smart-client/">Smart Client</a> whose design was layered. In order to stay away from interfaces that accepted dozens of ints, strings, and dates, they wanted to have each layer talk to the other using &#8220;entities&#8221;. So where are these entities defined &#8211; oh, in a &#8220;vertical layer&#8221; that all the horizontal layers talk to.</p>
<p>OK, so we&#8217;ve taken the simplistic one-dimensional layered architecture and added a dimension. What now?</p>
<p>Well, it seems that having the business logic and the entities in separate layers goes against one of the most basic <a href="http://udidahan.weblogs.us/category/oo/">Object Oriented</a> principles &#8211; encapsulation. So, let&#8217;s put the entities back in the <a href="http://udidahan.weblogs.us/category/business-rules/">Business Logic</a> Layer. But then how will the <a href="http://udidahan.weblogs.us/category/data-access/">Data Access</a> Layer accept those objects as parameters?</p>
<p>So, that is solved by keeping Entity Interfaces in the &#8220;vertical&#8221; shared &#8220;layer&#8221;, and having the entities in the business logic layer implement those interfaces. That way, the data access layer can still accept parameters corresponding to those interfaces:</p>
<pre>void InsertCustomer(Shared.Entities.ICustomer customer);</pre>
<p>So far so good. Now, we want more testable UI layer code &#8211; so we use Model-View-Controller (MVC) &#8211; of whichever flavor suits your fancy. I&#8217;d say that <a href="http://www.martinfowler.com/eaaDev/SupervisingPresenter.html">Supervising Controller</a> is a must. You could also add another presenter for more complex screens as in <a href="http://www.martinfowler.com/eaaDev/PassiveScreen.html">Passive View</a>, but I&#8217;d be less strict on that. So, in which layer do these Controllers/Presenters sit? And is the Business Logic Layer the Model? Or is the Model just part of it?</p>
<p>Well, our Supervising Controllers are those who decide what action to do and when, where to get the data from, etc. That sounds like business logic to me. So let&#8217;s put them in the BLL. Presenters for the Passive View are much more UI centered, so let&#8217;s put them in the Presentation Layer. But we don&#8217;t want them tied to the implementation of the view, so we&#8217;ll put them in a separate package, and have them depend only on the view&#8217;s interface. So we&#8217;ll put the view interfaces in a package separate from the view implementation as well.</p>
<p>If it wasn&#8217;t clear up to this point, all the questions raised in this post are architectural in nature &#8211; as in they have a substantial impact on the structure and flow of the system, and will definitely have a profound effect on its maintainability. In other words, if you think that Layer Diagram covers your design &#8211; you&#8217;re probably deluding yourself. Personally, I think that&#8217;s why many developers consider architects to be &#8220;out of touch with the real world&#8221;.</p>
<p>When you have a design that answers these, and other architectural concerns, you&#8217;ll find that layering is of little importance. The specific constraints on each package are what counts. The fact that the Presentation Layer can talk to the Business Logic Layer doesn&#8217;t mean that the classes in your Views Implementation Package can. A large part of an architects work is to specify these constraints, and communicate them to the team. Tools like FxCop may help in terms of enforcing these constraints, but I believe that getting the team to actually &#8220;buy-in&#8221; is more effective.</p>
<p>Single-dimensional layered architectures don&#8217;t work. They violate Einstein&#8217;s maxim:</p>
<p><i>Make everything as simple as possible, but not simpler.</i></p>
<p>Layering &#8211; &#8220;simpler&#8221; to the point of simplistic.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2007/06/03/layering-too-simplistic-to-actually-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>NHibernate will rule, because Ayende already does</title>
		<link>http://www.udidahan.com/2007/05/20/nhibernate-will-rule-because-ayende-already-does/</link>
		<comments>http://www.udidahan.com/2007/05/20/nhibernate-will-rule-because-ayende-already-does/#comments</comments>
		<pubDate>Sun, 20 May 2007 21:56:30 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2007/05/20/nhibernate-will-rule-because-ayende-already-does/</guid>
		<description><![CDATA[First I find out that NHibernate does support &#8220;Persistence by Reachability&#8221;, even though the docs say it doesn&#8217;t. Next, Ayende makes it support multiple queries in a single DB roundtrip, something I&#8217;ve been asking all the other O/R mappers out there to do. To top it off, he&#8217;s got his sights set on solving the [...]]]></description>
			<content:encoded><![CDATA[<p>First I find out that <a href="http://udidahan.weblogs.us/category/nhibernate/">NHibernate</a> does support &#8220;Persistence by Reachability&#8221;, even though the docs say it doesn&#8217;t. Next, Ayende makes it support <a href="http://ayende.com/Blog/archive/2007/05/20/NHibernate-Multi-Criteria.aspx">multiple queries in a single DB roundtrip</a>, something I&#8217;ve been asking all the other O/R mappers out there to do. To top it off, he&#8217;s got his sights set on solving the issues I raised in my talk on Complex Business Logic with DDD and O/R Mapping at DevTeach. That&#8217;s right, he&#8217;s going to give me my decorators and state machines.</p>
<p>I love you, Oren.</p>
<p>I know that the ADO.NET Entity Framework guys are open to this as well, but I&#8217;m pretty sure that the &#8220;Entity Model&#8221; thinking will hold them back. You just can&#8217;t divorce data and behavior &#8211; not when employing state machines or decorators.</p>
<p>I&#8217;m sold.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2007/05/20/nhibernate-will-rule-because-ayende-already-does/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Domain Model Pattern</title>
		<link>http://www.udidahan.com/2007/04/21/domain-model-pattern/</link>
		<comments>http://www.udidahan.com/2007/04/21/domain-model-pattern/#comments</comments>
		<pubDate>Sat, 21 Apr 2007 14:53:38 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[DataSets]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2007/04/21/domain-model-pattern/</guid>
		<description><![CDATA[When implementing a domain model, often object-relational mapping technologies are used. Like many tools, with their use comes the danger of abuse &#8211; abuse to the point of invalidating the benefits of the pattern itself.
From some pointers about how to use (and not to use) these tools, see why object-relational mapping sucks.
Martin Fowler&#8217;s has this [...]]]></description>
			<content:encoded><![CDATA[<p>When implementing a domain model, often object-relational mapping technologies are used. Like many tools, with their use comes the danger of abuse &#8211; abuse to the point of invalidating the benefits of the pattern itself.</p>
<p>From some pointers about how to use (and not to use) these tools, see <a href="http://udidahan.weblogs.us/2008/06/25/object-relational-mapping-sucks/">why object-relational mapping sucks</a>.</p>
<p>Martin Fowler&#8217;s has this to say about the <a href="http://martinfowler.com/eaaCatalog/domainModel.html">Domain Model Pattern</a>:</p>
<blockquote><p>
At its worst business logic can be very complex. Rules and logic describe many different cases and slants of behavior, and it&#8217;s this complexity that objects were designed to work with. A Domain Model creates a web of interconnected objects, where each object represents some meaningful individual, whether as large as a corporation or as small as a single line on an order form.
</p></blockquote>
<p>In short, using <a href="http://udidahan.weblogs.us/category/oo/">Object-Oriented techniques</a> to handle the complexity.</p>
<p>A more comprehensive discussion about what happens when it is not used can be found under the <a href="http://www.martinfowler.com/bliki/AnemicDomainModel.html">Anemic Domain Model Anti-Pattern</a>:</p>
<blockquote><p>
The basic symptom of an Anemic Domain Model is that at first blush it looks like the real thing. There are objects, many named after the nouns in the domain space, and these objects are connected with the rich relationships and structure that true domain models have. The catch comes when you look at the behavior, and you realize that there is very little behavior on these objects. Indeed often these models come with design rules that say that you are not to put any domain logic in the the domain objects. Instead there are a set of service objects which capture all the domain logic. These services live on top of the domain model and use the domain model for data.</p>
<p>The fundamental horror of this anti-pattern is that it&#8217;s so contrary to the basic idea of object-oriented design; which is to combine data and process together. The anemic domain model is really just a procedural style design&#8230;
</p></blockquote>
<p>In terms of <a href="http://udidahan.weblogs.us/category/ddd/">Domain-Driven Design</a>, this pattern is also known as Domain Layer.</p>
<p>Domain Models do a lot for encapsulating <a href="http://udidahan.weblogs.us/category/business-rules/">Business Rules</a>, thus making them amenable to automated testing. This hinges on keeping the Domain Model independent of things related to <a href="http://udidahan.weblogs.us/category/data-access/">Data Access</a>.</p>
<p>It is therefore almost required to use some kind of <a href="http://udidahan.weblogs.us/category/nhibernate/">Object/Relational Mapping</a> tool to make it possible to persist objects belonging to the domain model to databases and other kinds of storage.</p>
<p>The use of <a href="http://udidahan.weblogs.us/category/datasets/">DataSets</a> in .NET is often a sign of the Anemic Domain Model Anti-Pattern.</p>
<p>One thing to keep in mind when working on a domain model is that you probably won&#8217;t get it &#8220;right&#8221; the first time, and will have re-work the division of responsibility a couple of times. Techniques like <a href="http://udidahan.weblogs.us/category/tdd/">Test-Driven Development</a> help out immensely for that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2007/04/21/domain-model-pattern/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Dataset – O/R mapping rumble at TechEd MVP Dinner</title>
		<link>http://www.udidahan.com/2006/11/09/dataset-%e2%80%93-or-mapping-rumble-at-teched-mvp-dinner/</link>
		<comments>http://www.udidahan.com/2006/11/09/dataset-%e2%80%93-or-mapping-rumble-at-teched-mvp-dinner/#comments</comments>
		<pubDate>Thu, 09 Nov 2006 13:11:05 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[DataSets]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://wp_630.weblogs.us/archives/348</guid>
		<description><![CDATA[So, last night I was at the MVP dinner in TechEd and everything was nice. We had a nice meal, conversation was nice, weather was… nice. And then the volume started to rise, slowly at first, so as you don’t quite notice it. After a bit, you kind of stop talking and look around. And then I hear it…
<br/><br/>
&#60;WWF announcer voice&#62;<br/>
Are you ready… to RUMBLE !?!?<br/>
&#60;/WWF announcer voice&#62;<br/>
<br/>
It was datasets vs. O/R mapping, a slight twist on the infamous datasets vs. custom objects debate, all over again. They pulled me in, kicking and screaming, I swear, I really do. The lines were drawn, maintainability, performance, all the things that architects like to philosophize about in terms of other people’s work.
<br/><br/>
Anyway, I won’t give you the play-by-play ‘cause we were there almost all night. I’ll just cut to the chase.
<br/><br/>
First things first – any comparison of solutions without the context of a problem leads nowhere, fast, and stays there. So the first question I asked (when I got the chance to speak) was “are we talking about querying/reporting here?” and the answer was something like “well, yeah, but a lot of other things too”. So my suggestion was that we discuss the solutions in terms of two contexts -  querying/reporting and OLTP.
<br/><br/>
What I mean by OLTP is the data-updating kind of work that you do on certain items. Examples of this include “insert order”, “change customer address”, and “discount product”. Querying/reporting doesn’t change data, and often involves dealing with large sets of data pulled from different kinds of entities (in ERD terms).
<br/><br/>
Luckily, my suggestion to deal with them separately was accepted. Secondly, I proposed that an object model (specifically implementing the Domain Model pattern) designed for OLTP would perform poorly when used for querying/reporting – simply because it wasn’t designed for it. The structure of a domain model is such that it makes it possible to define / implement business rules in one place. That’s possible, not easy. 
<br/><br/>
Well, the dataset people weren’t going to just hand me the OLTP side of the equation without a fight, so they mentioned how easy it was to just “AcceptChanges”, and that my way was much more complex. My rebuttal came in the form of a question (are you seeing a pattern here?): Do you just swallow DbConcurrencyExceptions are do you throw all the user’s changes away when it happens? I didn’t quite make out the answer since there was a lot of mumbling going on, but I’m pretty sure they had one. I mean, you can’t develop multi-user systems using datasets without running into this situation.
<br/><br/>
The example that clinched OLTP was this. Two users perform a change to the same entity at the same time – one updates the customer’s marital status, the other changes their address. At the business level, there is no concurrency problem here. Both changes should go through. When using datasets, and those changes are bundled up with a bunch of other changes, and the whole snapshot is sent together from each user, you get a DbConcurrencyException. Like I said, I’m sure there’s a solution to it, I just haven’t heard it yet.
<br/><br/>
Now, here’s where things get interesting. I didn’t say that using a domain model automatically solves this problem. Rather, I described how each client could send a specific message, one a ChangeMaritalStatusMessage, the other a ChangeAddressMessage, to the server – in essence, giving the server the context in which each bit of data is relevant. The server could just open a transaction, get the customer object based on its Id, call a method on the customer (ChangeMaritalStatus or ChangeAddress), and commit the transaction. If two of these messages got to the server at the same time, the transactions would just cause them to be performed serially, and both transactions would succeed. The important part here is not losing the context of the changes.
<br/><br/>
When we talked about querying/reporting, things seemed quite a bit clearer. Datasets, or rather datatables seemed like a fine solution – most 3rd party controls support them out of the box. One guy mentioned that datasets performed poorly for large sets of data and that by designing custom entities for the result set, he could improve performance and memory utilization by, like, 70%. To tell you the truth, I think that if you need the performance, do it, if not, just use datasets. There isn’t much of an issue of correctness.
<br/><br/>
Just as an ending comment, in response to something someone said about scalability, I asked if they were reporting against the live OLTP data. The response was “yes”. Well, there’s a database scalability problem if I ever saw one. OLTP works most correctly when employing transactions that have an isolation level of serializable. The problem with them is that they lock up the whole table, or get blocked when a table scan is going on. Querying often results in a table scan. You can see the problem. Anyway, a common solution to this problem is to just reduce the isolation level, a quick fix that improves performance almost immediately. You take one hit in that your reports may be showing incorrect data, especially if they do aggregate type work. You might take another hit if your OLTP transactions need to do aggregate type work themselves. That second hit is pretty much unacceptable. A different solution is to accept the fact that the heaviest querying can usually show data that isn’t up to date up to the second.
<br/><br/>
In such a solution, you would have another database for reporting. It wouldn’t be just a replica of the OLTP database, but rather a lot more denormalized – which is a really not nice way of saying designed for reporting. You could then move the data from your OLTP database to the reporting database in some way (more to come on this topic) and you increase the scalability of your database. Just to define that a bit better – your OLTP database will be able to handle more transactions per unit of time, and reports will run faster, meaning that you will both improve their latency and the number of queries that can be handled per unit of time.
<br/><br/>
Anyway, I was pretty tired after all that, but if I had to sum it up I’d say something like this: before debating solutions, define the problem, you get a lot more insight into the solutions and you get it faster. That’s just win-win all around.]]></description>
			<content:encoded><![CDATA[<p>So, last night I was at the MVP dinner in TechEd and everything was nice. We had a nice meal, conversation was nice, weather was… nice. And then the volume started to rise, slowly at first, so as you don’t quite notice it. After a bit, you kind of stop talking and look around. And then I hear it…</p>
<p>&lt;WWF announcer voice&gt;<br />
Are you ready… to RUMBLE !?!?<br />
&lt;/WWF announcer voice&gt;</p>
<p>It was <a href="http://udidahan.weblogs.us/category/datasets/">Datasets</a> vs. <a href="http://udidahan.weblogs.us/category/nhibernate/">O/R mapping</a>, a slight twist on the infamous datasets vs. custom objects debate, all over again. They pulled me in, kicking and screaming, I swear, I really do. The lines were drawn, maintainability, performance, all the things that architects like to philosophize about in terms of other people’s work.</p>
<p>Anyway, I won’t give you the play-by-play ‘cause we were there almost all night. I’ll just cut to the chase.</p>
<p>First things first – any comparison of solutions without the context of a problem leads nowhere, fast, and stays there. So the first question I asked (when I got the chance to speak) was “are we talking about querying/reporting here?” and the answer was something like “well, yeah, but a lot of other things too”. So my suggestion was that we discuss the solutions in terms of two contexts &#8211;  querying/reporting and OLTP.</p>
<p>What I mean by OLTP is the data-updating kind of work that you do on certain items. Examples of this include “insert order”, “change customer address”, and “discount product”. Querying/reporting doesn’t change data, and often involves dealing with large sets of data pulled from different kinds of entities (in ERD terms).</p>
<p>Luckily, my suggestion to deal with them separately was accepted. Secondly, I proposed that an object model (specifically implementing the <a href="http://udidahan.weblogs.us/2007/04/21/domain-model-pattern/">Domain Model pattern</a>) designed for OLTP would perform poorly when used for querying/reporting – simply because it wasn’t designed for it. The structure of a domain model is such that it makes it possible to define / implement <a href="http://udidahan.weblogs.us/category/business-rules/">business rules</a> in one place. That’s possible, not easy. </p>
<p>Well, the dataset people weren’t going to just hand me the OLTP side of the equation without a fight, so they mentioned how easy it was to just “AcceptChanges”, and that my way was much more complex. My rebuttal came in the form of a question (are you seeing a pattern here?): Do you just swallow DbConcurrencyExceptions are do you throw all the user’s changes away when it happens? I didn’t quite make out the answer since there was a lot of mumbling going on, but I’m pretty sure they had one. I mean, you can’t develop multi-user systems using datasets without running into this situation.</p>
<p>The example that clinched OLTP was this. Two users perform a change to the same entity at the same time – one updates the customer’s marital status, the other changes their address. At the business level, there is no <a href="http://udidahan.weblogs.us/2007/01/22/realistic-concurrency/">concurrency</a> problem here. Both changes should go through. When using datasets, and those changes are bundled up with a bunch of other changes, and the whole snapshot is sent together from each user, you get a DbConcurrencyException. Like I said, I’m sure there’s a solution to it, I just haven’t heard it yet.</p>
<p>Now, here’s where things get interesting. I didn’t say that using a domain model automatically solves this problem. Rather, I described how each client could send a specific message, one a ChangeMaritalStatusMessage, the other a ChangeAddressMessage, to the server – in essence, giving the server the context in which each bit of data is relevant. The server could just open a transaction, get the customer object based on its Id, call a method on the customer (ChangeMaritalStatus or ChangeAddress), and commit the transaction. If two of these messages got to the server at the same time, the transactions would just cause them to be performed serially, and both transactions would succeed. The important part here is not losing the context of the changes.</p>
<p>When we talked about querying/reporting, things seemed quite a bit clearer. Datasets, or rather datatables seemed like a fine solution – most 3rd party controls support them out of the box. One guy mentioned that datasets performed poorly for large sets of data and that by designing custom entities for the result set, he could improve performance and memory utilization by, like, 70%. To tell you the truth, I think that if you need the performance, do it, if not, just use datasets. There isn’t much of an issue of correctness.</p>
<p>Just as an ending comment, in response to something someone said about <a href="http://udidahan.weblogs.us/category/scalability/">scalability</a>, I asked if they were reporting against the live OLTP data. The response was “yes”. Well, there’s a database scalability problem if I ever saw one. OLTP works most correctly when employing transactions that have an isolation level of serializable. The problem with them is that they lock up the whole table, or get blocked when a table scan is going on. Querying often results in a table scan. You can see the problem. Anyway, a common solution to this problem is to just reduce the isolation level, a quick fix that improves performance almost immediately. You take one hit in that your reports may be showing incorrect data, especially if they do aggregate type work. You might take another hit if your OLTP transactions need to do aggregate type work themselves. That second hit is pretty much unacceptable. A different solution is to accept the fact that the heaviest querying can usually show data that isn’t up to date up to the second.</p>
<p>In such a solution, you would have another database for reporting. It wouldn’t be just a replica of the OLTP database, but rather a lot more denormalized – which is a really not nice way of saying designed for reporting. You could then move the data from your OLTP database to the reporting database in some way (more to come on this topic) and you increase the scalability of your database. Just to define that a bit better – your OLTP database will be able to handle more transactions per unit of time, and reports will run faster, meaning that you will both improve their latency and the number of queries that can be handled per unit of time.</p>
<p>Anyway, I was pretty tired after all that, but if I had to sum it up I’d say something like this: before debating solutions, define the problem, you get a lot more insight into the solutions and you get it faster. That’s just win-win all around.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2006/11/09/dataset-%e2%80%93-or-mapping-rumble-at-teched-mvp-dinner/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DDD – why bother?</title>
		<link>http://www.udidahan.com/2006/10/07/ddd-%e2%80%93-why-bother/</link>
		<comments>http://www.udidahan.com/2006/10/07/ddd-%e2%80%93-why-bother/#comments</comments>
		<pubDate>Sun, 08 Oct 2006 05:49:08 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://wp_630.weblogs.us/archives/332</guid>
		<description><![CDATA[Domain Driven Design (DDD), alongside its growing popularity, is experiencing some growing pains. The Domain Model pattern, documented in the Patterns of Enterprise Application Architecture book, is at the heart of DDD yet the division of responsibility between it and other DDD patterns like Service Layer isn’t quite clear. To make matters worse, the value of the Domain Model pattern relative to simpler code-generation techniques remains vague. The one thing that has reached a wide consensus is that it requires a higher level of skill to employ these techniques than continue using the widespread procedural programming practices. There is one overwhelming reason to do it anyway, though, and that is that it is the cheapest way to get a system up and running right. The reasoning behind this has to do with business rules...]]></description>
			<content:encoded><![CDATA[<p>Domain Driven Design (DDD), alongside its growing popularity, is experiencing some growing pains. The <a href="http://udidahan.weblogs.us/2007/04/21/domain-model-pattern/">Domain Model pattern</a>, documented in the Patterns of Enterprise Application Architecture book, is at the heart of DDD yet the division of responsibility between it and other DDD patterns like Service Layer isn’t quite clear. To make matters worse, the value of the Domain Model pattern relative to simpler code-generation techniques remains vague. The one thing that has reached a wide consensus is that it requires a higher level of skill to employ these techniques than continue using the widespread procedural programming practices. There is one overwhelming reason to do it anyway, though, and that is that it is the cheapest way to get a system up and running right. The reasoning behind this has to do with <a href="http://udidahan.weblogs.us/category/business-rules/">business rules</a>.</p>
<p>An exceedingly large number of systems are being built and modified today in order to support business rules. Beyond just computerizing the management of data in the enterprise, today’s systems are required to computerize the business processes that use that data, and these processes are built upon business rules.</p>
<p>A rule is composed of two main elements, a clause and an action. The clause defines under what circumstances the action is to be activated. An example of a rule employed in the business environment might be “if the customer is a preferred customer, then give them a 5% discount on all orders”. The rich behavior of an enterprise is governed by these interacting rules. Consider the result of adding another business rule like “if the customer has ordered $100,000 or more in the last year, then they are to be considered a preferred customer”. If a customer is not preferred, but then sends in a new order that puts them over the limit, how is the system to behave? Conversely, what if a preferred customer cancels an order bringing them under the limit?</p>
<p>I’ve seen too many projects that have been tasked with implementing these kinds of behaviors yet were unable to get the system running right. Customers that should have gotten discounts didn’t in some cases, and those that should not have enjoyed a discount did at times. After much time was spent trying to track down what part of the code was wrong, changing some code, testing, over and over again, an executive decision was made to put the system into production as is. The harm to the business was deemed cheaper in this case then not putting the system in production. Is it any wonder that business is skeptical of IT’s ability to handle the agile enterprise of the future where, not only will the business rules be more complex, they will be changing all the time.</p>
<p>The Domain Model pattern encapsulates these business rules in such a way that they will be run even when not directly invoked. This is especially critical when one rule triggers another. Intelligent use of <a href="http://udidahan.weblogs.us/category/oo/">OO principles</a> when designing the domain can help you altogether avoid the jump in complexity found in Business Rules Engines.</p>
<p>Finally, we need to understand that supporting techniques like <a href="http://udidahan.weblogs.us/category/nhibernate/">Object/Relational (O/R) mapping</a> are but a means, and not the end. The discussions around DDD often get mired down in the relative costs of O/R mapping and procedural code generation. Persistence is a solved problem, a technical problem that has no meaning to business. Is it not time to raise the discussion to the level of business? If the only problem you are trying to solve is the manipulation of data in a database, then don’t bother with DDD and its descendents. It won’t make your life any easier. If, on the other hand, business has gotten sick of IT deciding for them how to run their business; if you are the one tasked with building the right system, you just won’t be able to do it unless you build the system right – DDD won’t be a bother, but a necessity.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2006/10/07/ddd-%e2%80%93-why-bother/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SOA Meets Business Rules</title>
		<link>http://www.udidahan.com/2006/09/05/soa-meets-business-rules/</link>
		<comments>http://www.udidahan.com/2006/09/05/soa-meets-business-rules/#comments</comments>
		<pubDate>Wed, 06 Sep 2006 05:41:30 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://wp_630.weblogs.us/archives/315</guid>
		<description><![CDATA[After reading <a href="http://www.ebizq.net/topics/soa/features/7249.html">this article</a> you just might be thinking that business rules management systems (BRMS's) are the key to what SOA was supposed to bring (now that we're in the <a href="http://blogs.zdnet.com/SAAS/index.php?p=158">trough of disillusionment</a>) - that is, if you haven't tried working with these beasts before.
<br/><br/>
There are several now-well-known fallacies around BRMS's. One is that business analysts write the business rules, in plain English, and these rules do not require a programmer to enter/code them into the system; the BRMS handles all that.
<br/><br/>
<em>"Costly, time-consuming transformation from business terms to programmer requirements and subsequent implementation can be reduced or eliminated. This in turn empowers business analysts to create and maintain the business logic directly, allowing them to make changes with little or no IT intervention."</em>
<br/><br/>
But, the author forgets to mention who performs these trivial activities:
<br/><br/>
<em>"After testing and validation the business logic can be deployed to a business rules server."</em>
<br/><br/>
This brings me to the single most important thing anyone should know about the business rules approach. <b>Employing a rules engine opens up the possibility that the system will behave non-deterministically.</b> Consider that only 3 non-linear equations are needed to create chaotic behavior, and try to imagine what will happen in a system with hundreds and thousands of business rules that affect each other.
<br/><br/>
Yes, business rules often end up changing data which, in turn, causes other business rules to be activated. No, it is not humanly possible to look at such a set of rules and predict what will happen when a certain event occurs. You can fire such an event in a test environment and see what will happen. When the results aren't what you wanted (that would be all the time), you can use the BRMS's to show you which rules were run as a result of that event. Let's see a business analyst debug that. "Plain English" my butt.
<br/><br/>
Let's not go too much further on the BRMS on its own, but rather view it in the light of service-oriented architectures.
<br/><br/>
In the article, an approach is shown that is supposed to bring you the best of both SOA and the Business Rules Philosophy. I'm afraid that my experience has been otherwise. The author states:
<br/><br/>
<em>"The business rules server may be implemented as a Web Service that is accessible to many SOA enabled applications across the organization. By supporting shared business logic within the SOA architecture that can be addressed by many disparate applications, organizations can reduce redundancy, speed implementation, lessen inconsistency and improve the overall efficiency of both the applications and the business processes they serve."</em>
<br/><br/>
This desire to concentrate all business logic into a single BRMS embodies <a href="http://www.theserverside.com/news/thread.tss?thread_id=26043">the 11th fallacy of enterprise computing</a>, as put forth by James Gosling "Business logic can and should be centralized". Not to mention that this goes against the grain of SOA where each service is autonomous and is entirely responsible for all of its data and behavior. There's another reason why the business rules community is pro centralization - the BRMS's are so damned expensive.
<br/><br/>
Anyway, I don't want to be all doom and gloom today so I'll end on a positive note. While good OO practices and the use of the <a href="http://www.martinfowler.com/eaaCatalog/domainModel.html">Domain Model Pattern</a> will take you VERY far in making business rules explicit, and, therefore, their maintenance and evolution much less costly, sometimes you really neeed a BRMS. Just so you know, the investment you made in the domain model is not written off when you "upsize" to business rules - the rules themselves are written using the concepts exposed in the domain. As for the service, well, its message handlers keep on working the same as they always did; parsing messages and making calls on the domain model. The business rules will just respond to the events raised by the domain; message handlers don't need to call into the BRMS.
<br/><br/>
Oh, and the next time somebody tells you how business analysts will be doing the work instead of programmers, ask them if they're willing to take the debugging too :)]]></description>
			<content:encoded><![CDATA[<p>After reading <a href="http://www.ebizq.net/topics/soa/features/7249.html">this article</a> you just might be thinking that business rules management systems (BRMS&#8217;s) are the key to what SOA was supposed to bring (now that we&#8217;re in the <a href="http://blogs.zdnet.com/SAAS/index.php?p=158">trough of disillusionment</a>) &#8211; that is, if you haven&#8217;t tried working with these beasts before.</p>
<p>There are several now-well-known fallacies around BRMS&#8217;s. One is that business analysts write the business rules, in plain English, and these rules do not require a programmer to enter/code them into the system; the BRMS handles all that.</p>
<p><em>&#8220;Costly, time-consuming transformation from business terms to programmer requirements and subsequent implementation can be reduced or eliminated. This in turn empowers business analysts to create and maintain the business logic directly, allowing them to make changes with little or no IT intervention.&#8221;</em></p>
<p>But, the author forgets to mention who performs these trivial activities:</p>
<p><em>&#8220;After testing and validation the business logic can be deployed to a business rules server.&#8221;</em></p>
<p>This brings me to the single most important thing anyone should know about the business rules approach. <b>Employing a rules engine opens up the possibility that the system will behave non-deterministically.</b> Consider that only 3 non-linear equations are needed to create chaotic behavior, and try to imagine what will happen in a system with hundreds and thousands of business rules that affect each other.</p>
<p>Yes, business rules often end up changing data which, in turn, causes other business rules to be activated. No, it is not humanly possible to look at such a set of rules and predict what will happen when a certain event occurs. You can fire such an event in a test environment and see what will happen. When the results aren&#8217;t what you wanted (that would be all the time), you can use the BRMS&#8217;s to show you which rules were run as a result of that event. Let&#8217;s see a business analyst debug that. &#8220;Plain English&#8221; my butt.</p>
<p>Let&#8217;s not go too much further on the BRMS on its own, but rather view it in the light of service-oriented architectures.</p>
<p>In the article, an approach is shown that is supposed to bring you the best of both SOA and the Business Rules Philosophy. I&#8217;m afraid that my experience has been otherwise. The author states:</p>
<p><em>&#8220;The business rules server may be implemented as a Web Service that is accessible to many SOA enabled applications across the organization. By supporting shared business logic within the SOA architecture that can be addressed by many disparate applications, organizations can reduce redundancy, speed implementation, lessen inconsistency and improve the overall efficiency of both the applications and the business processes they serve.&#8221;</em></p>
<p>This desire to concentrate all business logic into a single BRMS embodies <a href="http://www.theserverside.com/news/thread.tss?thread_id=26043">the 11th fallacy of enterprise computing</a>, as put forth by James Gosling &#8220;Business logic can and should be centralized&#8221;. Not to mention that this goes against the grain of SOA where each service is autonomous and is entirely responsible for all of its data and behavior. There&#8217;s another reason why the business rules community is pro centralization &#8211; the BRMS&#8217;s are so damned expensive.</p>
<p>Anyway, I don&#8217;t want to be all doom and gloom today so I&#8217;ll end on a positive note. While good OO practices and the use of the <a href="http://www.martinfowler.com/eaaCatalog/domainModel.html">Domain Model Pattern</a> will take you VERY far in making business rules explicit, and, therefore, their maintenance and evolution much less costly, sometimes you really neeed a BRMS. Just so you know, the investment you made in the domain model is not written off when you &#8220;upsize&#8221; to business rules &#8211; the rules themselves are written using the concepts exposed in the domain. As for the service, well, its message handlers keep on working the same as they always did; parsing messages and making calls on the domain model. The business rules will just respond to the events raised by the domain; message handlers don&#8217;t need to call into the BRMS.</p>
<p>Oh, and the next time somebody tells you how business analysts will be doing the work instead of programmers, ask them if they&#8217;re willing to take the debugging too <img src='http://www.udidahan.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2006/09/05/soa-meets-business-rules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>To map, or not to map?</title>
		<link>http://www.udidahan.com/2006/03/11/to-map-or-not-to-map/</link>
		<comments>http://www.udidahan.com/2006/03/11/to-map-or-not-to-map/#comments</comments>
		<pubDate>Sun, 12 Mar 2006 04:22:00 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://wp_630.weblogs.us/archives/267</guid>
		<description><![CDATA[I had this discussion with Clemens when he was last here in Israel - his position, as was so eloquently stated <a href="http://friends.newtelligence.net/clemensv/CommentView,guid,0fbf07a9-9e7a-4db4-a305-58250ac57a73.aspx">here</a>, was against, while I was pro. The question he raised "To map, or not to map?" he himself answered, "to map". The question remaining was how to map; write the sql yourself, or let some tool write/generate it for you.
<br/><br/>
The overarching question is: what do you REALLY gain by O/R mapping?
<br/><br/>
(As an aside, among the comments of his post are those using the acronym ORM. Please stop - that acronym is already taken by Object Role Modeling.)
<br/><br/>
I've been using O/R mapping techniques on mission critical projects for some time now, and if I wanted to compare it to what I did before, it would not be to writing all the sql by hand. I don't remember ever doing that - there was always code generation involved. Because, let's face it - there's a lot of drudgery involved for things that aren't performance critical. No reason to do THAT by hand.
<br/><br/>
So, for me, the ONE THING that O/R mapping gives me better than what I did before is this:
<br/><br/>
O/R mapping gets me better object-oriented business logic.
<br/><br/>
That's it.
<br/><br/>
Like Clemens said, if you "don't know SQL and RDBMS technology in any reasonable depth", don't expect to get good performance. Obviously this is true for any technique. But I guess that empirically speaking, the percentage of people without said knowledge is larger in the group where you don't HAVE to write sql.
<br/><br/>
So, I'll bet you're asking yourself, "if that's all Udi gets from O/R mapping, why does he keep doing it?" Or maybe you're asking yourself "should I get a beer? This is getting long..."
<br/><br/>
The fact of the matter is that I don't know a better way to write business logic than by using OO techniques. I grant that data is important, but the reason that many applications are built is business logic - there's something that this new system can/should do, that the old systems couldn't (often using the same data).
<br/><br/>
If I could sum up my understanding of Clemens position, it would be this: 
<br/><br/>
A lot of developers probably aren't experienced, or knowledgeable enough the use O/R mapping well. Therefore writing the sql yourself is better.
<br/><br/>
While I agree with the first statement, and I think that the same could be said about communication, threading, .net and many other things, I don't think that the conclusion logically follows.
<br/><br/>
So, I guess I would sum up my position like this:
<br/><br/>
If you would like to develop a persistent domain model, O/R mapping techniques will probably help you. If you would like your solution to perform well, you should probably learn how databases work, as well as what the O/R mapping tool does under the covers.]]></description>
			<content:encoded><![CDATA[<p>I had this discussion with Clemens when he was last here in Israel &#8211; his position, as was so eloquently stated <a href="http://friends.newtelligence.net/clemensv/CommentView,guid,0fbf07a9-9e7a-4db4-a305-58250ac57a73.aspx">here</a>, was against, while I was pro. The question he raised &#8220;To map, or not to map?&#8221; he himself answered, &#8220;to map&#8221;. The question remaining was how to map; write the sql yourself, or let some tool write/generate it for you.</p>
<p>The overarching question is: what do you REALLY gain by O/R mapping?</p>
<p>(As an aside, among the comments of his post are those using the acronym ORM. Please stop &#8211; that acronym is already taken by Object Role Modeling.)</p>
<p>I&#8217;ve been using O/R mapping techniques on mission critical projects for some time now, and if I wanted to compare it to what I did before, it would not be to writing all the sql by hand. I don&#8217;t remember ever doing that &#8211; there was always code generation involved. Because, let&#8217;s face it &#8211; there&#8217;s a lot of drudgery involved for things that aren&#8217;t performance critical. No reason to do THAT by hand.</p>
<p>So, for me, the ONE THING that O/R mapping gives me better than what I did before is this:</p>
<p>O/R mapping gets me better object-oriented business logic.</p>
<p>That&#8217;s it.</p>
<p>Like Clemens said, if you &#8220;don&#8217;t know SQL and RDBMS technology in any reasonable depth&#8221;, don&#8217;t expect to get good performance. Obviously this is true for any technique. But I guess that empirically speaking, the percentage of people without said knowledge is larger in the group where you don&#8217;t HAVE to write sql.</p>
<p>So, I&#8217;ll bet you&#8217;re asking yourself, &#8220;if that&#8217;s all Udi gets from O/R mapping, why does he keep doing it?&#8221; Or maybe you&#8217;re asking yourself &#8220;should I get a beer? This is getting long&#8230;&#8221;</p>
<p>The fact of the matter is that I don&#8217;t know a better way to write business logic than by using OO techniques. I grant that data is important, but the reason that many applications are built is business logic &#8211; there&#8217;s something that this new system can/should do, that the old systems couldn&#8217;t (often using the same data).</p>
<p>If I could sum up my understanding of Clemens position, it would be this: </p>
<p>A lot of developers probably aren&#8217;t experienced, or knowledgeable enough the use O/R mapping well. Therefore writing the sql yourself is better.</p>
<p>While I agree with the first statement, and I think that the same could be said about communication, threading, .net and many other things, I don&#8217;t think that the conclusion logically follows.</p>
<p>So, I guess I would sum up my position like this:</p>
<p>If you would like to develop a persistent domain model, O/R mapping techniques will probably help you. If you would like your solution to perform well, you should probably learn how databases work, as well as what the O/R mapping tool does under the covers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2006/03/11/to-map-or-not-to-map/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SOA, mediation, and the holy code</title>
		<link>http://www.udidahan.com/2005/09/27/soa-mediation-and-the-holy-code/</link>
		<comments>http://www.udidahan.com/2005/09/27/soa-mediation-and-the-holy-code/#comments</comments>
		<pubDate>Tue, 27 Sep 2005 12:57:55 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://wp_630.weblogs.us/archives/223</guid>
		<description><![CDATA[Ronan Bradley of www.WebServices.org predicts the failure of SOA solutions based on &#8220;coded frameworks&#8221;. To say I disagree would be an understatement. There is one section in particular that disturbs me:
&#8220;My aversion to code is not based an abstract principle. Rather, each line of code the organization develops is custom for that organization and (unfortunately) [...]]]></description>
			<content:encoded><![CDATA[<p>Ronan Bradley of www.WebServices.org <a href="http://www.webservices.org/ws/content/view/full/72499">predicts the failure</a> of SOA solutions based on &#8220;coded frameworks&#8221;. To say I disagree would be an understatement. There is one section in particular that disturbs me:</p>
<p>&#8220;My aversion to code is not based an abstract principle. Rather, each line of code the organization develops is custom for that organization and (unfortunately) sometimes for that project. As a consequence it must be maintained and evolved over time. Furthermore, the knowledge about what that code does must be maintained in the organization and evolved over time. This is a potentially huge cost and distraction for IT organizations, which need to focus on solving business problems rather than maintaining integration code.&#8221;</p>
<p>1. Custom code is not necessarily integration code.<br />
2. Some business problems are about getting access to legacy data stuck in some proprietary format.<br />
3. IT maintains and develops code. That&#8217;s why it exists.</p>
<p>I worry that people will start thinking of SOA as &#8220;buy product from vendor, drag and drop, done&#8221;. Developers will always be writing code.</p>
<p>Let&#8217;s go back to the pipe-dreams of business-rule systems. You know, those hyper-flexible monstrosities that end-users (of all people) would define the rules.</p>
<p>Why don&#8217;t we, as an industry, JUST GET OVER IT.</p>
<p>There will always be code, as in custom code. Code without a solid design is a maintainence nightmare. Design without architecture is the right solution to the wrong problem. Architecture without requirements is answering the right question asked by the wrong person.</p>
<p>Nothing has changed.</p>
<p>Developing software will continue to be hard.</p>
<p>And no matter how many three letter acronyms you throw at it, software development will continue to be hard. If you don&#8217;t look at the whole lifecycle, you&#8217;re bound to optimize the small, and ruin the large.</p>
<p>Are we done with the hype yet?</p>
<p>Can we all just get back to work?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2005/09/27/soa-mediation-and-the-holy-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Architecture, Business Rules, and Aspects, oh my !</title>
		<link>http://www.udidahan.com/2003/11/14/architecture-business-rules-and-aspects-oh-my/</link>
		<comments>http://www.udidahan.com/2003/11/14/architecture-business-rules-and-aspects-oh-my/#comments</comments>
		<pubDate>Fri, 14 Nov 2003 19:07:42 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[AOP]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>

		<guid isPermaLink="false">http://wp_630.weblogs.us/archives/8</guid>
		<description><![CDATA[<p>I've been reading a lot from the <a href="http://www.agilemanagement.net/Articles/Weblog/blog.html">agile management</a> blog lately, and I came upon <a href="http://www.agilemanagement.net/Articles/Weblog/BusinessRulesinAspects.html">this entry</a> about implementing business rules with aspects. If you haven't yet read <a href="http://www.agilemanagement.net/Articles/Weblog/FeaturedBlogEntries/BusinessRulesObjectModeli.html">this previous entry</a> about the division of the system architecture not only along system lines, but also along "risk of change" lines, then I suggest you read it first.</p>

<p>First of all, I would like both developers and managers to read the previous entry because of its profound meaning. We CAN and should separate different parts of the system having differing levels of risk. Once again, <a href="http://udidahan.weblogs.us/archives/000374.html">"3-tier architectures"</a> don't address these issues.</p>


<p>So, if we are dividing the architecture along risk lines as well, then obviously the UI would be the riskiest - having the highest probability to change - over the life of the project. However, business rules are often considered quite stable. Why ? Because their time-line of change is often stretched out over many project life-times - rather obvious really, business rules deal with the business, not the project. During development of the project we are often given the requirements in such a way that it is difficult to know what is domain knowledge and what is a business rule. It takes effort to separate out what are the business rules - what has changed in the past, and what will change in the future.</p>

<p>For example, in an academic project I performed some time ago, I was given a set of requirements including:</p>


<p>
1. A student can register to a given course once in a semester.<BR>
2. A student can register to a project in a semester.<BR>
</p>

<p>Which is the business rule, which is the domain ? In this case (1) is the domain, and (2) is the rule - "a project" meaning "only one project" as I later found out. This rule changed sometime after my second alpha to "A student can register to only one project <u>alone</u> in a semester, but several projects in the same semester as long as they have a partner for each of those projects."</p>


<p>As it is apparent, I, as a developer, have to take into account these risk/change factors and change my architecture accordingly. Ever since that project I have always created a business rules layer separate from the infamous "BL" ( Business Logic ). </p>


<p>Now, getting to the issue of implemeting using aspects. I am a big proponent of AOP, however I often find alternative implementations to be more desirable. Most of the examples given for the use of AOP including logging, transactions, security and others that can be made part of a framework, as .Net has done in many cases. Many have pushed Java's superiority because of AspectJ, and although .Net doesn't have an Aspect.Net yet ( although various developments are under way ), I have yet to miss it. <a href="http://staff.newtelligence.net/clemensv/Default.aspx">Clemens Vasters</a> has done some truly incredible stuff in the use of attributes in .Net for implementing AOP stuff. I've always thought that attributes should be used that way.</p>


<p>I find that there is one basic flaw in the conclusion to using aspects for connecting business rules to systems. The entire white paper is <a href="http://ssel.vub.ac.be/Members/Maja/papers/Cibran-BIS03.pdf">here</a>.  ( Yes, its a pdf unfortunately - only good for print really, <a href="http://www.useit.com/alertbox/20030714.html">Jakob Nielsen thinks so too</a>. ) The basic premise is that I'm building an entire system in an OO manner, which I've stopped doing some time ago for <a href="http://udidahan.weblogs.us/archives/000374.html">these</a> reasons. Clemens has great insights on this as well, see them <a href="http://staff.newtelligence.net/clemensv/PermaLink.aspx?guid=6868ac5d-16cc-4863-bde4-11f663691c76">here</a>.</p>


<p>When using an SOA, your UI, or any other system needing services for that matter, will be sending messages to the guts of your system - the "BL" for you hardcore 3-tier-guys. Let's call the thing receiving messages in this case the Gateway. All the gateway does is receive messages ( like "Register student number 12 to course number 15 in semester 21" ) and pass them on to the appropriate handler. The word "handler" hear is used like in the term "event handler" for a reason: </p>


<p><u><b>The receipt of a message is an event.</b></u></p>


<p>In the white-paper the authors refer to these events as a problem that has to be dealt with. Why ? Because when working in an object oriented fashion, you would have to intercept the call to:</p>


<p>new Student(12).RegisterToProject(93);</p>


<p>in order to handle the event, ie check/activate a business rule. Aspects are great for this sort of thing. However, when working in a service-oriented fashion, you would send a message of type "RegisterStudentToProject" with the parameters StudentID and ProjectID as above. No need to intercept any call since it has to first go through the gateway. The gateway would then pass the message to the business rules engine which would then find and activate the appropriate rules before and after the actual call to register the student. The rules engine does something like this:</p>


<p>
If ( ActivateBusinessRulesForMessageAndReturnTrueIfCanMakeCall(myMessage) )<BR>
{<BR>
&#160;&#160;MakeCallForMessage(myMessage);<BR>
&#160;&#160;ActivateBusinessRulesAfterMessage(myMessage);<BR>
}</p>


<p>The business rules themeselves are implemented in a separate layer than the engine. The mapping between rules and messages is also done in a layer separate from both the engine and the rules. Once we have a layer for each of these, we have architecturally separated the parts that change more often in the system from the rest of it.</p>

<p>One can also move to a more dynamic model. One in which you define a language for defining rules, and the mapping to messages as well. Thus, changes could be made by changing a configuration file instead of recompiling any part of the system.</p>

<p>Note that when you have lots of rules and the order for activating them matters, you should move to a commercial rule engine instead of implementing your own. You'll see that performance becomes an issue as the number of rules increases.</p>

<p>I hope that I've managed to introduce yet another strength of the SOA over the pure OO paradigm.</p>

<p>Tell me what you think !</p>

<p>Where does the SOA fall short ? <BR>
Where does the OOA beat the SOA ?<BR>
Am I full of it ?</p>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been reading a lot from the <A href="http://www.agilemanagement.net/Articles/Weblog/blog.html">agile management</A> blog lately, and I came upon <A href="http://www.agilemanagement.net/Articles/Weblog/BusinessRulesinAspects.html">this entry</A> about implementing business rules with aspects. If you haven&#8217;t yet read <A href="http://www.agilemanagement.net/Articles/Weblog/FeaturedBlogEntries/BusinessRulesObjectModeli.html">this previous entry</A> about the division of the system architecture not only along system lines, but also along &#8220;risk of change&#8221; lines, then I suggest you read it first. </p>
<p>First of all, I would like both developers and managers to read the previous entry because of its profound meaning. We CAN and should separate different parts of the system having differing levels of risk. Once again, <A href="http://udidahan.weblogs.us/archives/000374.html">&#8220;3-tier architectures&#8221;</A> don&#8217;t address these issues. So, if we are dividing the architecture along risk lines as well, then obviously the UI would be the riskiest &#8211; having the highest probability to change &#8211; over the life of the project. </p>
<p>However, business rules are often considered quite stable. Why ? Because their time-line of change is often stretched out over many project life-times &#8211; rather obvious really, business rules deal with the business, not the project. During development of the project we are often given the requirements in such a way that it is difficult to know what is domain knowledge and what is a business rule. It takes effort to separate out what are the business rules &#8211; what has changed in the past, and what will change in the future. </p>
<p>For example, in an academic project I performed some time ago, I was given a set of requirements including: 1. A student can register to a given course once in a semester. 2. A student can register to a project in a semester. Which is the business rule, which is the domain ? In this case (1) is the domain, and (2) is the rule &#8211; &#8220;a project&#8221; meaning &#8220;only one project&#8221; as I later found out. This rule changed sometime after my second alpha to &#8220;A student can register to only one project <U>alone</U> in a semester, but several projects in the same semester as long as they have a partner for each of those projects.&#8221; </p>
<p>As it is apparent, I, as a developer, have to take into account these risk/change factors and change my architecture accordingly. Ever since that project I have always created a business rules layer separate from the infamous &#8220;BL&#8221; ( Business Logic ). </p>
<p>Now, getting to the issue of implemeting using aspects. I am a big proponent of AOP, however I often find alternative implementations to be more desirable. Most of the examples given for the use of AOP including logging, transactions, security and others that can be made part of a framework, as .Net has done in many cases. Many have pushed Java&#8217;s superiority because of AspectJ, and although .Net doesn&#8217;t have an Aspect.Net yet ( although various developments are under way ), I have yet to miss it. <A href="http://staff.newtelligence.net/clemensv/Default.aspx">Clemens Vasters</A> has done some truly incredible stuff in the use of attributes in .Net for implementing AOP stuff. I&#8217;ve always thought that attributes should be used that way. I find that there is one basic flaw in the conclusion to using aspects for connecting business rules to systems. The entire white paper is <A href="http://ssel.vub.ac.be/Members/Maja/papers/Cibran-BIS03.pdf">here</A>. ( Yes, its a pdf unfortunately &#8211; only good for print really, <A href="http://www.useit.com/alertbox/20030714.html">Jakob Nielsen thinks so too</A>. ) The basic premise is that I&#8217;m building an entire system in an OO manner, which I&#8217;ve stopped doing some time ago for <A href="http://udidahan.weblogs.us/2003/11/08/simple/">these</A> reasons. Clemens has great insights on this as well, see them <A href="http://staff.newtelligence.net/clemensv/PermaLink.aspx?guid=6868ac5d-16cc-4863-bde4-11f663691c76">here</A>. </p>
<p>When using an SOA, your UI, or any other system needing services for that matter, will be sending messages to the guts of your system &#8211; the &#8220;BL&#8221; for you hardcore 3-tier-guys. Let&#8217;s call the thing receiving messages in this case the Gateway. All the gateway does is receive messages ( like &#8220;Register student number 12 to course number 15 in semester 21&#8243; ) and pass them on to the appropriate handler. The word &#8220;handler&#8221; hear is used like in the term &#8220;event handler&#8221; for a reason: <U><B>The receipt of a message is an event.</B></U> In the white-paper the authors refer to these events as a problem that has to be dealt with. Why ? Because when working in an object oriented fashion, you would have to intercept the call to: new Student(12).RegisterToProject(93); in order to handle the event, ie check/activate a business rule. Aspects are great for this sort of thing. However, when working in a service-oriented fashion, you would send a message of type &#8220;RegisterStudentToProject&#8221; with the parameters StudentID and ProjectID as above. No need to intercept any call since it has to first go through the gateway. The gateway would then pass the message to the business rules engine which would then find and activate the appropriate rules before and after the actual call to register the student. </p>
<p>The rules engine does something like this: </p>
<p>If ( ActivateBusinessRulesForMessageAndReturnTrueIfCanMakeCall(myMessage) )<br />
{<br />
  MakeCallForMessage(myMessage);<br />
  ActivateBusinessRulesAfterMessage(myMessage);<br />
} </p>
<p>The business rules themeselves are implemented in a separate layer than the engine. The mapping between rules and messages is also done in a layer separate from both the engine and the rules. Once we have a layer for each of these, we have architecturally separated the parts that change more often in the system from the rest of it. One can also move to a more dynamic model. One in which you define a language for defining rules, and the mapping to messages as well. Thus, changes could be made by changing a configuration file instead of recompiling any part of the system. </p>
<p>Note that when you have lots of rules and the order for activating them matters, you should move to a commercial rule engine instead of implementing your own. You&#8217;ll see that performance becomes an issue as the number of rules increases. </p>
<p>I hope that I&#8217;ve managed to introduce yet another strength of the SOA over the pure OO paradigm. Tell me what you think ! Where does the SOA fall short ? Where does the OOA beat the SOA ? Am I full of it ?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2003/11/14/architecture-business-rules-and-aspects-oh-my/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
