<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jak Charlton - Insane World : Architecture</title><link>http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx</link><description>Tags: Architecture</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>What is Domain Driven Design?</title><link>http://devlicio.us/blogs/casey/archive/2011/05/16/what-is-domain-driven-design.aspx</link><pubDate>Mon, 16 May 2011 02:15:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:67301</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=67301</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=67301</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2011/05/16/what-is-domain-driven-design.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span&gt;&amp;quot;... the key to expert performance in many fields
is domain knowledge rather than intelligence.&amp;quot; &lt;br /&gt;
&lt;/span&gt;&lt;span&gt;&lt;i&gt;Don Reinertsen&lt;/i&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Domain Driven Design
is a software development methodology, intended to achieve a software system
closely modelled on and aligned with real business processes.&lt;/p&gt;
&lt;p&gt;Traditionally
development tends to be a technically led process, where requirements are
passed from the business to the development teams, and then the development
teams go off and produce their best guesstimate at what the requirements said.&lt;/p&gt;
&lt;p&gt;In a waterfall
approach this leads to large requirements documents that are diligently
collated, analysed, reviewed and approved. These documents are then given to
development teams to turn into working software.&lt;/p&gt;
&lt;p&gt;Agile approaches can
also accept the requirements documents that waterfall tends to produce, but for
them to actually progress forwards they must be broken to small tasks and
stories which can then be queued ready for development.&lt;/p&gt;
&lt;p&gt;Domain Driven Design
to a large degree steps back from these two distinct ends of the spectrum, and
looks at how the requirements are gathered in the first place - if you like, it
bridges the gap between doing everything up front and everything at the last
minute.&lt;/p&gt;
&lt;p&gt;DDD understands that
requirements are never &amp;#39;done&amp;#39;, but exist as a living document. More
importantly, the living document in question is actually the software itself -
all other documents are an artefact and reflection of the code.&lt;/p&gt;
&lt;p&gt;As the software
system develops and grows, deeper understanding of the problem grows too - DDD
is all about the discovery of the solution through deeper understanding of the
problem.&lt;/p&gt;
&lt;p&gt;Where DDD really
differs though is that it sees the software system as a reflection of the
business process, an enabler rather than the driver. DDD is deeply concerned
about the business processes, business terminology and practices. Technical
concerns are largely secondary and a means to an end.&lt;/p&gt;
&lt;p&gt;The &lt;a href="http://thinkddd.com/blog/2009/02/09/the-ubiquitous-language/"&gt;Ubiquitous Language&lt;/a&gt; is at the centre of DDD - this is a shared and growing language. A
negotiated language derived from the business terminology and enriched by the development teams. If a business person doesn&amp;#39;t understand a term in the UL,
then the chances are the UL needs an evolution. If a technical person doesn&amp;#39;t
understand a term in the UL then the chances are they need a conversation with
a Domain Expert.&lt;/p&gt;
&lt;p&gt;The Domain Experts
are the second essential component of DDD - people who deeply understand the
business domain, and the business itself. These people are integral to the
development process. They may not be required &amp;quot;full time&amp;quot; like the
traditional Product Owners of some Agile methodologies, but they must be
accessible continually, and be prepared to invest time in the process. Domain
Experts are not to be considered outsiders, but as the core of the DDD process
- they are as much a part of the development team as any developer or tester.&lt;/p&gt;
&lt;p&gt;DDD doesn&amp;#39;t begin
and end - it is a constant process of re-evaluation, refactoring, remodelling
and redesign - every conversation should bring you to a closer understanding of
the problem. At no point is DDD &amp;#39;done&amp;#39; - it is always there; the Ubiquitous Language
grows and develops, the Domain Models change as understanding changes, code is
restructured and refactored to better represent that understanding.&lt;/p&gt;
&lt;p&gt;Artefacts will come
and go, but only one really matters - the code base. It is the only expression
of the solution that is free of abstraction. While documents may try to explain
or describe the system, only the code does so without losing information. This
means that in DDD the code must remain of high quality, be clear and
expressive, be free of technical abbreviations or jargon, and as far as
possible should make at least some sense to a Domain Expert when explained to
them.&lt;/p&gt;
&lt;p&gt;Clever code doesn&amp;#39;t
work in DDD, nor do magical processes or &amp;quot;you don&amp;#39;t need to know&amp;quot;
components. Domain Experts shouldn&amp;#39;t need to become developers to understand
what the key parts of the software system is doing for them.&lt;span&gt;&amp;nbsp; &lt;/span&gt;They also shouldn&amp;#39;t need to think in terms of
databases or batch jobs or other technical concerns.&lt;/p&gt;
&lt;p&gt;DDD is the ultimate
expression of Agile - it is about dealing with a constantly shifting and
evolving set of requirements - because as anyone who has ever been involved
with a software project knows - it is very rare for a requirement to remain
intact from the beginning to the end of a project, and almost impossible for it
to do so as the business grows and changes.&lt;/p&gt;
&lt;p&gt;Through constant
conversations, DDD will guide you to an ever more accurate software
representation of your business process.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=67301" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Rants/default.aspx">Rants</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Domain+Driven+Design/default.aspx">Domain Driven Design</category></item><item><title>My Objective Today is to Make You Think</title><link>http://devlicio.us/blogs/casey/archive/2010/07/19/my-objective-today-is-to-make-you-think.aspx</link><pubDate>Mon, 19 Jul 2010 00:08:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:61124</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=61124</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=61124</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2010/07/19/my-objective-today-is-to-make-you-think.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&amp;quot;Maybe There is a Better Way&amp;quot;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I recently presented at DeveloperDeveloperDeveloper in Sydney, and although my talk was Stuff About CQRS, I opened with the slide&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;My Object Today Is to Make You Think ... &amp;#39;Maybe There is a Better Way&amp;#39;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;(&lt;a href="http://thinkddd.com/blog/2010/07/17/dddsydney-presentation-stuff-about-cqrs/"&gt;slides here&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The real focus of this was around how normal people think, and how unlike normal people we developers really are. My role in development is all about enabling better communication, because fundamentally I believe the real value a developer brings to a project is not technical, but is in the way they interact with the team, and more importantly with the normal people they are actually creating software for.&lt;/p&gt;
&lt;p&gt;Obviously some of this has roots in DDD, the Ubiquitous Language is obviously an attempt to traverse this chasm that seems to exist between us.&lt;/p&gt;
&lt;p&gt;Then I touched on user interfaces, and how they are so rarely designed the way people think - normal people think about their objectives and goals, not in terms of data like we developers do. Inductive UIs are focused on tasks, unlike the traditional data driven UIs that we tend to throw at users - users and people don&amp;#39;t think in grids and columns and rows, they think &amp;quot;I want to change my address&amp;quot;, not &amp;quot;open my customer record, edit the three fields under address and save to the database&amp;quot; - only a sadist or a developer would think that way.&lt;/p&gt;
&lt;p&gt;And finally I touched on things like NoSQL databases, which neatly solve a communication problem - they stop us thinking about How to store information and let us focus on What we are storing and Why.&lt;/p&gt;
&lt;p&gt;And lastly I tried to show the link between CQRS and these business problems - how it made you focus on the language, on tasks and objectives and how it let you detach the How from the What and Why.&lt;/p&gt;
&lt;p&gt;But most importantly, what I was trying to do in that presentation was to throw some non-mainstream ideas out into the audience, to spark discussion and debate, and to get people to think - Maybe There is a Better Way &amp;nbsp;&lt;/p&gt;
&lt;p&gt;If only a few of those in the audience went away and Googled some of the ideas I was talking about, it will be another step towards moving development away from it&amp;#39;s heavy focus on technology and technological solutions - and towards a people and business driven focus, where technology is an artifact, not the deciding factor.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=61124" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Rants/default.aspx">Rants</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category><category domain="http://devlicio.us/blogs/casey/archive/tags/cqrs/default.aspx">cqrs</category></item><item><title>Agile is Performance, Feedback, Revision</title><link>http://devlicio.us/blogs/casey/archive/2010/04/15/agile-is-performance-feedback-revision.aspx</link><pubDate>Thu, 15 Apr 2010 02:45:08 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:57265</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=57265</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=57265</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2010/04/15/agile-is-performance-feedback-revision.aspx#comments</comments><description>&lt;p&gt;I came across a great video link yesterday, entitled “&lt;a href="http://blog.newhumanist.org.uk/2010/03/see-baba-brinkmans-rap-guide-to.html" target="_blank"&gt;Performance, Feedback, Revision&lt;/a&gt;”. It’s a Canadian rapper named Baba Brinkman covering the theory of evolution and the work of Charles Darwin, and he equates evolution with how he writes his lyrics, Performance, Feedback, Revision.&lt;/p&gt;  &lt;p&gt;As I listened to it, it struck me that this is Agile … we develop, we run retrospectives, and we revise what we have.&lt;/p&gt;  &lt;p&gt;Evolution may be more like waterfall, and take millions of years to evolve something as fundamentally pointless as the &lt;a href="http://www.talkorigins.org/faqs/vestiges/appendix.html" target="_blank"&gt;appendix&lt;/a&gt;, and in Agile we may look to have Performance, Feedback and Revision inside a week or two.&lt;/p&gt;  &lt;p&gt;I can’t quite believe I just showed this video to a client to explain Agile, but it seemed to get the idea across pretty well. I’m considering using it more often, but I promise to draw the line at learning to rap.&lt;/p&gt;  &lt;p&gt;But from now on, in big letters across the top of my white boards will go the words:&lt;/p&gt;  &lt;h3 align="center"&gt;Performance, Feedback, Revision&lt;/h3&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=57265" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Agile/default.aspx">Agile</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Rants/default.aspx">Rants</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category></item><item><title>Solving Complexity with Message Based Architectures </title><link>http://devlicio.us/blogs/casey/archive/2010/02/04/solving-complexity-with-message-based-architectures.aspx</link><pubDate>Thu, 04 Feb 2010 07:55:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:55264</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=55264</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=55264</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2010/02/04/solving-complexity-with-message-based-architectures.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoPlainText"&gt;Ultimately most complexity in software comes not from the
requirements, the business logic, or even the underlying systems. Most
complexity comes out of a poorly considered and managed architecture, and this
is commonly seen in tightly coupled systems that rapidly degrade into Big Balls
of Mud.&lt;/p&gt;
&lt;p class="MsoPlainText"&gt;The key to simplifying architectures is to decouple their
component parts, making each more specialised and less dependent on the
others. This allows every component to focus on a specific task without risk of
those tasks becoming compromised by, or interfering with, others.&lt;/p&gt;
&lt;p class="MsoPlainText"&gt;The basic premise of most architectures that succeed in
this way is that they are message based, rather than being glorified CRUD
systems pulling data to and from databases. Operations are central to the
system, data is ultimately just the storage for the results of those
operations.&lt;/p&gt;
&lt;p class="MsoPlainText"&gt;This separation of concerns allows development teams to
concentrate on component parts with little impact upon other teams, and no more
reliance upon them than an agreement on the messages they will listen for and
those they will publish. While messages share some characteristics of service
interfaces, they differ mostly in their granularity and lack of dependence upon
one another. Messages allow true decoupling, where traditional service layers
only promise this and usually end up creating even more tightly coupled
systems.&lt;/p&gt;
&lt;p class="MsoPlainText"&gt;&amp;quot;Message based&amp;quot; does not inherently mean SOA, and in many
ways differs substantially. Although both can be considered message based, SOA
brings significant overhead and generalisation, along with an organisation wide
remit. Simple message based systems operate on the basis of message within the application
rather than across an enterprise.&lt;/p&gt;
&lt;p class="MsoPlainText"&gt;Poor software architecture ends up impacting more than
just the software system under development, and frequently the impact can be
felt across an organisation; in the way development teams approach problems, in
the way they respond to new requirements, and throughout the SDLC.&lt;/p&gt;
&lt;p class="MsoPlainText"&gt;More often than not, the SDLC is a reflection of the weaknesses
in software architecture, and is frequently trying to play &amp;#39;catch up&amp;#39; to attempt
to deal with the issues weak architecture brings. These compromises and aspects
of the SDLC then start to propagate and impact other areas of the development and
support process. Often decisions about software architecture become the underlying
drivers for the entire IT department, and the SDLC turns into a point of
conflict as it becomes more about trying to restrain and control a big ball of
mud than it does about providing a framework for providing business value.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=55264" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category><category domain="http://devlicio.us/blogs/casey/archive/tags/SOA/default.aspx">SOA</category></item><item><title>What Is The Difference Between an IoC Container and MEF?</title><link>http://devlicio.us/blogs/casey/archive/2009/12/18/what-is-the-difference-between-an-ioc-container-and-mef.aspx</link><pubDate>Fri, 18 Dec 2009 06:50:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:54625</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=54625</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=54625</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/12/18/what-is-the-difference-between-an-ioc-container-and-mef.aspx#comments</comments><description>&lt;p&gt;Today a conversation sparked off on Twitter, started by &lt;a target="_blank" href="http://www.twitter.com/jbogard"&gt;Jimmy Bogard&lt;/a&gt; and &lt;a target="_blank" href="http://www.twitter.com/mhinze"&gt;Matt Hinze&lt;/a&gt;, and then carried on by myself and &lt;a target="_blank" href="http://www.twitter.com/gblock"&gt;Glenn Block&lt;/a&gt;. The basic starting point was what the difference was between using an IoC container like &lt;a target="_blank" href="http://www.castleproject.org/container/index.html"&gt;Windsor&lt;/a&gt; or &lt;a target="_blank" href="http://structuremap.sourceforge.net/Default.htm"&gt;StructureMap&lt;/a&gt; and using MEF (the &lt;a target="_blank" href="http://www.codeplex.com/MEF"&gt;Managed Extensibility Framework&lt;/a&gt;)   &lt;/p&gt;
&lt;p&gt;I started off by answering a question from Jimmy, he wanted the one paragraph sales pitch for MEF that didn&amp;rsquo;t include technical terminology &amp;hellip; I guess he was asking why MEF is a better option than one of the IoC containers we all love and hopefully all use religiously.&lt;/p&gt;
&lt;p&gt;My key answers were:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;MEF allows you to write your application as a series of much simpler mini applications, and have those brought together to create a much great whole&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;IoC container = service level components, MEF = Autonomous Components. A MEF &amp;quot;part&amp;quot; will probably use an IoC container&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This sparked some comments from Glenn where he wanted to clarify that sometimes an IoC container is the right choice, sometimes MEF is, and sometimes both are. Luckily we are both pragmatic, so not much to disagree with there. He pointed to two of his blogs on the subject, I recommend reading them highly, they will do a much better job than I could at explaining the detail of the decision points, &lt;a target="_blank" href="http://codebetter.com/blogs/glenn.block/archive/2009/08/16/should-i-use-mef-for-my-general-ioc-needs.aspx"&gt;Should I use MEF for my general IoC needs?&lt;/a&gt; and &lt;a target="_blank" href="http://codebetter.com/blogs/glenn.block/archive/2009/10/31/should-i-use-mef-with-an-ioc-container.aspx"&gt;Should I use MEF with an IoC container?&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;What&amp;rsquo;s Wrong With IoC Containers?&lt;/h3&gt;
&lt;p&gt;What an IoC container is very good at is low level control of your code, your dependency graphs, your lifestyle management, and gives you great low level ways to deal with facilities, factories, decorators and AOP. In just about any code base that goes past spike stage, I use an IoC container of some sort, it is just a fools errand to try and write and application without one these days. The keyword &amp;ldquo;new&amp;rdquo; is in my code smells book.&lt;/p&gt;
&lt;p&gt;However, what an IoC container is pretty poor at is enforcing boundaries. Sure you could use an IoC container to write much larger applications, but as Glenn rightly pointed out, the central configuration becomes a headache. In fact, the central registration can rapidly become the equivalent of using global variables, you never really know what is using what as everything has access to everything else.&lt;/p&gt;
&lt;p&gt;Most of the containers give you the capability to have child containers to try and deal with this situation, and while they do limit the global access issue, they are pretty hard to manage in their own right, and you can without some proper thought end up with a spaghetti mess of containers replacing your spaghetti mess of components.&lt;/p&gt;
&lt;h3&gt;An Architectural Solution To The Problem&lt;/h3&gt;
&lt;p&gt;To me, MEF is an architectural solution, while an IoC container is a code level solution. They both have their places in an application, but what MEF is exceptionally good at is creating process boundaries.&lt;/p&gt;
&lt;p&gt;One of the core underlying principles of good use of an IoC container is that you only ever reference a container at a process boundary &amp;ndash; in most applications that means it is probably referenced in your bootstrapping code inside your web application (maybe Global.ascx), or inside an HttpModule around your web services, but from that point on, your code should never reference the container again &amp;ndash; the magic of an IoC container comes from relinquishing all object construction and dependency graph resolution to it.&lt;/p&gt;
&lt;p&gt;But this means in a larger application, you may end up with massive containers with hundreds of registered components, and in reality most operations will only use a small handful at a time. &lt;/p&gt;
&lt;p&gt;In most larger systems you would naturally have parts of the system you want to devolve into discrete components, or Autonomous Components. Doing this with an IoC container starts to lead you down an architectural design geared around the limitations of the IoC container, and not necessarily driven by good architectural principles.&lt;/p&gt;
&lt;p&gt;What MEF allows you to do is to break the system apart easily, or as Glenn referred to it, MEF allows you to easily create composite applications. These composite &lt;strong&gt;parts&lt;/strong&gt; can operate as Autonomous Components, in almost total isolation from the core application, and therefore they provide a natural boundary without having to use things like web of WCF service layers just for that purpose. Oddly the MEF terminology that Jimmy wanted to avoid in his original question was &amp;ldquo;part&amp;rdquo; and yet it fits naturally in this context.&lt;/p&gt;
&lt;h3&gt;Russian Dolls&lt;/h3&gt;
&lt;p&gt;As I started off by saying, I would almost always end up using an IoC container in any application I was to write, even a fairly small scale one. If we look at these MEF parts as now being Autonomous Components, and therefore operating as mini applications, it now becomes apparent that they will themselves need a way to create service components and dependency graphs, and while MEF could in theory do a lot of that, it&amp;rsquo;s just not the best fit for the job.&lt;/p&gt;
&lt;p&gt;So it&amp;rsquo;s pretty certain that inside your MEF part you will be using yet another IoC container for object resolution, but now you don&amp;rsquo;t have a massive central registry, but one specifically geared to the MEF part in question.&lt;/p&gt;
&lt;p&gt;And this pattern can recurse if needed, each time providing a clean boundary within your application.&lt;/p&gt;
&lt;h3&gt;A Real World Example&lt;/h3&gt;
&lt;p&gt;The last &amp;ldquo;large scale&amp;rdquo; system I worked on where I had the opportunity to put some of these ideas into practice was an insurance portal. This portal was responsible for providing insurance quotations, but across a massively diverse and inconsistent set of products.&lt;/p&gt;
&lt;p&gt;The solution we came to in the end was a combination of all of the above principles &amp;ndash; we had a web application that held a Castle Windsor container in Global.ascx, that container was responsible for instantiating everything from controllers backwards, and all of the service components they used.&lt;/p&gt;
&lt;p&gt;But some of those controllers needed to call our product plugins, and in this case we actually used Castle Windsor to use MEF to instantiate the plugins (MEF parts). Within each product plugin we had another set of bootstrapping code that had it&amp;rsquo;s own instance of a Castle Windsor container, and it&amp;rsquo;s own registry specific to that plugin.&lt;/p&gt;
&lt;p&gt;The plugins were actually used by a number of points in the application, some in process, and some out of process, either on other servers or just behind service or service bus boundaries &amp;ndash; what MEF allowed us to do was create an independently deployable package that was self contained and self sufficient, without the need for managing masses of registration information &amp;ndash; trying to use an IoC container for this would have been a mini-nightmare, but with MEF it was almost a pleasure.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=54625" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/.NET/default.aspx">.NET</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://devlicio.us/blogs/casey/archive/tags/MEF/default.aspx">MEF</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Castle/default.aspx">Castle</category></item><item><title>We Are Not Doing DDD – Part Two - CQS</title><link>http://devlicio.us/blogs/casey/archive/2009/06/22/we-are-not-doing-ddd-part-two-cqs.aspx</link><pubDate>Mon, 22 Jun 2009 16:32:58 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:48438</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=48438</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=48438</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/06/22/we-are-not-doing-ddd-part-two-cqs.aspx#comments</comments><description>&lt;p&gt;From the beginning of my current project we have been working under some horrible constraints, many imposed by legacy systems, many by late decisions that would have speeded things immensely if made earlier, and many imposed by decisions that are outside of my control.&lt;/p&gt;  &lt;p&gt;This lead us early on to make decisions on architecture that eliminated any possibility of using DDD heavily, and as I mentioned in my original post about this project it is also essentially a glorified CRUD system - we read data from databases, we modify it a bit, and we put it back again.&lt;/p&gt;  &lt;h3&gt;What We Are Doing – Command Query Separation&lt;/h3&gt;  &lt;p&gt;The first architectural choice that I made, and in hindsight got accepted with relative ease, was to employ &lt;a href="http://dddstepbystep.com/wikis/ddd/blogged-command-query-separation-as-an-architectural-concept.aspx" target="_blank"&gt;CQS&lt;/a&gt; to simplify the system.&lt;/p&gt;  &lt;p&gt;Earlier projects had suffered quite badly from being abstractions of legacy systems with new functionality bolted on. As &lt;/p&gt;  &lt;p&gt;far as possible I wanted to eliminate this problem from our project, as I was all too aware that this was an easy trap for the team to fall into, and would burn a lot of development time very fast.&lt;/p&gt;  &lt;p&gt;To make the system simpler I chose to separate our query mechanisms from the &amp;quot;domain&amp;quot; type stuff, our commands and data writing/updating code.&lt;/p&gt;  &lt;p&gt;CQS is a pretty simple concept. One path through your system is responsible for querying of anything much more than &amp;quot;Get By ID&amp;quot; type calls. This side of our system is responsible for reading data from legacy systems, reading data from legacy web services, and for reading data from our application database.&lt;/p&gt;  &lt;h4&gt;Querying&lt;/h4&gt;  &lt;p&gt;The Query side deals largely in DataTables, DataSets and a very very limited number of DTOs. This data is largely and essentially used for display purposes, it is the contents of search results, the results of postcode lookups, the information to populate dropdown lists and tables. &lt;/p&gt;  &lt;p&gt;The Query side of CQS does not need strongly typed entities, nor does it require strongly typed DTOs - as it is largely ad-hoc data maintaining these entities and DTOs would consume a disproportionate amount of development time for something a DataTable can deal with more than adequately.&lt;/p&gt;  &lt;p&gt;Our Query side actually uses the Query Object pattern, most of which encapsulate a call to an Oracle DB, some of which wrap simple NHibernate criteria and a few of which sit around web services. These are fronted by a WCF facade, which allows us to put these queries out of process, and to cache them aggressively should be need.&lt;/p&gt;  &lt;p&gt;What we do not have in the system anywhere, and especially in the Query side, is any big entity model – we have two very small entity models (5 entities in total) used for very specific functions within our application.&lt;/p&gt;  &lt;h4&gt;Command&lt;/h4&gt;  &lt;p&gt;The Command side of the CQS equation is a little different. Firstly, it deals with anything that is essentially a request to &amp;quot;go do something&amp;quot;. In our case these are commands like &amp;quot;SaveDraft&amp;quot; and &amp;quot;CancelPolicy&amp;quot; &lt;/p&gt;  &lt;p&gt;The Command side can write data to the databases, the Query side is not allowed to.&lt;/p&gt;  &lt;p&gt;If we were doing DDD, this is where our Domain would sit - and in fact we have been referring to this as &amp;quot;the domain&amp;quot; for most of this project. But, we aren’t using DDD and we don&amp;#39;t really have a Domain Model. It is however where most stuff we could describe as &amp;quot;logic&amp;quot; lives. This is primarily a bunch of application services, most of which operate in response to messages fired over MSMQ using NServiceBus.&lt;/p&gt;  &lt;p&gt;This side of the architecture has a number of listeners that are responsible for pulling messages from various MSMQs and for dealing with the specific requests, for example pushing data back to our legacy systems, for logging, and eventually for notifications.&lt;/p&gt;  &lt;h4&gt;Benefits&lt;/h4&gt;  &lt;p&gt;Early on, the use of CQS paid for itself easily. By removing the complications of maintaining read and write models together, we quickly managed to get working prototype up and running, with no need for a &amp;quot;domain&amp;quot; or entity model up front.&lt;/p&gt;  &lt;p&gt;As the project has progressed, this decision has helped simplify decisions that otherwise would have involved structural or fundamental changes to the architecture – but more importantly, it has helped all of the developers think in terms of a command driven system, rather than viewing the system as CRUD over a database.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=48438" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category><category domain="http://devlicio.us/blogs/casey/archive/tags/CQS/default.aspx">CQS</category></item><item><title>Commercial Suicide - Integration at the Service Level</title><link>http://devlicio.us/blogs/casey/archive/2009/05/19/commercial-suicide-integration-at-the-service-level.aspx</link><pubDate>Tue, 19 May 2009 07:03:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:46913</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=46913</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=46913</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/05/19/commercial-suicide-integration-at-the-service-level.aspx#comments</comments><description>&lt;p&gt;In my previous post I described &lt;a href="http://devlicio.us/blogs/casey/archive/2009/05/14/commercial-suicide-integration-at-the-database-level.aspx"&gt;the problems with trying to integrate your organisation at the database level&lt;/a&gt;, and the fallacies surrounding the idea of the One True Authority Database. I also alluded to this being a problem with services too.&lt;/p&gt;
&lt;p&gt;When you try to create a monolithic and authoritarian database that knows all, sees all, and brings everything into a single coherrent whole, you are heading down a slippery slope. Different applications have different requirements, and even within the same job role, the meaning of Customer can have a totally different meaning in two different contexts. I also argued that data has no value without context, and the context is the application that is meeting the business requirements, be these user interaction, system interaction, or reporting.&lt;/p&gt;
&lt;h3&gt;SOA to the Rescue!&lt;/h3&gt;
&lt;p&gt;I&amp;#39;m not arguing without ample evidence behind me here - the idea of integration at a database level was one that was well discredited many years ago. I grant you many organisations haven&amp;#39;t got the message yet, and that many vested interests still keep trying to make the magic solution work, but fundamentally a long time back we in IT recognised that the One True Authority Database (OTADB)&amp;nbsp;was a &amp;quot;bad thing&amp;quot; - and this lead us to the promised land - initially Web Services and then on to Service Oriented Architectures.&lt;/p&gt;
&lt;p&gt;SOA is an attempt to resolve the OTADB issue - take all those database calls, and even calls to different databases, and unify them through a set of coherrent and cohesive services - SOA can provide us with the abstraction over out data stores that is required - and now we can have the One True Authority Service Layer.&lt;/p&gt;
&lt;p&gt;With SOA we can define services to wrap our databases, services to update data, service to read and query data, and even services to unify those &amp;quot;different&amp;quot; Customers into a&amp;nbsp;single coherrent and canonical data model.&lt;/p&gt;
&lt;p&gt;And that&amp;#39;s where it all goes wrong again.&lt;/p&gt;
&lt;h3&gt;Anti-Pattern: The&amp;nbsp;Canonical Data Model&lt;/h3&gt;
&lt;p&gt;Back when our database guys were trying to integrate all this information, what they were trying to achieve, but maybe didn&amp;#39;t realise, was a canonical data model - this is the &amp;quot;holy grail&amp;quot; of SOA. &lt;a href="http://www.amazon.com/Enterprise-Integration-Patterns-Designing-Addison-Wesley/dp/0321200683/"&gt;Martin Fowler describes&lt;/a&gt; it thus:&lt;/p&gt;
&lt;p style="PADDING-LEFT:30px;"&gt;&lt;span class="Apple-style-span" style="TEXT-ALIGN:left;WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;COLOR:#000000;WORD-SPACING:0px;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0;"&gt;How can you minimize dependencies when integrating applications that use different data formats? &amp;hellip;Design a Canonical Data Model that is independent from any specific application. Require each application to produce and consume messages in this common format&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Sounds perfect! We now have a way of abstracting our systems from each other, and our canonical data model will pull this information all together at the end.&lt;/p&gt;
&lt;p&gt;So, I&amp;#39;m going out on a limb here, disagreeing with Martin Fowler is rarely a smart move.&lt;/p&gt;
&lt;p&gt;Except I&amp;#39;m not disagreeing with Martin Fowler so much as I am disagreeing with those that try to implement his ideas and principles. I don&amp;#39;t recollect anywhere that Martin says that all data can be squeezed into this model, or that this model is the &amp;quot;one true view&amp;quot;. In fact, later evolutions of Martin&amp;#39;s work have included things like Domain Driven Design, which is almost the opposite of a canoncial data model - DDD says that you have a different model for every domain or context, each specifically engineered to solve the problem at hand. Given the choice, I am certainly in the DDD camp here - after all data without context is worthless, and it is the domain (and&amp;nbsp;DDD) that provides the context.&lt;/p&gt;
&lt;p&gt;The key and important part of a canonical data model is it provides a mapping between inconsistent formats and differing views of data.&lt;/p&gt;
&lt;p&gt;So as an ideal, the canonical data model is fine - in practice it is often a major anti-pattern.&lt;/p&gt;
&lt;p&gt;There is still no One True Authority, and SOA really never promised this, nor did the canonical data model - what they promised was enough abstraction to allow applcaitions to work independently, and to fulfill their specific requirements independently. And then to leave it to the service layers to unify this data.&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;So Service Oriented Architectures HAVE Come to the Rescue?&lt;/h3&gt;
&lt;p&gt;Not quite - again my argument here isn&amp;#39;t so much about SOA, or any of the things around it, but more the people who try to implement it, often&amp;nbsp;without understanding what they are trying to achieve, the problems that SOA or a service layer solves,&amp;nbsp; or even the amount of effort that is required to get a canonical data model right.&lt;/p&gt;
&lt;p&gt;The key part here is that a canonical data model is a massive excercise to do right. Essentially it means analysing every applciation, every service and every database within your organisation and extracting their individual requirements and current functionality, and trying to distill that down to a set of common definitions.&lt;/p&gt;
&lt;p&gt;While this may be eventually achievable, in practice it is almost never the case. This &amp;quot;holy grail&amp;quot; of SOA is much like the &amp;quot;holy grail&amp;quot; of integration at the database level, eventually it boils down to &amp;quot;the problem is too complex, too fast moving and too indeterminate to define&amp;quot;. In development we figured out long ago that we needed Agile to help us move quickly as requirements shifted and changed, but the OTADB and the SOA canonical data model do not allow this to happen, they seek to solidify the design early.&lt;/p&gt;
&lt;p&gt;SOA done &amp;quot;right&amp;quot; may well make this problem smaller, but SOA is rarely done right.&lt;/p&gt;
&lt;h3&gt;Can Domain Driven Design Help?&lt;/h3&gt;
&lt;p&gt;DDD was largely an attempt to address these problems - by identifying that individual applications and contexts required different data, DDD allowed us to recognise that square pegs and round holes do not match. Eventually with a big enough hammer you may be able to force it through, but there is going to&amp;nbsp;be a lot of effort and a lot of collaterel damage.&lt;/p&gt;
&lt;p&gt;Domain Driven Design includes terms like &amp;quot;Domain&amp;quot;, &amp;quot;Bounded Context&amp;quot; and&amp;nbsp;&amp;quot;Anti Corruption Layer&amp;quot;&amp;nbsp;specifically to deal with the issues around trying to use a &amp;quot;one solution fits all&amp;quot; hammer to crack what is quite a simple nut. &lt;/p&gt;
&lt;p&gt;DDD is largely unconcerned with what other things Domains talk to, or even how, all that matters is that within a Domain there is one logical view of the world, tailored to the requirements of that specific Domain. Beyond that Domain, there may be a large central database, there may be a perfect canonical data model, but at application level, we have the Domain, Bounded Contexts and Anot Corruption Layers to protect us from all of that.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;DDD gives us a loosely coupled way of thinking.&lt;/p&gt;
&lt;h3&gt;So SOA is Evil and&amp;nbsp;the One True Authority Database is Evil?&lt;/h3&gt;
&lt;p&gt;Well hardly, but unfortunately we live in an imperfect world, and the people implementing these noble ideas are often far from perfect. They do not have mystical powers of future vision, and they do make mistakes.&lt;/p&gt;
&lt;p&gt;By creating properly decoupled systems, and in this I certainly do not mean &amp;quot;systems that use web services&amp;quot;, we can isolate our applications from external change and influence, and ensure we meet the requirements we have today, and can react quickly to future requirements as they appear.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Big Design Up Front is still BDUF, whether it is done at a database level, a service level, or in a canonical data model.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=46913" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category><category domain="http://devlicio.us/blogs/casey/archive/tags/SOA/default.aspx">SOA</category></item><item><title>Commercial Suicide - Integration at the Database Level</title><link>http://devlicio.us/blogs/casey/archive/2009/05/14/commercial-suicide-integration-at-the-database-level.aspx</link><pubDate>Thu, 14 May 2009 06:44:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:46809</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=46809</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=46809</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/05/14/commercial-suicide-integration-at-the-database-level.aspx#comments</comments><description>&lt;p&gt;There are many ways you can commit commercial suicide, but there is possibly no slower and more agonising death than that produced by attempting that great architectural objective, the single authoritative database to which all applications talk.&lt;br /&gt;&lt;br /&gt;The theory is good, if we have a single database then we have all our business information in one place, accessible to all, easy to report against, reduced maintenance costs, consistency across all applications, and a host of other good objectives.&lt;br /&gt;&lt;br /&gt;However all these noble ideals hide a more fundamental problem, that the single database does not solve any of them, and makes most of them into far bigger problems.&lt;/p&gt;
&lt;h3&gt;All Information In One Place - The Single Authority&lt;/h3&gt;
&lt;p&gt;On the face of it this sounds like a great objective - after all developers try to live by the maxim of Don&amp;#39;t Repeat Yourself - and data in many place is a clear violation of that principle.&lt;/p&gt;
&lt;p&gt;Data that appears across many applications and across many storage mechanisms leads to all sorts of massive problems; inconsistency, duplication, replication, duplicated business logic and code, essentially all boiling down to - you end up with spaghetti data. Spaghetti data is much like spaghetti code - it sprawls, gets tangled up, and becomes hard to pull apart without covering yourself in pasta sauce. This is obviously a &amp;quot;bad thing&amp;quot;.&lt;/p&gt;
&lt;h4&gt;So what is wrong?&lt;/h4&gt;
&lt;p&gt;Well the first and most obvious thing that is wrong is that all applications have different requirements, and different &amp;quot;world views&amp;quot;. Although there may in theory be some concept of a &amp;quot;Customer&amp;quot; for the organisation as a whole, even this most basic of data items varies widely between individual departments and even individual applications within a single department.&lt;/p&gt;
&lt;p&gt;You could approach this problem, as many database centric people would do, and say &amp;quot;that is the problem right there, we need to standardise all these applications to use the One True Customer&amp;quot;. But that is missing the really important word in the definition of the problem ... &amp;quot;requirements&amp;quot; ... this is not accidental that the Customer is different to different parts of the business, it really *is* different.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Your database guys could say &amp;quot;well your Customer in your application may be different as you have different requirements, but you will just have to fit it into our One True Customer&amp;quot;, but this is then like trying to put snakes into a plastic bag - they really don&amp;#39;t want to be in the bag, they don&amp;#39;t fit too well in the bag, and sooner or later while putting one in some others are going to escape or bite your hand. And when your other department starts putting his snakes in the bag too you will be fighting for who gets to not be bitten.&lt;/p&gt;
&lt;p&gt;And worse still - now you are trying to map from your requirements based Customer to the One True Customer, and spend an inordinate amount of time maintaining this translation layer in your application. When that One True Customer changes, as he undoubtedly will as new applications require he is expanded to deal with more data they require, every previous application needs to be revisited, large parts of it need ot be re-written, and the whole application needs to be regression tested again. And you have to do this for every single application talking to your single authority database. &lt;/p&gt;
&lt;p&gt;You could just skip this stuff, and rely on your applications ignoring this new data, and rely on the database not caring if they correctly updated new data - but this really will come back to bite you - when that bag of snakes starts getting really large and really full - you really don&amp;#39;t want to be the one trying to get new snakes into it.&lt;/p&gt;
&lt;h4&gt;The Truth About the One True Authority&lt;/h4&gt;
&lt;p&gt;There isn&amp;#39;t one.&lt;/p&gt;
&lt;p&gt;There - I&amp;#39;ve said it - I have upset all those database guys, probably upset a large number of SOA guys (I&amp;#39;ll cover Commercial Suicide - Integration at Service Level in a later post), and have totally disagreed with noble business objectives.&lt;/p&gt;
&lt;p&gt;The truth is, data must have context - without context, data is worthless, absolutely and totally worthless. Data stored in a database has no context, and therefore has no value. Context is provided by the applications that read and write that data, and therefore they are the only thing that matters, and their requirements are the only thing that matters. That means, they need data that is specific to their application, structured in a way that makes that application meet the business objectives, and in a way that makes that application meet non-functional requirements like resilience, reliability and consistency.&lt;/p&gt;
&lt;h4&gt;So Why Does Anyone Want the One True Authority Database?&lt;/h4&gt;
&lt;p&gt;Well, in legacy terms it is easy to understand why database admins and database developers want it - it is their lifeblood, their whole raison&amp;nbsp;d&amp;#39;etre. More importantly, it is the culture in which they were brought up - the data is the important thing, the data is the centre of the universe, the data must be consistent, uniform and pure.&lt;/p&gt;
&lt;p&gt;But leaving database developers aside, more importantly why would a corporation want the One True Authority Database (OTADB)? After all, the title of this post says &amp;nbsp;this is &amp;quot;Commercial Suicide&amp;quot;, so why hasn&amp;#39;t this got through to management?&lt;/p&gt;
&lt;p&gt;Well - the promise of OTADB is that it will reduce errors in duplication, reduce waste, reduce duplicated effort and reduce maintenance costs - all highly desirable business objectives. And indeed, from those that advocate this approach (those database admins and developers again), the OTADB sounds mighty attractive. On the face of it, it achieves all of these objectives.&lt;/p&gt;
&lt;p&gt;Where it falls down is that this holy grail of software development is always just out of reach, they never quite manage to achieve it. Each application that is developed starts to make the OTADB worse, people start to hack things into it to get it to meet business requirements, not because developers want to hack things together, but precisely becasue the restriction of the OTADB *forces* them to do it that way if they are to deliver any kind of functionality at all.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;They blame these hacks for ruining their vision of the One True Authority Database, the database admins tell them that they have to fight the application teams to stop them messing up their nice database, but that the OTADB no longer meets the noble objectives as those pesky development teams have messed it up for everyone.&lt;/p&gt;
&lt;h3&gt;Wait a Minute - What is the BUSINESS Objective Behind the One True Authority Database?&lt;/h3&gt;
&lt;p&gt;If anyone was to step back from those noble objectives and ask a far more fundamental question, the solution might actually be a lot more obvious than it may seem. While they are all noble objectives, largely actually made worse specifically by the OTADB approach, they are not the real business objective.&lt;/p&gt;
&lt;p&gt;Underlying all the other requirements, the ultimate business requirement that drives people (in particular database admins) to want a single database is so that they can see what their company looks like, in other words - so they can produce Management Information - reports to you and me. &lt;/p&gt;
&lt;p&gt;This is the single and most fundamental requirement for a business - to have a clear, consistent, accurate and up to date picture of what their company looks. This is what management needs, it is what allows them to make decisions, allows them to identify problems and allows them to spot opportunities.&lt;/p&gt;
&lt;p&gt;So, we are going to all this effort, and believe me it is extensive and significant effort, all to support some reporting tools at the end of the day. Reporting tools have problems with data in different formats, with data that is inconsistent, with data that is disparate and distributed. So at some point in the past, the &amp;quot;accepted truth&amp;quot; became &amp;quot;we need one true authority database to be able to produce good reports&amp;quot;&lt;/p&gt;
&lt;p&gt;Reporting is a Context - and data only has purpose and relevance in context&lt;/p&gt;
&lt;h4&gt;If the Elephant in the Room is Actually Reporting, How Do We Solve The Elephant Problem?&lt;/h4&gt;
&lt;p&gt;This is almost so easy to deal with, it is silly. Perhaps it is because it is so obviously simple that is has been overlooked by many and rejected by others. Especially as it violates another one of those noble objectives ... to provide quality reporting information, we duplicate more of our data.&lt;/p&gt;
&lt;p&gt;Yep - we duplicate it - after all reporting data is read only, so it doesn&amp;#39;t matter if it is just a copy of other data. Reporting requirements are also very different to transactional requirements too, so we get the added benefit of being able to optimise that duplicated data for the reporting functions. &lt;/p&gt;
&lt;p&gt;Data in relational databases is actually very poor for query and reporting purposes, and there is a constant compromise to make it fast for all those applications to write to, that makes it poor to report on - and vice-versa.&lt;/p&gt;
&lt;p&gt;How this data gets into the reporting database isn&amp;#39;t my direct concern in this blog post, suffice to say the &amp;quot;easy&amp;quot; way is to publish messages with data changes, and have a reporting application pick those up and persist them. My point here - is that splitting the reporting functions from the day-to-day business functions pays massive dividends.&lt;/p&gt;
&lt;h4&gt;Now We Have a New Problem&lt;/h4&gt;
&lt;p&gt;That still leaves us with one problem - what happens when disparate applications really do need to know about data in other applications? What happens when my call centre operatives are asked to update the address for one of the Customers. Now as each application has it&amp;#39;s own view of the world, and it&amp;#39;s own data stores, my accounting applciation does not have access to that change.&lt;/p&gt;
&lt;p&gt;Well, the solution to the &amp;quot;how does data get in the reporting databases&amp;quot; question is exactly the same one here - you published messages from your application when you have changes that the rest of the corporation may be interested in. Fire off a message saying &amp;quot;CustomerAddressUpdated&amp;quot; and any other applciation that is concerned can now listen for that message and deal with it as it sees fit.&lt;/p&gt;
&lt;h3&gt;As It Sees Fit&lt;/h3&gt;
&lt;p&gt;And this is the real business objective we were trying to achieve in the beginning ... avoiding Corporate Suicide.&lt;/p&gt;
&lt;p&gt;When applications are each responsible for their own data, their own actions, and are only responsible for letting the &amp;quot;enterprise&amp;quot; know they have made some changes that other things may need to know about - then you have your solution.&lt;/p&gt;
&lt;p&gt;In good development terms, we have proper separation of concerns ... applications are responsible for their data, and their data only. They decide if they care about data from other applications - they are not forced to use it, nor to work around it.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=46809" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/.NET/default.aspx">.NET</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category></item><item><title>We Are Not Doing DDD</title><link>http://devlicio.us/blogs/casey/archive/2009/04/08/we-are-not-doing-ddd.aspx</link><pubDate>Wed, 08 Apr 2009 20:34:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:45582</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>28</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=45582</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=45582</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/04/08/we-are-not-doing-ddd.aspx#comments</comments><description>&lt;p&gt;Recently I joined a new project, and within reason, and excluding some legacy systems we have to talk to, we have the &amp;ldquo;luxury&amp;rdquo; of an almost greenfield project. Probably as greenfield as you realistically get anyway.&lt;/p&gt;
&lt;p&gt;I was brought in partially as a good old fashioned coder (the project needed another person cutting code) and partly as guys in the team knew my blog and general approach to software development and wanted to inject some of those ideas into the team. I&amp;rsquo;m not sure exactly what they were expecting, and I&amp;rsquo;m not sure if they got what they were expecting, but suffice to say the first two weeks has been turbulent and frenzied.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m certainly not known for my subtlety, nor for my coding skills which are by no means the best you can buy in. What I feel is my key strength is my ability to look at the wider picture than sometimes teams have the luxury to do, and to push some of my enthusiasm and energy into a project. I&amp;rsquo;m sure I&amp;rsquo;ll blog in the near future about the actual impact my presence has had on the team and the project, but this blog is primarily about why we are not going to be doing DDD.&lt;/p&gt;
&lt;h3&gt;But Casey Does Domain-Driven Design&lt;/h3&gt;
&lt;p&gt;One of the factors that made the client hire me was my knowledge around DDD, or at least my projection of that knowledge on the web. It was certainly something they felt could have helped some of their past and ongoing projects, and something they thought would be a real help to them. I don&amp;rsquo;t think anyone suspected I would come in and say &amp;ldquo;Hey guys, lets all do full on DDD&amp;rdquo;, so luckily for them I didn&amp;rsquo;t, what may have surprised them is that I did pretty much the opposite. In fact my early assessment was this project really didn&amp;rsquo;t have any need for most of the stuff in DDD &amp;ndash; fundamentally it, like the other projects in progress by the same team, was a CRUD application at heart. Data out of the DB, a bit of manipulation, some input by users and services, and dump the data back to the DB.&lt;/p&gt;
&lt;p&gt;Many out there disagree with my general statement that CRUD applications don&amp;rsquo;t make good candidates for DDD &amp;ndash; but not only do I stand by that statement, but two of the other client&amp;rsquo;s projects nearing completion showed some of the real pitfalls of DDD. While previous projects had attempted to use parts of DDD to help simplify their applications, it had not all gone according to plan.&lt;/p&gt;
&lt;h3&gt;What Goes Wrong With DDD Over CRUD &amp;ndash; The DB Domain Model&lt;/h3&gt;
&lt;p&gt;The first most obvious weakness of the systems that existed was the Domain Model. It had started with all the best intentions &amp;ndash; there was a legacy database that acted as the primary store for the business, and many different systems used and updated this database. So NHibernate was chosen as a way to create a Domain Model over the database, to abstract the system away from the DB and to treat it as a legacy system.&lt;/p&gt;
&lt;p&gt;Unfortunately one of the primary requisites of DDD is direct involvement and commitment from the business to the project&amp;nbsp; and in this case that wasn&amp;rsquo;t part of the project. The team had to come up with their own Domain Model &amp;ndash; and in doing so had fallen into the classic pitfall of creating a &amp;ldquo;domain model&amp;rdquo; that mirrored the legacy database. Now not only was there a legacy database, but essentially there was a legacy domain model too. A large amount of work was put into getting this NHibernate model right, and in hindsight it has probably consumed disproportionate resources to get this model overlaying the DB. &lt;/p&gt;
&lt;p&gt;In addition, although at present it simplifies many operations against the DB, it does not abstract the code base from the database, it just acts as a heavyweight data access layer. Worse still, many of the &amp;ldquo;hacks&amp;rdquo; that had to be made to get NHibernate to work with the (frankly terrible) underlying database structure, had actually made the system very fragile, and fairly rigid.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lesson to be learned:&lt;/strong&gt; Do not drive your domain model from the database, drive it from the &lt;a target="_blank" href="http://dddstepbystep.com/wikis/ddd/domain-expert.aspx"&gt;Domain Experts&lt;/a&gt;, user stories and use cases. Without significant commitment from your business to provide you with those experts, you are at risk of creating a significant amount of code, with no quantifiable benefit.&lt;/p&gt;
&lt;h3&gt;What Goes Wrong With DDD &amp;ndash; Where is The Ubiquitous Language?&lt;/h3&gt;
&lt;p&gt;A somewhat related problem that is now obvious, again in hindsight, is that there was no clear attempt made to extract any kind of Ubiquitous Language out of the (non-existent) domain experts, nor from the business analysts. The language between the teams has fallen into the classic pitfall of a language confused across development teams, technical support teams, legacy systems, and business terminology that was not clearly defined nor understood.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lesson to be learned:&lt;/strong&gt; Get your language sorted out early on. Insist upon a basic glossary of terms at the very minimum, regardless of whether you do DDD or not. This will save countless hours of mistakes and confusion, and will get new developers and business people up to speed on the project in record time.&lt;/p&gt;
&lt;h3&gt;What Goes Wrong With DDD &amp;ndash; Services Services Services!&lt;/h3&gt;
&lt;p&gt;I sat down early on with numerous team members and the architect who was in the unenviable position of making all this stuff work, and ensuring all the new stuff learned from previous mistakes and moved forward.&lt;/p&gt;
&lt;p&gt;One of the things that became apparent to me was that while discussing architectures, the term &amp;ldquo;Services&amp;rdquo; was in frequent use, more than frequent to be honest, it permeated through everything. It was a term that business analysts and project managers were comfortable with, and perhaps that was the reason that &amp;ldquo;Services&amp;rdquo; was the terminology for decoupling things. From that it soon became obvious that Services of all kinds had become intrinsic to the current codebase, from Web Services, to Application Services, to Domain Services, and on and on.&lt;/p&gt;
&lt;p&gt;Now, there is nothing wrong with &amp;ldquo;Services&amp;rdquo; per se, but interestingly when I suggested a new architecture for the new project, and discussed this with various people, I managed to explain the entire new architecture with almost no use of the word &amp;ldquo;Services&amp;rdquo;. &lt;/p&gt;
&lt;p&gt;When I pointed this out, it soon became clear we could discuss a decoupled system, with little direct reference to &amp;ldquo;Services&amp;rdquo;, as fundamentally Services are an implementation detail, and have little important to the overall architecture of a system. Now instead of talking about Service being called, we can discuss messages being moved around the system, without any need to worry about what consumes them, or how they are delivered. The final architecture includes services for sure, but they are the implementation of a much more abstract concept.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lesson learned:&lt;/strong&gt; Technical language can become the norm, and then it tends to be the solution to every problem. Step back from things once in a while, and see if the terminology you are using is driving your architecture and design. Try to avoid using implementation terminology in relation to things like architecture, or it will drive the design.&lt;/p&gt;
&lt;h3&gt;So Where Are We Now?&lt;/h3&gt;
&lt;p&gt;The one really big thing that has happened over the past two weeks is that the introduction of some new blood to the team, along with some new ideas, has really revitalised the team. Everyone has new found energy. We also have a broad consensus of what didn&amp;rsquo;t go right on the last two projects, and the things that went well &amp;ndash; we now have a strong idea of how to build on the strengths of the existing systems, and where we need to compensate for the things that didn&amp;rsquo;t turn out as planned.&lt;/p&gt;
&lt;p&gt;Luckily the teams involved are all really eager to learn, and really keen to make each and every system better &amp;ndash; and that alone will make a huge impact to how successful this project will be.&lt;/p&gt;
&lt;p&gt;Well, this post has turned into a bit of an EPIC &amp;hellip; and is far too long to continue without boring everyone to death &amp;hellip; so I will post the concluding part soon. And with luck I may explain my opening statement &amp;hellip; &amp;ldquo;We Are NOT Doing DDD&amp;rdquo;.&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:13440f7e-d7d5-41ff-839a-d956b4f1b3d6" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;del.icio.us Tags: &lt;a rel="tag" href="http://del.icio.us/popular/DDD"&gt;DDD&lt;/a&gt;,&lt;a rel="tag" href="http://del.icio.us/popular/Domain+Driven+Design"&gt;Domain Driven Design&lt;/a&gt;,&lt;a rel="tag" href="http://del.icio.us/popular/Architecture"&gt;Architecture&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=45582" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category></item><item><title>Delay All Your Decisions</title><link>http://devlicio.us/blogs/casey/archive/2009/03/21/delay-all-your-decisions.aspx</link><pubDate>Sat, 21 Mar 2009 16:02:26 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:45084</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=45084</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=45084</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/03/21/delay-all-your-decisions.aspx#comments</comments><description>&lt;p&gt;I sat down yesterday for a really interesting interview with a potential client. We discussed a variety of things, but one thing that stood out for me was a question around which message bus I would use and why.&lt;/p&gt;  &lt;p&gt;Apparently there had been some discussions already, and the application being developed might need one in future. So the team had been discussing their options, after all at some point in the future the whole organisation could benefit from this and other applications could start to use this message bus to benefit everyone.&lt;/p&gt;  &lt;p&gt;My response, and a rather unexpected one it seems, was “do you really need it?”, and “if so, what’s wrong with just using MSMQ?”&lt;/p&gt;  &lt;p&gt;I’m all for simplicity, and YAGNI, which this decision would fall firmly under – but I’m also a great advocate of:&lt;/p&gt;  &lt;h3&gt;Delay All Your Decisions Until The Last Possible Moment&lt;/h3&gt;  &lt;p&gt;Although the team could have gone for a well established message bus, JBoss and RabbitMQ were mentioned as options – my view was that this was a decision they didn’t need to make now, and making that decision could possibly work to the detriment of this project, and possibly of the whole organisation.&lt;/p&gt;  &lt;p&gt;While this project may well need a messaging system of some kind, the implications of picking one of these choices, or another like NServiceBus or MassTransit now were quite large.&lt;/p&gt;  &lt;p&gt;Without any requirement from the organisation as a whole, picking a message bus and hoping the rest of the organisation would run with it too were small – something intended to work on this scale requires a lot more time, thought and consideration than was available, and putting it out for discussion at this point could delay the current project, potentially fatally.&lt;/p&gt;  &lt;p&gt;Picking one of the options now for this project in isolation was a much better route, but the requirements for this application are as of yet unknown, and so any choice would be made on an arbitrary criteria, potentially going down a technology route for the sake of it, rather than a real need. And the more complex the solution picked for this application, the less likely it was going to fit with the organisation’s wider decision later on.&lt;/p&gt;  &lt;p&gt;So my answer was simple … for now I would go with a very simple event broker in the application, and have that use the simplest possible method of messaging available right now (probably MSMQ). When the needs of the application developed, it would then be simple to bring in a more advanced bus system by just changing the event broker, or similarly if the organisation’s needs became clearer it would be easy to use their bus of choice.&lt;/p&gt;  &lt;p&gt;This was a decision that didn’t need to be made at this point. Consider it a premature optimisation.&lt;/p&gt;  &lt;p&gt;By taking a simple option now, and delaying the real decision to a later date, the team will gain flexibility on their immediate application, gain clarification of their requirements and of the organisation’s requirements as a whole, will avoid locking in to any technology that could be harder to back out of later – and most importantly of all – keep focused on delivering the project at hand, rather than get distracted by this decision.&lt;/p&gt;  &lt;p&gt;When a decision stops being trivial, delay it until the last possible moment you can – you will always have more information by then, and will therefore make a better decision.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=45084" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category></item><item><title>DDD: Invariants or Contextual Validation?</title><link>http://devlicio.us/blogs/casey/archive/2009/03/11/ddd-invariants-or-contextual-validation.aspx</link><pubDate>Wed, 11 Mar 2009 08:10:44 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:44910</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=44910</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=44910</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/03/11/ddd-invariants-or-contextual-validation.aspx#comments</comments><description>&lt;p&gt;Greg Young made a good point to me regarding my last post about &lt;a href="http://dddstepbystep.com/blogs/dddstepbystep/archive/2009/03/04/ddd-validity-consistency-and-immutability.aspx" target="_blank"&gt;Validation, Consistency and Immutability&lt;/a&gt;, specifically around validation – even more specifically he thought I may have simplified it too far.&lt;/p&gt;  &lt;p&gt;I was trying to cover the basics of the subject, but may have made the bit around validation a little fuzzy – over the series I have been deliberately taking baby steps so as not to lose anyone early. A lot of the confusion I see around DDD is largely around people jumping right into the deep end without the basic stuff clear first.&lt;/p&gt;  &lt;p&gt;The last blog post had a very long section that I removed – it was entitled “Invariants” – but after I proof read it, it made absolutely no sense to me, so I figured it would make no sense to anyone reading it either. Turns out, by removing it, I may have made less sense too! I’ll try again. (&lt;em&gt;disclaimer: suffering from really rotten head cold, so this may make no sense either – feel free to tell me if so&lt;/em&gt;)&lt;/p&gt;  &lt;h3&gt;Invariants&lt;/h3&gt;  &lt;p&gt;As the name says, an Invariant is something that cannot change – it cannot be varied. &lt;/p&gt;  &lt;p&gt;Greg’s point was:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;not to get philosophical but... consider Plato&amp;#39;s ideals. What attributes of an object make it that object? Must be valid!&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This is what I believe Greg likes to refer to as an invariant – or what I was trying to explain around using constructors to enforce “valid” entities.&lt;/p&gt;  &lt;p&gt;This then splits our validation into two distinct camps – the invariants and the contextual validation.&lt;/p&gt;  &lt;p&gt;An invariant is something that cannot be changed about our object – using our previous example, it must have a CustomerNumber, a FirstName and a LastName, and these must always be valid individually and when evaluated together – there can be no variance from that rule – and so we enforce that by only allowing setting these via the constructor or a strongly named method. No Customer can ever exist without these being valid.&lt;/p&gt;  &lt;p&gt;The things that make our Customer a Customer are those attributes.&lt;/p&gt;  &lt;h3&gt;Contextual Validation&lt;/h3&gt;  &lt;p&gt;In my previous example, I used the concept that a Customer may have a Policy – let’s say this is an insurance scenario. I also said that the fact that the Customer didn’t have a Policy, it didn’t mean the Customer was invalid, it just meant we would have to have some kind of validation at the point we needed to have a Policy&lt;/p&gt;  &lt;p&gt;This is what I believe Greg refers to as Contextual Validation – when we use the &lt;a href="http://dddstepbystep.com/wikis/ddd/entity.aspx"&gt;entity&lt;/a&gt; we may need to validate it is in a fit state for the specific operation we wish to carry out, but we shouldn’t have to validate everything about it.&lt;/p&gt;  &lt;h3&gt;In Conclusion&lt;/h3&gt;  &lt;p&gt;Greg said he is doing up a blog post of his own on the subject at some point in the future, which is good as the issue of validation seems to be a sticking point in DDD and in development generally. I definitely look forward to reading it. Keep an eye on &lt;a href="http://codebetter.com/blogs/gregyoung/" target="_blank"&gt;his blog at Codebetter&lt;/a&gt; for it.&lt;/p&gt;  &lt;p&gt;By defining the types of validation more accurately, we can try and avoid some of the confusion which I was introducing in my last post on the subject.&lt;/p&gt;  &lt;p&gt;Invariants are the rules that cannot ever be broken about an entity, aggregate or VO&lt;/p&gt;  &lt;p&gt;Contextual validation is a check that for a particular action, the entity, aggregate or VO is valid at that point.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:dc8aa1f1-eaf7-4264-9ce6-8a1ff94b8002" class="wlWriterEditableSmartContent"&gt;del.icio.us Tags: &lt;a href="http://del.icio.us/popular/DDD" rel="tag"&gt;DDD&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Validation" rel="tag"&gt;Validation&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Invariants" rel="tag"&gt;Invariants&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=44910" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category></item><item><title>DDD: Sample Application First Steps</title><link>http://devlicio.us/blogs/casey/archive/2009/03/09/ddd-sample-application-first-steps.aspx</link><pubDate>Mon, 09 Mar 2009 16:39:14 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:44877</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=44877</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=44877</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/03/09/ddd-sample-application-first-steps.aspx#comments</comments><description>&lt;p&gt;As &lt;a href="http://dddstepbystep.com/blogs/dddstepbystep/archive/2009/03/07/ddd-the-beginning-of-my-sample-application.aspx" target="_blank"&gt;mentioned in my last post&lt;/a&gt;, the &lt;strong&gt;DDD Parcel Service&lt;/strong&gt; is opening up for business soon. Also as mentioned, the first thing I did was to grab my &lt;a href="http://dddstepbystep.com/wikis/ddd/domain-expert.aspx" target="_blank"&gt;Domain Expert&lt;/a&gt; and start some initial conversations around the first areas I am approaching – the Booking service.&lt;/p&gt;  &lt;p&gt;All of the documentation I am generating is to be included in the SVN (SVN public access to follow in the near future please be patient), so hopefully as the design evolves, and the &lt;a href="http://dddstepbystep.com/wikis/ddd/ubiquitous-language.aspx"&gt;UL&lt;/a&gt; and model change with it, this will be evident to some degree within that documentation. I’m going to get boring again, but the documentation is far more important in DDD than many of us Agilists would traditionally like – at least the resulting artifacts are, so it is important to me to show in the documentation why I am making code decisions.&lt;/p&gt;  &lt;h3&gt;The Tools&lt;/h3&gt;  &lt;p&gt;I am primarily using two nifty tools at the moment for capturing the information I need – the ever excellent MS Office OneNote 2007, and a new addition to my arsenal – &lt;a href="http://www.balsamiq.com/" target="_blank"&gt;Balsamiq&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;OneNote provides and excellent dumping group for notes and ideas as they get fired at me, and quickly allows me to form those into some coherent format.&lt;/p&gt;  &lt;p&gt;But the true star of this process so far is Balsamiq. &lt;/p&gt;  &lt;h3&gt;User Interface Lead Design?&lt;/h3&gt;  &lt;p&gt;I wanted to validate the User Stories I had extracted from my DE, by showing how these could take form within the UI. To some this may seem like heresy, after all DDD is all about the domain right?&lt;/p&gt;  &lt;p&gt;Well, I actually see DDD as being lead by a combination of the Ubiquitous Language on one hand, and the User Stories and Use Cases on the other.&lt;/p&gt;  &lt;p&gt;Extracting the UL from the initial User Stories was part of the first task, but validation of these was far easier to do with some visual aid – so a mockup was needed. I could have done this in HTML, but luckily Balsamiq recently became a devlicio.us partner and I was keen to try it in anger on a “real” project.&lt;/p&gt;  &lt;h3&gt;Mockups Made Simple&lt;/h3&gt;  &lt;p&gt;The initial user story we worked out was around booking a parcel delivery, and after some discussion we got to:&lt;/p&gt;  &lt;p&gt;&lt;em&gt;The Customer wants to be able to send a Consignment , so they log onto the Client website to Book a Delivery:&lt;/em&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;em&gt;They choose their &lt;strong&gt;Pickup Location&lt;/strong&gt;&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;&lt;em&gt;They choose their &lt;strong&gt;Destination&lt;/strong&gt;&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;&lt;em&gt;They choose their required &lt;strong&gt;Pickup Date&lt;/strong&gt;&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;&lt;em&gt;They choose their &lt;strong&gt;Delivery Window&lt;/strong&gt; options&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;&lt;em&gt;They choose the &lt;strong&gt;Consignment&lt;/strong&gt; details&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;&lt;em&gt;They choose any &lt;strong&gt;Special Instructions&lt;/strong&gt;&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;&lt;em&gt;They receive a &lt;strong&gt;Quotation&lt;/strong&gt; for delivery if the system is capable of quoting, or a &lt;strong&gt;Callback&lt;/strong&gt; for a &lt;strong&gt;Non-Standard Consignment&lt;/strong&gt;&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;&lt;em&gt;If they wish to proceed they click &lt;strong&gt;Confirm&lt;/strong&gt;&lt;/em&gt; &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Firing up Balsamiq was quick, and within a few minutes of drag and drop, and a few moments of cursing when I kept forgetting to lock items, I had a good starting point for the UI:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="Booking a new delivery - step 1" border="0" alt="Booking a new delivery - step 1" src="http://devlicio.us/blogs/casey/Bookinganewdeliverystep1_203A5A53.png" width="640" height="409" /&gt; &lt;/p&gt;  &lt;p&gt;Less than 30 minutes later I had the whole story mocked up and ready to validate. Not unsurprisingly, it was really easy for my DE to look at these screens and envisage the process in action. This lead to a few changes in the UL, and a couple of extra questions arising – exactly the kind of validation that was needed. Had I done this in HTML or code I could have spent a good many hours doing something not half as representative.&lt;/p&gt;  &lt;p&gt;If you want to see the initial modelling you can download them as PDFs here:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://dddstepbystep.com/cfs-file.ashx/__key/CommunityServer.Components.SiteFiles/SampleApp.Docs/Booking-and-Tracking-Stories.pdf" target="_blank"&gt;Booking and Tracking User Stories&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://dddstepbystep.com/cfs-file.ashx/__key/CommunityServer.Components.SiteFiles/SampleApp.Docs/Ubiquitous-Language.pdf" target="_blank"&gt;Ubiquitous Language&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://dddstepbystep.com/cfs-file.ashx/__key/CommunityServer.Components.SiteFiles/SampleApp.Docs/Mockups-for-Booking-a-Delivery.pdf" target="_blank"&gt;Mockups for Booking Delivery&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Kudos to both OneNote and Balsamiq for excellent applications to let me gather and organise the results of my initial conversations!&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:0018b3ba-9797-42e2-a915-bb95154fc11b" class="wlWriterEditableSmartContent"&gt;del.icio.us Tags: &lt;a href="http://del.icio.us/popular/DDD" rel="tag"&gt;DDD&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Balsamiq" rel="tag"&gt;Balsamiq&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/OneNote" rel="tag"&gt;OneNote&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Modelling" rel="tag"&gt;Modelling&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=44877" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category></item><item><title>DDD: The Beginning of My Sample Application</title><link>http://devlicio.us/blogs/casey/archive/2009/03/07/ddd-the-beginning-of-my-sample-application.aspx</link><pubDate>Sat, 07 Mar 2009 19:08:20 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:44858</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=44858</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=44858</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/03/07/ddd-the-beginning-of-my-sample-application.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://devlicio.us/blogs/casey/dddps_logo2_441892A7.png"&gt;&lt;img style="border-right-width:0px;display:block;float:none;border-top-width:0px;border-bottom-width:0px;margin-left:auto;border-left-width:0px;margin-right:auto;" title="DDD Parcel Service" border="0" alt="DDD Parcel Service" src="http://devlicio.us/blogs/casey/dddps_logo2_thumb_19E8E4BF.png" width="540" height="86" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;Domain Driven Design Parcel Service&lt;/strong&gt; is a small competitor in a hugely competitive market. With such competitors as UPS, FedEx and DHL, we are at a competitive disadvantage, and need a solution to cover our interactions with our customers, and to streamline our internal processes. This software will be at the heart of our expansion plans and needs to grow as the business does. Although we are currently only a national carrier in the UK, our long term vision has much wider reach.&lt;/p&gt;  &lt;h3&gt;The Domain Vision Statement (Evans page 416):&lt;/h3&gt;  &lt;p&gt;The DDD Parcel Service Booking Domain is to be responsible for Customer interactions. It should reflect the processes and practices our Customers want to use. This should reflect the relationship we want to have with our Customers, and easily allow them to deal with booking processes, managing their accounts with us, our discount and promotion schemes, and allow them to see a historical record of their business with us. &lt;/p&gt;  &lt;h3&gt;And So It Begins&lt;/h3&gt;  &lt;p&gt;I needed something concrete to start putting some of the concepts from this series into, to try and show how the patterns of DDD can be used in a “real” code base. In addition, I have spent a long time thinking about the specific domain for the example application, as it had to be something that had real meat to it, and real behaviour. &lt;/p&gt;  &lt;p&gt;I could have gone down the shopping site route, but expanded it to be more like Amazon than a small one man operation – but as shopping sites have been done to death I had to find something else. Unfortunately the original example application for DDD also exists in a similar sphere to my choice. I did consider this carefully, and have weighed up many options.&lt;/p&gt;  &lt;p&gt;The domain chosen had to be:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Easy to understand when you put yourself in the shoes of the business &lt;/li&gt;    &lt;li&gt;Easy to understand when in the shoes of a customer of that business &lt;/li&gt;    &lt;li&gt;Simple enough to not get swamped in technical details &lt;/li&gt;    &lt;li&gt;Complex enough to show a number of aspects around the things DDD brings real benefits to &lt;/li&gt;    &lt;li&gt;Have real behavioural process rather than just another database and a pretty UI &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In addition, I had to be able to deal with the domain with little friction – although it may have been fun to learn some of the domains that various people suggested to me (including the Oil Drilling Industry!) – I only have a limited amount of spare time, and my girlfriend has a big priority on most of that.&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;DDD Parcel Service&lt;/strong&gt; fulfilled all of my simplicity and complexity requirements, it is a pretty common domain to most of us in everyday life, at least from the Customer perspective. It also happens to be a domain that I have touched on in the past from a business perspective.&lt;/p&gt;  &lt;p&gt;It happens that with my choice of a Parcel Service domain I had easy access to a Domain Expert of sorts – my girlfriend has a long family history in the haulage business, and on a daily basis manages the haulage for a customer just like those that the DDD Parcel Service would have.&lt;/p&gt;  &lt;h3&gt;This Morning’s Progress&lt;/h3&gt;  &lt;p&gt;Despite both myself and my girlfriend suffering bad colds, I pinned her down for 20 minutes to start the first session on extracting some of the user stories for the DDD Parcel Service&lt;/p&gt;  &lt;p&gt;I then started hammering away at a few screen mockups in &lt;a href="http://www.balsamiq.com/" target="_blank"&gt;Balsamiq&lt;/a&gt; … and I think on that point I’ll start a new blog post for tomorrow or the day after.&lt;/p&gt;  &lt;h3&gt;So Where Is The Code?&lt;/h3&gt;  &lt;p&gt;Don’t be in too much of a hurry! I have started a project, and have the core framework starting to form up. But more importantly to me, and to DDD is the design, the extraction of the &lt;a href="http://dddstepbystep.com/wikis/ddd/ubiquitous-language.aspx"&gt;UL&lt;/a&gt;, the agreement of the User Stories, and the way we get to the code. Over the next week or so I will try and start turning the initial stories into a beginning code model – and at that point you can all rip it to pieces!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f6a75167-5f33-4905-a873-c3b530deb909" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/DDD" rel="tag"&gt;DDD&lt;/a&gt;,&lt;a href="http://technorati.com/tags/User+Stories" rel="tag"&gt;User Stories&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Balsamiq" rel="tag"&gt;Balsamiq&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=44858" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Sample+Code/default.aspx">Sample Code</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category></item><item><title>DDD: Validity, Consistency and Immutability</title><link>http://devlicio.us/blogs/casey/archive/2009/03/04/ddd-validity-consistency-and-immutability.aspx</link><pubDate>Wed, 04 Mar 2009 16:47:05 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:44801</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=44801</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=44801</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/03/04/ddd-validity-consistency-and-immutability.aspx#comments</comments><description>&lt;p&gt;There seems to be some confusion around these and similar concepts, so I thought it might be an idea to provide some clarification. Now these things aren’t specific to DDD, but they certainly have a lot of relevance there, and often provoke furious debate.&lt;/p&gt;  &lt;p&gt;In this series I first mentioned these things back when I introduced Entities and Value Objects, I said that you should try and ensure Entities are valid at all times,&amp;#160; and that Value Objects should be immutable. The discussion around Aggregates and Aggregate Roots has also proved to be of some confusion, the definition from Eric Evans of an Aggregate says “A set of consistency rules applies within the Aggregate&amp;#39;s boundaries”.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://dddstepbystep.com/r.ashx?2" target="_blank"&gt;Download the ebook of the series so far&lt;/a&gt;&amp;#160;&amp;#160; (also &lt;a href="http://dddstepbystep.com/blogs/dddstepbystep/archive/2009/03/04/ddd-validity-consistency-and-immutability.aspx" target="_blank"&gt;published on dddstepbystep.com&lt;/a&gt;)&lt;/p&gt;  &lt;h3&gt;Validity&lt;/h3&gt;  &lt;p&gt;The term “valid” has many meanings in many contexts. When it was mentioned with regards to an Entity, I suggested that you should attempt to ensure that your Entities are always in a valid state, and cannot become invalid. I followed up that this could be achieved by removing property accessors, and only using either constructors or strongly named methods.&lt;/p&gt;  &lt;p&gt;Let us go back to our Customer example, and presume that for a Customer to be valid within our Domain, it must have a CustomerNumber, a FirstName, and a LastName. It may also have half a dozen other properties.&lt;/p&gt;  &lt;p&gt;So we can ensure this Entity never becomes invalid by removing the property accessors&amp;#160; for the CustomerNumber, FirstName, and LastName, and providing a single constructor that allows us to create a new Customer … Customer(customerNumber, firstName, lastName)&lt;/p&gt;  &lt;p&gt;Now there is no possibility that this Customer can become invalid – it either has these three properties or it cannot be created. If we want to be able to change the Customer’s name, we can create a new method .UpdateName(firstName, LastName)&lt;/p&gt;  &lt;p&gt;By taking this approach, we ensure that the entity can never be put into an invalid state, for example a developer setting a .FirstName property, but forgetting to set the LastName at the same time.&lt;/p&gt;  &lt;p&gt;Some have taken this concept too far, and assume that as a Customer also has a Policy, that it must always have one of these or it is invalid. Of course, there are things we may want to do with our Customer that will require it to have a Policy, but that does not constitute an invalid Customer, it constitutes a rule that must be applied before we attempt to take the action.&lt;/p&gt;  &lt;p&gt;The purpose of ensuring the Entity is always valid prevents us having to do messy things like checking an .IsValid() method every time we want to persist our entity, or we write a function that accepts an instance of our entity. Enforcing an “always valid” approach will mean that we do not have to code multiple guard clauses scattered across our applications.&lt;/p&gt;  &lt;h3&gt;Consistency&lt;/h3&gt;  &lt;p&gt;The term consistency has come up in this series and around DDD mailing lists. In this respect, consistency is the idea that one piece of data in the system is consistent with another. Here is the bad news:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;You cannot ever achieve total consistency&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;OK – headline over. The point is that something in your application will always be inconsistent. The best example is that when your users have chosen to edit the Customer on their screen they have a copy of that data. By the time they have received that data it will potentially be inconsistent with the domain, and with your persistence store – after all this is why we have the concepts of optimistic and pessimistic locking. When they submit a change to the Customer, it is probably minutes out of date.&lt;/p&gt;  &lt;p&gt;Back to our headline, when you accept that it is impossible to achieve total consistency, you can deal with the real problem – how you achieve eventual consistency. Eventually messages will make their way through our domain, eventually data will be persisted, eventually you will have consistency.&lt;/p&gt;  &lt;p&gt;While our reporting domain may lag behind our primary domain, does it honestly matter? How important is it that they are both totally consistent? You can certainly achieve a close facsimile of total consistency, but it will come at a high price – does the business want to pay for this? &lt;/p&gt;  &lt;p&gt;When you let go of the premise that your data is ever really consistent, it now just becomes an issue of an SLA agreement, if the business really wants data that accurate, then explain the costs and tradeoffs and go with it. If after an explanation they understand that “almost consistent” is good enough for almost all applications, then they just have to make a decision about how important this is to them.&lt;/p&gt;  &lt;p&gt;Consistency also applies at a much more granular level, with the domain for example. Entities and Aggregates may be valid, but not consistent with each other at all times. Eventually they should become consistent, but ensuring they are all consistent at all times is very tricky to say the least. &lt;/p&gt;  &lt;p&gt;While an Aggregate Root is responsible for ensuring consistency with the Aggregate, it is acceptable, and almost inevitable that Aggregates will be inconsistent with each other at some point in their lifetime.&lt;/p&gt;  &lt;p&gt;Again, letting go of the myth of total consistency here will give you far more flexibility. &lt;/p&gt;  &lt;h3&gt;Immutability&lt;/h3&gt;  &lt;p&gt;I would hope this is familiar to all – but perhaps some of you saw me mention it and didn’t quite get my context.&lt;/p&gt;  &lt;p&gt;The concept of immutability is pretty much what it says – the thing cannot be mutated – or in context, an Immutable Value Object can be created, but can never be changed.&lt;/p&gt;  &lt;p&gt;If you want to change an immutable object, then you can create a new one and replace the original.&lt;/p&gt;  &lt;p&gt;By making objects immutable, we avoid many problems with validation and consistency, only the constructors on the object matter, and they only need to validate the parameters passed in. It is far easier to check all the parameters at a single time than to check individual properties against each other at a later stage.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:b27ad928-4233-42a3-956d-b1e66b163890" class="wlWriterEditableSmartContent"&gt;del.icio.us Tags: &lt;a href="http://del.icio.us/popular/Validation" rel="tag"&gt;Validation&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/DDD" rel="tag"&gt;DDD&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Domain+Driven+Design" rel="tag"&gt;Domain Driven Design&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Immutability" rel="tag"&gt;Immutability&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Consistency" rel="tag"&gt;Consistency&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=44801" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category></item><item><title>DDD: The Specification Pattern</title><link>http://devlicio.us/blogs/casey/archive/2009/03/02/ddd-the-specification-pattern.aspx</link><pubDate>Mon, 02 Mar 2009 15:57:13 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:44761</guid><dc:creator>Jak Charlton</dc:creator><slash:comments>19</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/rsscomments.aspx?PostID=44761</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/casey/commentapi.aspx?PostID=44761</wfw:comment><comments>http://devlicio.us/blogs/casey/archive/2009/03/02/ddd-the-specification-pattern.aspx#comments</comments><description>&lt;p&gt;Continuing our series on Domain Driven Design, we now get to one of the more interesting patterns in DDD – the &lt;strong&gt;Specification&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;A &lt;strong&gt;Specification&lt;/strong&gt; is, in simple terms, a small piece of logic that sits on it’s own and gives an answer to a simple question … “does this match?”&lt;/p&gt;  &lt;p&gt;With a &lt;strong&gt;Specification&lt;/strong&gt; we split the logic of how a selection is made, away from the thing we are selecting.&lt;/p&gt;  &lt;p&gt;We have a Customer, and we want to be able check if they are eligible for a discount on a Product.&lt;/p&gt;  &lt;p&gt;If we were to put a method on the Customer entity, for example .IsEntitledToDiscountPrice(Product) we start to couple our entities tightly together, and as the number of questions we want to ask of our entity increases, the more polluted its interface becomes.&lt;/p&gt;  &lt;p&gt;To avoid this we can use a &lt;strong&gt;Specification&lt;/strong&gt;.&lt;/p&gt;  &lt;h3&gt;Some Code – A First for the Series&lt;/h3&gt;  &lt;p&gt;The basic implementation of Specification has a single method .IsSatisifiedBy, in our Discount example above we may have a EligibleForDiscountSpecification, the method would be .IsSatisifiedBy(Customer c)&lt;/p&gt; &lt;style&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;  &lt;p&gt;The actual Specification is variable in how it is defined, but a very simple version for this scenario would be:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public interface &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISpecification&lt;/span&gt;&amp;lt;T&amp;gt;
{
    &lt;span style="color:blue;"&gt;bool &lt;/span&gt;IsSatisfiedBy(T sut);
}

&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EligibleForDiscountSpecification &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;ISpecification&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;&amp;gt;
{
    &lt;span style="color:blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Product &lt;/span&gt;_product;
    &lt;span style="color:blue;"&gt;public &lt;/span&gt;EligibleForDiscountSpecification(&lt;span style="color:#2b91af;"&gt;Product &lt;/span&gt;product)
    {
        _product = product;
    }

    &lt;span style="color:blue;"&gt;public bool &lt;/span&gt;IsSatisfiedBy(&lt;span style="color:#2b91af;"&gt;Customer &lt;/span&gt;customer)
    {
        &lt;span style="color:blue;"&gt;return &lt;/span&gt;(_product.Price &amp;lt; 100 &amp;amp;&amp;amp; customer.CreditRating &amp;gt;= _product.MinimumCreditRating);
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;style&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;This now simplifies selection or matching, and the Customer and Product are no longer coupled – our Specification now knows how to decide if the Customer is eligible for a discount. Ignoring the fact that the following is a rotten unit test, we can use our Specification like this:&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color:#2b91af;"&gt;Fact&lt;/span&gt;]
&lt;span style="color:blue;"&gt;public void &lt;/span&gt;TestSpecification()
{
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;product = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Product&lt;/span&gt;() { MinimumCreditRating = 3, Price = 50 };
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;spec = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EligibleForDiscountSpecification&lt;/span&gt;(product);
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;goodCustomer = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;() { CreditRating = 3 };
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;badCustomer = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;() { CreditRating = 1 };

    &lt;span style="color:#2b91af;"&gt;Assert&lt;/span&gt;.True(spec.IsSatisfiedBy(goodCustomer));
    &lt;span style="color:#2b91af;"&gt;Assert&lt;/span&gt;.False(spec.IsSatisfiedBy(badCustomer));
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Two things become very easy when using Specification – we can easily select from lists and we can pass dependencies without creating coupling.&lt;/p&gt;

&lt;p&gt;To find all the Customers in a List&amp;lt;Customer&amp;gt; who are eligible for a discount, we can do (again ignoring the terrible unit test:&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color:#2b91af;"&gt;Fact&lt;/span&gt;]
&lt;span style="color:blue;"&gt;public void &lt;/span&gt;TestSpecificationCollection()
{
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;minCredit = 3;
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;product = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Product&lt;/span&gt;() { MinimumCreditRating = minCredit, Price = 50 };
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;spec = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EligibleForDiscountSpecification&lt;/span&gt;(product);

    &lt;span style="color:blue;"&gt;var &lt;/span&gt;customers = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;&amp;gt;()
                        {
                            &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;() {CreditRating = minCredit-2}, &lt;span style="color:green;"&gt;// bad credit
                            &lt;/span&gt;&lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;() {CreditRating = minCredit-1}, &lt;span style="color:green;"&gt;// bad credit
                            &lt;/span&gt;&lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;() {CreditRating = minCredit} &lt;span style="color:green;"&gt;// good credit
                        &lt;/span&gt;};
    &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;&amp;gt; eligible = GetAllCustomersMatching(customers, spec);
    &lt;span style="color:blue;"&gt;int &lt;/span&gt;count = 0;
    &lt;span style="color:blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color:blue;"&gt;var &lt;/span&gt;eligibleCustomer &lt;span style="color:blue;"&gt;in &lt;/span&gt;eligible)
    {
        &lt;span style="color:#2b91af;"&gt;Assert&lt;/span&gt;.True(eligibleCustomer.CreditRating &amp;gt;= minCredit);
        count++;
    }
    &lt;span style="color:#2b91af;"&gt;Assert&lt;/span&gt;.Equal(1, count);
}

&lt;span style="color:blue;"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;&amp;gt; GetAllCustomersMatching(&lt;span style="color:#2b91af;"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;&amp;gt; customers, &lt;br /&gt;                                           &lt;span style="color:#2b91af;"&gt;ISpecification&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Customer&lt;/span&gt;&amp;gt; specification)
{
    &lt;span style="color:blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color:blue;"&gt;var &lt;/span&gt;customer &lt;span style="color:blue;"&gt;in &lt;/span&gt;customers)
    {
        &lt;span style="color:blue;"&gt;if &lt;/span&gt;(specification.IsSatisfiedBy(customer))
            &lt;span style="color:blue;"&gt;yield return &lt;/span&gt;customer;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h3&gt;Removing the Dependencies&lt;/h3&gt;

&lt;p&gt;A scenario I had a while back required an Entity to only allow a method to proceed if a search against a Repository return no matches already there. This specific example turns out to be quite common, people seem to need access to Domain Services or Repositories on a frequent basis. &lt;/p&gt;

&lt;p&gt;My first question would be, is the Entity doing too much? Is this an operation that should be in a Domain Service or perhaps a Specification?&lt;/p&gt;

&lt;p&gt;Assuming you can legitimately answer “no” to that last question – let us assume that we want to have a Product with a ProductCode – but the ProductCode cannot be duplicated. Leaving aside the fact that this is a rather fake scenario, it means our Product now needs access to the ProductRepository, which is a bit back to front.&lt;/p&gt;

&lt;h3&gt;What Options Do We Have?&lt;/h3&gt;

&lt;p&gt;Generally in this scenario we have a few options:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The Entity could call the Repository&amp;lt;Product&amp;gt;.GetByProductCode &lt;/li&gt;

  &lt;li&gt;The Entity could call a Domain Service to check that it is a unique code &lt;/li&gt;

  &lt;li&gt;A domain service could hold all the logic and coordinate the Entity&amp;#160; with the Repository. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Option (1) is not a good design – an Entity should not be aware of Repositories. It would probably require a Service Locator pattern to achieve, but becomes very messy very quickly. It could possibly be resolved with Double Dispatch, but is still a bit back to front (see 2 below).&lt;/p&gt;

&lt;p&gt;Option (2) is a bit back to front – Entities logically sit behind Domain Services. This could work, as alluded to in a comment to a previous blog post on Domain Services, and if so will require Double Dispatch to be clean.&lt;/p&gt;

&lt;p&gt;Option (3) is probably the best of all those options. Domain Services sit in front of Repositories, Entities, and other Domain Services. &lt;/p&gt;

&lt;p&gt;The last option not on that list is to use Specification.&lt;/p&gt;

&lt;p&gt;If we created a NoOtherMatchingProductCodeSpecification, we can cleanly use this to do the work.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NoMatchingProductCodeSpecification &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;ISpecification&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Product&lt;/span&gt;&amp;gt;
{
    &lt;span style="color:blue;"&gt;private readonly &lt;/span&gt;IRepository&amp;lt;&lt;span style="color:#2b91af;"&gt;Product&lt;/span&gt;&amp;gt; _repository;
    &lt;span style="color:blue;"&gt;public &lt;/span&gt;NoMatchingProductCodeSpecification(IRepository&amp;lt;&lt;span style="color:#2b91af;"&gt;Product&lt;/span&gt;&amp;gt; repository)
    {
        _repository = repository;
    }

    &lt;span style="color:blue;"&gt;public bool &lt;/span&gt;IsSatisfiedBy(&lt;span style="color:#2b91af;"&gt;Product &lt;/span&gt;product)
    {
        &lt;span style="color:#2b91af;"&gt;Product &lt;/span&gt;match = _repository.GetProductByProductCode(product.ProductCode);
        &lt;span style="color:blue;"&gt;return &lt;/span&gt;match == &lt;span style="color:blue;"&gt;null&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Please bear in mind, this is a contrived scenario, I know this specific example has issues around ACID etc … it is here to prove a point, and Kyle Baley made me write this in a hurry!!!!&lt;/p&gt;

&lt;h3&gt;What Is Double Dispatch?&lt;/h3&gt;

&lt;p&gt;So far I have mentioned it more than a few times, and even used it in the title of this blog – so it must be important. You have probably used Double Dispatch many times, even if you didn’t know it had become a pattern with a proper name. &lt;/p&gt;

&lt;p&gt;In simple terms, Double Dispatch means we pas in an object to a function, to let the function take an action or make a decision, based on the logic of the passed in object. This decouples our Entities from the actions they take.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h3&gt;Chaining Specifications&lt;/h3&gt;

&lt;p&gt;It also happens, another useful side effect of Specification is that they can be chained very easily, but I will leave that till a future post. A starting point &lt;a href="http://en.wikipedia.org/wiki/Specification_pattern" target="_blank"&gt;if you are interested now is on Wikipedia&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;In Conclusion&lt;/h3&gt;

&lt;p&gt;I seem to have written more code in this post than I intended, and more than has been the style for the rest of the series. I again intend to blame Kyle for that :) Hopefully it has however given you a more concrete idea of where and how the Specification pattern can be of use.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a9f9e014-89e9-4c13-ae54-98d9d5aeeed9" class="wlWriterEditableSmartContent"&gt;del.icio.us Tags: &lt;a href="http://del.icio.us/popular/Specification" rel="tag"&gt;Specification&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Architecture" rel="tag"&gt;Architecture&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/DDD" rel="tag"&gt;DDD&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Domain+Driven+Design" rel="tag"&gt;Domain Driven Design&lt;/a&gt;,&lt;a href="http://del.icio.us/popular/Patterns+and+Practices" rel="tag"&gt;Patterns and Practices&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=44761" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/casey/archive/tags/Practices+and+Principles/default.aspx">Practices and Principles</category><category domain="http://devlicio.us/blogs/casey/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://devlicio.us/blogs/casey/archive/tags/DDD/default.aspx">DDD</category></item></channel></rss>