<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Billy McCafferty</title><subtitle type="html" /><id>http://devlicio.us/blogs/billy_mccafferty/atom.aspx</id><link rel="alternate" type="text/html" href="http://devlicio.us/blogs/billy_mccafferty/default.aspx" /><link rel="self" type="application/atom+xml" href="http://devlicio.us/blogs/billy_mccafferty/atom.aspx" /><generator uri="http://communityserver.org" version="4.1.31106.3070">Community Server</generator><updated>2009-09-16T11:29:00Z</updated><entry><title>A Few Thoughts on DDD, DTOs, View Models, Repositories and Separation of Concerns</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2010/03/06/a-few-thoughts-on-ddd-dtos-view-models-cqs-repositories-and-separation-of-concerns.aspx" /><id>/blogs/billy_mccafferty/archive/2010/03/06/a-few-thoughts-on-ddd-dtos-view-models-cqs-repositories-and-separation-of-concerns.aspx</id><published>2010-03-06T16:41:00Z</published><updated>2010-03-06T16:41:00Z</updated><content type="html">&lt;p&gt;What I&amp;#39;ve loved most about developing an open-source project is the ideas that I get from others who look at the work and either A) validate ideas, B) suggest that something stinks, or C) call a royal WTF and force people (e.g., me) &amp;nbsp;to explain ideas more fully. &amp;nbsp;It&amp;#39;s usually during these explanation attempts that light bulbs start to come on and ideas are refactored and become more substantiated (or at least more defendable). &amp;nbsp;The S#arp Architecture forums have been a gold mine, IMO, for the discussion of the practical application of balanced DDD techniques on real-world projects. &amp;nbsp;After feedback that I&amp;#39;ve already gotten on my recent &lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2010/03/05/better-application-services-and-cqs-with-s-arp-architecture-1-0-q3-2009.aspx"&gt;post concerning application services and CQS&lt;/a&gt;&amp;nbsp;and listening to and adding my two cents concerning ideas on the S#arp forums, I&amp;#39;d like to share a few ideas (that I shared on the forum) concerning drawing the line among application layers, the management of DTOs and View Models, and a general discussion of separation of concerns. &amp;nbsp;(As &lt;a href="http://panteravb.com/blog/posts/2009/1/9/because-thats-how-they-do-it-in-sharp-architecture.ashx"&gt;Chris Carter correctly (and a tad bluntly) explained&lt;/a&gt;, don&amp;#39;t take these ideas at face value...use them as a point of discussion and contemplation to come to your own conclusions with your project team...and let me know what you think.)&lt;/p&gt;
&lt;p&gt;Every great developer knows that any problem can be solved by adding another layer of abstraction. &amp;nbsp;;)&lt;/p&gt;
&lt;p&gt;Obviously, with additional layers of abstraction and indirection, we also introduce complexity and additional objects which need to be maintained. &amp;nbsp;Over the past year, I have had struggles concerning just how far the &amp;quot;DTO paradigm&amp;quot; should be taken to effectively separate layers from the domain and &amp;quot;best practices&amp;quot; for how they should be used. &amp;nbsp;I don&amp;#39;t think the answer can be found in a best practice (but I also hate to leave it simply at &amp;quot;it depends&amp;quot;). &amp;nbsp;I truly feel that the most important practice is for a project development team to decide, and firmly agree upon, which approach will be taken on a project and for the agreed upon approach to be followed with discipline (and enforced via code reviews) by the team.&lt;/p&gt;
&lt;p&gt;On a couple of recent projects, the biggest problems we ran into weren&amp;#39;t concerning &lt;i&gt;which&lt;/i&gt;&amp;nbsp;approach to leveraging DTO for layer separation was &amp;quot;best,&amp;quot; but on coming to a firm layering decision and having the team discipline to adhere to those practices. &amp;nbsp;Because of these &amp;quot;gray&amp;quot; areas, which we didn&amp;#39;t come to a firm team agreement on, there were inconsistent practices used by the developers which led to rot, particularly between the controller (or code-behind) and application services layer.&lt;/p&gt;
&lt;p&gt;So while we can discuss which approach is best or most effective, I think the greater concern is for the team to pick a direction, to stick to it, and to diverge or refactor as a team decision.&lt;/p&gt;
&lt;p&gt;But as I said, I don&amp;#39;t want to leave it at &amp;quot;it depends.&amp;quot; ;) &amp;nbsp;So after reading everyone&amp;#39;s ideas here and contemplating further on past projects and the ideas I recently put forth in &lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2010/03/05/better-application-services-and-cqs-with-s-arp-architecture-1-0-q3-2009.aspx"&gt;http://devlicio.us/blogs/billy_mccafferty/archive/2010/03/05/better-application-services-and-cqs-with-s-arp-architecture-1-0-q3-2009.aspx&lt;/a&gt;, here are a couple of ideas that I&amp;#39;ve been trying to adhere to (and/or will be adhering to on my next project). &amp;nbsp;The layers referenced below may be viewed in a &lt;a href="http://github.com/codai/Sharp-Architecture/raw/master/docs/ProjectArchitecture.PNG"&gt;package diagram here&lt;/a&gt;&amp;nbsp;for better clarity of what&amp;#39;s discussed.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Usually, one object should be used to populate a form while a second should be used to collect data from a form. &amp;nbsp;The former would contain information for&amp;nbsp;default/selected values along with&amp;nbsp;drop-down values, for examples, while the latter would encapsulate which options were selected when the user submitted the form. &amp;nbsp;Personally, I use a &amp;lt;EnityName&amp;gt;FormViewModel to populate the form and use an entity to collect from the form...but I see it equally valid to instead use an intermediary DTO to collect from the form if further separation is warranted. &amp;nbsp;Additionally, I feel it is OK for a FormViewModel object to have references to domain objects to make it easier to transfer data to the form for input population. &amp;nbsp;This is an arguable point and would be just as valid to only pass DTOs in the view model for a cleaner separation between view and domain, with the acceptance that there will be more objects and maintenance that comes with that approach.&lt;/li&gt;
&lt;li&gt;I believe that view models should be maintained either in the application services layer OR in a separate ViewModels class library if a project team feels the additional separation is warranted (although I can&amp;#39;t envision much reason to do so). &amp;nbsp;(I&amp;#39;ll be modifying my sample project post to reflect the former.) &amp;nbsp;Either way, they should not be maintained in .Core to keep a very clean separation from domain concerns. &amp;nbsp;While the name &amp;quot;ViewModel&amp;quot; implies that they are a view concern, they only describe &lt;i&gt;what&lt;/i&gt;&amp;nbsp;should be displayed, not &lt;i&gt;how&lt;/i&gt; it should be displayed. &amp;nbsp;Accordingly, there is still a very clear separation of concerns between the view layer (which the user interacts with) and the view model classes. &amp;nbsp;Furthermore, the application services expose methods which should be usable by any type of client. &amp;nbsp;In line with this, a ViewModel or FormViewModel class does not impart a particular mechanism for the implementation of the view layer - again, it only describes &lt;i&gt;what&lt;/i&gt;&amp;nbsp;data should be available to the view. &amp;nbsp;So even if view model classes are maintained in app services, this does not preclude there use by various client types; e.g., they could be equally consumed by ASP.NET, ASP.NET MVC, Spark, SilverLight, etc. &amp;nbsp;(The only caveat is that if the view model classes maintain references to domain objects, then it will be more difficult to expose them for serialization via web services and WCF. Accordingly, a project team should firmly decide if ViewModels may contain references to domain objects.)&lt;/li&gt;
&lt;li&gt;I believe that DTOs should be maintained in a separate assembly and have no reference to .Core or be maintained in .Core if warranted with the acceptance of decreased &amp;quot;shareability.&amp;quot; &amp;nbsp;DTOs should almost always be serializable; i.e., transferable via web services and WCF. &amp;nbsp;This allows the DTOs class library to be easily shared with a consumer of web services which may expose these objects, for easier deserialization. &amp;nbsp;Some of the motivations for deciding when to use DTOs would be to provide a clean separation between the view and app services (if a project team feels its warranted), to expose data to web service consumers, and to better enable Command/Query Separation.&lt;/li&gt;
&lt;li&gt;Repositories may return Entities, Value Objects or DTOs, in addition to other &amp;quot;basic&amp;quot; types; e.g., primitives, arrays, etc. &amp;nbsp;Ideally, repositories would not be used by domain objects (even via their interfaces) and would have their interfaces maintained in the application services layer. &amp;nbsp;If the repository interfaces are maintained in .Core, then this forces DTOs to be maintained in .Core as well with the possibility that domain references may &amp;quot;leak&amp;quot; into DTOs. &amp;nbsp;If the repository interfaces are maintained in .ApplicationServices, then DTOs may be maintained in a cleanly separated library without worrying about the line between DTOs and domain objects being grayed. &amp;nbsp;But data interfaces (and DTOs) may be maintained in .Core if the team feels the simplification is warranted.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With all this said, I still think the most important decision is deciding a path to take for your project, having the project team agree upon it, and maintaining the discipline to stay on course.&lt;/p&gt;
&lt;p&gt;Your feedback on these ideas is most welcome!&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=55672" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Architecture/default.aspx" /><category term="DeepThoughtsByJackHandey" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/DeepThoughtsByJackHandey/default.aspx" /></entry><entry><title>Better Application Services and CQS using S#arp Architecture 1.0 Q3 2009</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2010/03/05/better-application-services-and-cqs-with-s-arp-architecture-1-0-q3-2009.aspx" /><link rel="enclosure" type="application/octet-stream" length="2841490" href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.00.05.56.53/BetterAppServices.rar" /><id>/blogs/billy_mccafferty/archive/2010/03/05/better-application-services-and-cqs-with-s-arp-architecture-1-0-q3-2009.aspx</id><published>2010-03-05T18:47:00Z</published><updated>2010-03-05T18:47:00Z</updated><content type="html">&lt;p&gt;&lt;i&gt;Updated 2010.03.09 to reflect small modifications that were decided through subsequent discussions on S#arp forum and other DDD posts.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Obviously, &lt;a href="http://wiki.sharparchitecture.net/"&gt;S#arp Architecture&lt;/a&gt; is the bee&amp;#39;s knees when it comes to developing ASP.NET MVC applications. ;) &amp;nbsp;But as a project evolves and gets larger, &amp;quot;out of the box&amp;quot; S#arp Architecture 1.0 guidance runs into a few pain points. &amp;nbsp;Particularly, there&amp;#39;s poor use of the &lt;a href="http://martinfowler.com/eaaCatalog/serviceLayer.html"&gt;application services&lt;/a&gt; layer, the separation between controllers and application services is not very clear, entity listing pages become performance bottlenecks as the domain model gets sizable, there is no &lt;a href="http://devlicio.us/blogs/casey/archive/2009/02/12/ddd-command-query-separation-as-an-architectural-concept.aspx"&gt;command/query separation (CQS)&lt;/a&gt;&amp;nbsp;&amp;quot;out of the box&amp;quot;, and unit tests require re-occurring&amp;nbsp;maintenance to deal with changes in the number of constructor parameters to controllers and application services. &amp;nbsp;While the amicable and adroit&amp;nbsp;&lt;a href="http://weblogs.asp.net/alecwhittington/default.aspx"&gt;Alec Whittington&lt;/a&gt;&amp;nbsp;(who is taking over the lead role from me on S#arp Architecture) is hard at work upgrading S#arp Architecture to accommodate recent dependency upgrades and accommodating ASP.NET MVC 2, I wanted to take a stab at resolving some of the architectural issues that I&amp;#39;ve run into, on S#arp projects over the past year.&lt;/p&gt;
&lt;p&gt;
        I&amp;#39;ve developed and included a sample project, built on S#arp Architecture 1.0 Q3 2009, for the following key reasons:
        &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;To resolve some &amp;quot;pain points&amp;quot; that develop as S#arp projects grow to large sizes,&lt;/li&gt;
&lt;li&gt;To demonstrate better use of the application services layer,&lt;/li&gt;
&lt;li&gt;To demonstrate better command/query separation of the entity listing pages for dramatically 
            better performance as the domain model grows,&lt;/li&gt;
&lt;li&gt;To create an architectural spike for a new project I&amp;#39;m working on, and&lt;/li&gt;
&lt;li&gt;To collect feedback from the S#arp community to determine if this is an 
            appropriate architectural direction for the next release of S#arp Architecture.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
        Please post any feedback and/or suggestions you may have in the comments below or, more preferably, to the S#arp Architecture forums at 
        &lt;a href="http://groups.google.com/group/sharp-architecture"&gt;http://groups.google.com/group/sharp-architecture&lt;/a&gt;.&lt;/p&gt;
&lt;hr /&gt;
&lt;h3&gt;Setup instructions&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Unzip the sample project to a BetterAppServices folder&lt;/li&gt;
&lt;li&gt;Create a new database called BetterAppServices&lt;/li&gt;
&lt;li&gt;Using SQL Enterprise Manager, run:
	&lt;ol&gt;
&lt;li&gt;/BetterAppServices/db/Schema/CreateBetterAppServicesDb_ChangesWillBeLost.sql&lt;/li&gt;
&lt;li&gt;/BetterAppServices/db/StoredProcedures/CreateGetProductCategorySummaries.sql&lt;/li&gt;
&lt;li&gt;/BetterAppServices/db/StoredProcedures/CreateGetProductSummaries.sql&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Open /BetterAppServices/BetterAppServices.sln with VS 2008&lt;/li&gt;
&lt;li&gt;In VS 2008, open BetterAppServices.Web/NHibernate.config and change the connection string to point to your BetterAppServices database&lt;/li&gt;
&lt;li&gt;Right click the BetterAppServices.Web project and &amp;quot;Set as StartUp Project&amp;quot;&lt;/li&gt;
&lt;li&gt;Run (F5) the project to see everything in action.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Changes from &amp;quot;out of the box&amp;quot; S#arp Architecture 1.0 Projects&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;
        This project is more of an architectural spike more than anything else at the moment.  
        Accordingly, the CRUD scaffolding generator has been removed and non-essential unit tests 
        have been removed to focus on the architecture itself. &amp;nbsp;Many of the changes will be 
        incorporated into the S#arp CRUD scaffolding generator; this will either be available in the next
        release, or will be provided as an add-on, as this new approach &lt;i&gt;does&lt;/i&gt; add complexity and 
        introduces a major breaking change to existing 1.0 projects.
    &lt;/p&gt;
&lt;p&gt;
        Major changes include:
        &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
                /db Folder Changes
                
&lt;ul&gt;
&lt;li&gt;Added /db/Schema/CreateBetterAppServicesDb_ChangesWillBeLost.sql.  
                    This gets auto-regenerated when unit tests are run.  The motivation was to have the DB 
                    schema automatically maintained while developing.&lt;/li&gt;
&lt;li&gt;Added /db/StoredProcedures/CreateGet&lt;i&gt;EntityNamePlural&lt;/i&gt;Summaries.sql.  These SPs provide 
                    command/query separation for the entity listing (Index.aspx) pages, which frequently became a 
                    performance bottleneck.
                    &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
                Cross-Project Changes
                
&lt;ul&gt;
&lt;li&gt;&lt;span style="text-decoration:line-through;"&gt;Moved /BetterAppServices.Core/DataInterfaces/* to /BetterAppServices.ApplicationServices/DataInterfaces/.  
                    &lt;strong&gt;This is the only major, possible breaking change for existing applications.&lt;/strong&gt;  This was done to further 
                    remove the potential of domain objects using data repositories directly, and to allow the repositories 
                    to return objects from a new Dtos project for command/query separation, among other benefits (dicussed below).&lt;/span&gt;&amp;nbsp;&amp;nbsp;Decided not to do this to support domain services which may require the use of repositories and to make upgrading from previous version much simpler in some cases. &amp;nbsp;Having the interfaces in .Core doesn&amp;#39;t necessitate that domain objects use them; in fact, my rule of thumb is for all domain objects to avoid the use of repositories unless an exceptive case exists.&lt;/li&gt;
&lt;li&gt;&lt;span style="text-decoration:line-through;"&gt;Replaced all uses of &amp;quot;using BetterAppServices.Core.DataInterfaces;&amp;quot; to 
                    &amp;quot;using BetterAppServices.ApplicationServices.DataInterfaces;&amp;quot; to support the above mentioned change.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Replaced all uses of IRepository&lt;i&gt;EntityName&lt;/i&gt; with I&lt;i&gt;EntityName&lt;/i&gt;Repository.  Be default, all 
                    entities now have an explicit repository.  This avoids the need to manually change from the generic to 
                    the explicit, when the need arose, in unit tests and in constructors; which was frequently occurring.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
                /BetterAppServices.Core/QueryDtos (/BetterAppServices.Dtos)
&lt;ul&gt;
&lt;li&gt;&lt;span style="text-decoration:line-through;"&gt;Added a Dtos class library to provide an appropriate location for View Models and DTOs.  While this project 
                    currently has a dependency on BetterAppServices.Core, so that the View Models can contain references to 
                    domain objects, this dependency could be severed for better separation between the view and the domain. 
                    The caveat is that much more work would be required maintaining a more complete DTO layer and transferring 
                    data via DTOs in all of the CRUD pages.&lt;/span&gt;&amp;nbsp;&amp;nbsp;Decided that a separate assembly was overkill. &amp;nbsp;Consequently, added this namespace to contain query DTOs for transferring results of &amp;quot;report&amp;quot; queries into objects. &amp;nbsp;With this example project, only the listing page uses &amp;quot;pure&amp;quot; DTOs 
                    for much better performance.  The other CRUD pages still communicate directly with domain objects to keep 
                    things much simpler. This is something that can be argued either way and it should depend on the 
                    project needs to determine if a more separated approach is warranted.&lt;/li&gt;
&lt;li&gt;Added&amp;nbsp;&lt;i&gt;EntityName&lt;/i&gt;Dto.cs to act as a summary object to be bound to results from stored procedures (e.g., the entity listing pages), or other DTO needs.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;/BetterAppServices.ApplicationServices/ViewModels&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Added this namespace and&amp;nbsp;&lt;i&gt;EntityName&lt;/i&gt;FormViewModel.cs to hold data related to adding and updating the &lt;i&gt;EntityName&lt;/i&gt;.  
                    This object gets populated and passed to the entity form pages, accordingly.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
                /BetterAppServices.ApplicationServices
                
&lt;ul&gt;
&lt;li&gt;Added reference to BetterAppServices.Dtos so that app services can return DTOs.&lt;/li&gt;
&lt;li&gt;Added &lt;i&gt;EntityName&lt;/i&gt;ManagementService.cs.  This app service class removes the CRUD logic 
                    from the controllers, makes the logic more reusable, and creates an appropriate class to add 
                    additional application service logic to.&lt;/li&gt;
&lt;li&gt;Added I&lt;i&gt;EntityName&lt;/i&gt;ManagementService.cs.  This app service interface makes unit testing more 
                    maintainable because you can mock the service interface and not worry about when the concrete class&amp;#39; 
                    constructor arguments change.&lt;/li&gt;
&lt;li&gt;Added /DataInterfaces/I&lt;i&gt;EntityName&lt;/i&gt;Repository.cs.  As described above, every entity now has 
                    an explicit repository interface to avoid changes down the road when custom interfaces would inevitably 
                    be introduced.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
                /BetterAppServices.Data
                
&lt;ul&gt;
&lt;li&gt;Added /BetterAppServices.Data/&lt;i&gt;EntityName&lt;/i&gt;Repository.cs to implement the associated interface. 
                    An important thing to note is that method provided invokes a stored procedure to act as a reporting means in 
                    line with command/query separation.  This is useful for entity listing pages which would frequently, previously, 
                    end up loading a huge portion of the domain model to show summary information.&lt;/li&gt;
&lt;li&gt;Added /BetterAppServices.Data/NamedQuery/Get&lt;i&gt;EntityName&lt;/i&gt;Summaries.hbm.xml and set its compile action to 
                    &amp;quot;Embedded Resource.&amp;quot;  This provides the &amp;quot;short cut&amp;quot; for invoking the stored procedure.  It could easily be modified 
                    to accept paging parameters.&lt;/li&gt;
&lt;li&gt;Added reference to BetterAppServices.Dtos so that repositories can return DTOs for better command/query 
                    separation.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
                /BetterAppServices.Web
                
&lt;ul&gt;
&lt;li&gt;Added reference to BetterAppServices.Dtos to be able show DTOs on web pages.&lt;/li&gt;
&lt;li&gt;Changed ComponentRegistrar.AddCustomRepositoriesTo &amp;quot;BetterAppServices.Core&amp;quot; to 
                    &amp;quot;BetterAppServices.ApplicationServices&amp;quot; to reflect the new location of the data repository interfaces.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
                /BetterAppServices.Web.Controllers
                
&lt;ul&gt;
&lt;li&gt;Added reference to BetterAppServices.Dtos.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
                /BetterAppServices.Tests
                
&lt;ul&gt;
&lt;li&gt;Modified MappingIntegrationTests.CanGenerateDatabaseSchema to save DB schema to 
                    /db/Schema/CreateBetterAppServicesDb_ChangesWillBeLost.sql every time the unit test is run.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
        
        To reiterate, many of the changes above can be incorporated into a CRUD scaffolding generator; the focus with this example 
        project is on providing an archtectural spike of the proposed architectural revisions.&lt;/p&gt;
&lt;p&gt;Even if you don&amp;#39;t use S#arp Archtiecture, this sample project should serve as a good example of using application services and basic use of command/query separation (CQS). &amp;nbsp;Although the CQS in the sample project could be taken much further, I felt that the sample provides a good balance between practical maintainability and a more austere separation of concerns.&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=55653" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Architecture/default.aspx" /><category term="S#arp Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Architecture/default.aspx" /><category term="DDD" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/DDD/default.aspx" /></entry><entry><title>Message-Based Systems for Maintainable, Asynchronous Development</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2010/03/01/message-based-systems-for-maintainable-asynchronous-development.aspx" /><id>/blogs/billy_mccafferty/archive/2010/03/01/message-based-systems-for-maintainable-asynchronous-development.aspx</id><published>2010-03-02T04:22:00Z</published><updated>2010-03-02T04:22:00Z</updated><content type="html">&lt;p&gt;&lt;strong&gt;Preface&lt;/strong&gt; (you know it&amp;rsquo;s good if there&amp;#39;s a preface)&lt;/p&gt;
&lt;p&gt;In &lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2010/02/03/software-architectural-approaches-for-robotics.aspx"&gt;Architectural Paradigms of Robotic Control&lt;/a&gt;, a number of architectures were reviewed including deliberative, reactive, and hybrid architectures. &amp;nbsp;Each of these exhibit a clean separation of concerns with layering and encapsulation of defined behaviors. &amp;nbsp;When implemented, the various capabilities, such as planners and mobility controllers, are encapsulated into discrete components for better reusability and maintainability. &amp;nbsp;A pivotal aspect &lt;i&gt;not&lt;/i&gt; discussed in the previous article is how the various system layers and components communicate with each other, such as reporting sensor feedback and sending commands to actuator controllers. &amp;nbsp;Effectively resolving this communication challenge is not only important to robotic systems but to many other industries and domains for the successful integration of disparate applications.&lt;/p&gt;
&lt;p&gt;To give credit where credit is due, this article pulls quite heavily&amp;nbsp;from the patterns, taxonomy, and best practices presented in&amp;nbsp;&lt;a href="http://www.amazon.com/Enterprise-Integration-Patterns-Designing-Deploying/dp/0321200683"&gt;Enterprise Integration Patterns&lt;/a&gt;&amp;nbsp;(Hohpe, 2003). &amp;nbsp;This well organized book is chock full of hard learned lessons and solid guidelines for developing maintainable message-based systems. &amp;nbsp;This article should not be seen as an adequate replacement for that book (it&amp;rsquo;s more like cliff notes with a spackling of robotics bias); indeed,&amp;nbsp;&lt;a href="http://www.amazon.com/Enterprise-Integration-Patterns-Designing-Deploying/dp/0321200683"&gt;Enterprise Integration Patterns&lt;/a&gt;&amp;nbsp;should have a prominent place on your bookshelf if you&amp;#39;re developing message-based systems&amp;nbsp;&amp;ndash;&amp;nbsp;so read this post and then browse&amp;nbsp;&lt;a href="http://www.eaipatterns.com/"&gt;http://www.eaipatterns.com/&lt;/a&gt;&amp;nbsp;while waiting for your copy to arrive to delve deeper.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A Need for Message-Based Systems&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A few industries in particular, such as finance, healthcare and robotics, are demanding integration of an intimidating number of separate technologies that may be spread across computers, networks, and/or built upon a variety of technological platforms. &amp;nbsp;Not only is this integration tricky, it can come with a significant cost to performance and maintainability if not implemented correctly. &amp;nbsp;Accordingly, a solution is needed which facilitates loosely coupled integration while accommodating the performance demands of the task at hand. &amp;nbsp;Taking a message-oriented approach to inter-application communications is one such way to accommodate these demands in a maintainable manner without sacrificing performance. &amp;nbsp;This article gives an introduction to developing message-based systems using messaging middleware, describes taxonomy for discussing messaging topics and patterns, and includes a number of best practices.&lt;/p&gt;
&lt;p&gt;Before delving further, it&amp;#39;s important to clarify a few terms that will be used frequently:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;i&gt;Messaging Middleware&lt;/i&gt; (aka, a message bus): &amp;nbsp;a 3rd party application which provides messaging infrastructure and capabilities (e.g., &lt;a href="http://www.microsoft.com/windowsserver2003/technologies/msmq/default.mspx"&gt;MSMQ&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/bb648752.aspx"&gt;MS Concurrency and Coordination Runtime&lt;/a&gt; (CCR), &lt;a href="http://www.ros.org/wiki/"&gt;Robot Operating System&lt;/a&gt; (ROS)),&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Component&lt;/i&gt;: &amp;nbsp;a stand-alone application or piece of executable code which communicates with the messaging middleware,&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Message-based system&lt;/i&gt;: &amp;nbsp;the entirety of the system including all integrated components and the messaging middleware.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As stated, messaging provides one means of facilitating inter-component communications. &amp;nbsp;But as with any design approach, the project requirements must be carefully considered to determine if messaging is the appropriate mechanism for integration. &amp;nbsp;While messaging is robust and facilitates integration, it also adds complexity and indirection. &amp;nbsp;So before deciding to use messaging as the means of integration, consider all component integration options including (Hohpe, 2003):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;i&gt;File Transfer&lt;/i&gt;: &amp;nbsp;wherein a component produces files of shared data which other components consume,&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Shared Database&lt;/i&gt;: &amp;nbsp;each component stores and retrieves data from a common database,&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Remote Procedure Invocation&lt;/i&gt;: &amp;nbsp;each component exposes specific procedures to be invoked remotely for exposing behavior and exchanging data,&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Messaging&lt;/i&gt;: &amp;nbsp;each component connects to a common messaging system, using messages to invoke behavior and exchange data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Determining which integration approach is most suitable to your project&amp;#39;s needs is beyond the scope of this article, focusing instead specifically on messaging. &amp;nbsp;In turn, we&amp;#39;ll review important elements of developing a message-based system including: &amp;nbsp;message channels, messages, message routers, and message endpoints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Message Channels&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When a component sends information to another component in a message-based system, it adds the information to a &lt;i&gt;message channel&lt;/i&gt;. &amp;nbsp;The receiving component then retrieves the information from the message channel. &amp;nbsp;Different channels are created for each kind of data to be carried; having a separate channel for each datatype better enables receiving components to know what kind of data will be retrieved from a given channel. &amp;nbsp;For using a channel, each channel is addressable for sending and retrieving messages to/from them. &amp;nbsp;How a channel is addressed varies depending on the messaging middleware being leveraged, but it&amp;#39;s usually a port number or a unique string identifier. &amp;nbsp;As a good practice for keeping channels organized, if string identifiers are available, a hierarchical naming convention may be employed to label channels by type and name; e.g., a channel carrying laser scans might be called &amp;quot;Perception/LaserScans.&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;img style="border:0;float:right;margin-top:6px;margin-bottom:6px;margin-left:2px;margin-right:2px;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/ChannelTypes.png" border="0" alt="" /&gt;There are two basic kinds of message channels:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;i&gt;Point-to-Point Channels&lt;/i&gt; (aka - client/server style): &amp;nbsp;routes messages for components to talk directly with other components; e.g., a remote procedure call to another component. &amp;nbsp;A message over a point-to-point channel only has a single receiver; so while the sender may not necessarily know who the receiver is, the sender can rest assured that the message will only be received by one receiver &amp;ndash; it&amp;rsquo;s a &lt;a href="http://en.wikipedia.org/wiki/FIFO"&gt;FIFO&lt;/a&gt; queue.&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Publish-Subscribe Channels&lt;/i&gt; (aka - broadcast): &amp;nbsp;routes messages for components to publish data and an arbitrary number of components to subscribe to that data. &amp;nbsp;A copy of the message is generated for each subscriber on the channel.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In addition to channels intended to carry information among components, it is a good practice to setup an &lt;i&gt;invalid message channel&lt;/i&gt;&amp;nbsp;that bad-formed or unreadable messages may be forwarded to for logging and to assist with debugging.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Messages&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;With a function call, a simple parameter or object reference may be passed and retrieved by the invoked method, sharing the same memory space. &amp;nbsp;But when passing data between two processes with separate memory spaces, the data must be packaged into a &amp;quot;message&amp;quot; adhering to an agreed upon format which the receiver will be able to disassemble and understand. &amp;nbsp;The sender of the message passes the message via a message channel. &amp;nbsp;The receiver retrieves the message from the message channel and transforms the message into internal data structures appropriate for the task at hand.&lt;/p&gt;
&lt;p&gt;A message is made up of two parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;i&gt;Header&lt;/i&gt;: &amp;nbsp;describes the data being transmitted and details concerning the message itself; e.g., origin, timestamp information, message expiration (if content is time-sensitive), message identifier, correlation identifier, return address, etc., and&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Body&lt;/i&gt;: &amp;nbsp;the data content that the receiver is looking to use.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When sending a message, the sender intends for the message to be used, or responded to, in a particular way. &amp;nbsp;The intention of the message may be described as being one of the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;i&gt;Command Message&lt;/i&gt;: &amp;nbsp;invokes a procedure in another application,&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Document Message&lt;/i&gt;: &amp;nbsp;passes a set of data to another application,&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Event Message&lt;/i&gt;: &amp;nbsp;notifies another application of a change in state, and&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Request-Reply&lt;/i&gt;: &amp;nbsp;requests a reply from another application.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Event messages deserve a bit more discussion. &amp;nbsp;In its simplest form, an event message would simply be informational, letting subscribers know that an event has occurred; e.g., a new laser scan is available. &amp;nbsp;If subscribers would like details concerning the event, they would send a request-reply to the sender of the event to provide further details; e.g., the laser scan details. &amp;nbsp;Alternatively, the event could be a document &lt;i&gt;as&lt;/i&gt;&amp;nbsp;an event to inform subscribers that an event has occurred along with the details of that event; e.g., a new laser scan is available with laser scan details included. &amp;nbsp;The size of the event details and the rapidity in which the event occurs should be considered when deciding between publishing simple event messages and document messages &lt;i&gt;as&lt;/i&gt; events.&lt;/p&gt;
&lt;p&gt;Request-reply messages could also use a bit more describing. &amp;nbsp;A request-reply is usually implemented as two point-to-point channels. &amp;nbsp;The first channel delivers the request as a command message, while the second carries the reply back to the requestor as a document message. &amp;nbsp;To keep the replying component more loosely coupled and reusable, the requestor should include a &lt;i&gt;return address&lt;/i&gt;&amp;nbsp;indicating the channel that the replier should use to publish the reply. &amp;nbsp;After receiving the reply, a challenge for the requestor is to then correlate the reply to the original request. &amp;nbsp;If the requestor is sending a number of requests in succession, it will likely be difficult to keep clear &amp;ndash; if it matters &amp;ndash; which request a reply is associated with. &amp;nbsp;To resolve this, every request may include a unique &lt;i&gt;message identifier&lt;/i&gt;&amp;nbsp;that the replier would then include as a &lt;i&gt;correlation identifier&lt;/i&gt;. &amp;nbsp;(A message could have both a message Id and a correlation Id.) &amp;nbsp;The requestor uses the correlation identifier to &amp;ldquo;jog its memory&amp;rdquo; concerning which request the response is for. &amp;nbsp;But frequently, a request-reply is in context of a particular domain object, such as a terrain map or a bank transaction; but the correlation Id doesn&amp;rsquo;t include such information. &amp;nbsp;To assist, the requestor can maintain a mapping (e.g., hashtable) between message Ids and relevant domain object Ids which are related to the original request. &amp;nbsp;When the reply is received, the mapping may be used to load the appropriate domain objects and take further action, accordingly.&lt;/p&gt;
&lt;p&gt;Obviously, it is important that the senders and receivers of a message system agree upon the format that messages will take for clear interoperability, better reusability of components, and extensibility of the system. &amp;nbsp;Consequently, a &lt;i&gt;canonical data model&lt;/i&gt;&amp;nbsp;should be well defined that all applications will adhere to. &amp;nbsp;The canonical data model does not dictate how each application&amp;#39;s domain model must be structured, only how each application must format data within messages. &amp;nbsp;&lt;i&gt;Message translators&lt;/i&gt;&amp;nbsp;are developed to convert the sending application&amp;#39;s domain model into the canonical data model before sending a message; receivers of messages then use their own message translators to translate the message into their own domain. &amp;nbsp;This mechanism allows applications built on completely different technologies (e.g., C#, Lisp, and C++) to communicate with each other and exchange data. &amp;nbsp;Many off the shelf messaging systems define their canonical data model which must be adhered to. &amp;nbsp;For example, the &lt;a href="http://www.ros.org/wiki/"&gt;Robot Operating System&lt;/a&gt; (ROS), which we&amp;#39;ll looking at in more detail in subsequent posts, defines their canonical model at &lt;a href="http://www.ros.org/wiki/msg"&gt;http://www.ros.org/wiki/msg&lt;/a&gt;. &amp;nbsp;But the canonical data model need not be limited to defining the types of primitives available and how to include them in messages.&lt;/p&gt;
&lt;p&gt;Domain-specific canonical data models may augment message formatting rules, adding semantic meaning to the data within a message. &amp;nbsp;For example, the &lt;a href="http://en.wikipedia.org/wiki/JAUS"&gt;Joint Architecture For Unmanned Systems (JAUS)&lt;/a&gt; is a set of message guidelines for the domain of unmanned systems, such as autonomous vehicles. &amp;nbsp;The JAUS guidelines provide domain specific rules for communicating data, such as propulsion and braking commands, sensor events, pose and location information, etc. &amp;nbsp;To demonstrate, JAUS message types include (Siciliano, 2008):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;i&gt;Command&lt;/i&gt;: &amp;nbsp;initiate mode changes or actions,&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Query&lt;/i&gt;: &amp;nbsp;used to solicit information from a component,&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Inform&lt;/i&gt;: &amp;nbsp;response to a query,&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Event set up&lt;/i&gt;: &amp;nbsp;passes parameters to set up an event, and&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Event notification&lt;/i&gt;: &amp;nbsp;sent when the event happens.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A challenge in dealing with canonical data models is how to handle changes to the model. &amp;nbsp;In order to support backwards compatibility of existing components when the canonical data model changes, new message channels could be created to carry the messages adhering to the model; e.g., &amp;quot;Perception/LaserScans_V1&amp;quot; and &amp;quot;Perception/LaserScans_V2.&amp;quot; &amp;nbsp;Alternatively, the existing channels could continue to be leveraged to carry messages adhering to different version of the canonical data model. &amp;nbsp;To do so, the message, within its header, would include a &lt;i&gt;format indicator&lt;/i&gt;, such as a version number or format document (e.g., DTD) reference. &amp;nbsp;But if a sender knows that receivers of a particular message are mixed in what format is being used, a component would need to send two messages, one for each version of the canonical data model. &amp;nbsp;Certainly, this is an important consideration when deciding which components should (or even can) be upgraded to newer formats, and in what order.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Message Routers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;While the heavy lifting of the message routing is handled by the messaging middleware itself, there are times when it is useful to augment the middleware with custom message routers to support unique scenarios.&lt;/p&gt;
&lt;p&gt;&lt;img style="border:0;float:right;margin-left:2px;margin-right:2px;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/MessageRouter.png" border="0" alt="" /&gt;Suppose the destination of a message may change based on the number of messages that have already passed over a channel. &amp;nbsp;In this scenario, the sender of a message may not know how many messages have been passed over a channel since other senders may have been publishing messages on the same channel. &amp;nbsp;Consequently, a message router may subscribe to the channel to determine where each message should be forwarded to, based on the described business rules. &amp;nbsp;Once the destination is determined, the router would then place the message on a subsequent channel to be delivered to the appropriate destination. &amp;nbsp;This intermediary routing is described as &lt;i&gt;predictive routing&lt;/i&gt;&amp;nbsp;as the message router is aware of every possible destination and the rules for routing, accordingly. &amp;nbsp;If the routing is based on content within the message itself, such as threshold values, then the custom router is known as a &lt;i&gt;content-based router&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;A drawback to using message routers is that if the routing rules change frequently, the message router will need to be modified just as often. &amp;nbsp;To help remedy this, if the rules are expected to change frequently, configurable routing rules (e.g., via XML) could be employed to enable easier management of routing rules.&lt;/p&gt;
&lt;p&gt;&lt;img style="border:0;float:right;margin-left:2px;margin-right:2px;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/MessageFilter.png" border="0" alt="" /&gt;Let&amp;#39;s now consider another scenario wherein it&amp;#39;s left up to the subscribers to determine which messages they&amp;#39;re interested in; i.e., subscribers will be responsible for filtering out the messages they&amp;#39;re uninterested in. &amp;nbsp;In this &lt;i&gt;reactive routing&lt;/i&gt; scenario, each subscriber would provide a respective &lt;i&gt;message filter&lt;/i&gt;&amp;nbsp;which is similar to a message router, but simply forwards, or does not forward, a message onto a subsequent channel that the destination subscriber is listening to. &amp;nbsp;Frequently, message filters decide to forward, or not forward, based on content in the message itself; e.g., only forwarding orders that have a coupon included. &amp;nbsp;While being similar to a message router in basic functionality, a message filter only has one possible channel to forward the message onto.&lt;/p&gt;
&lt;p&gt;Deciding between predictive and reactive filtering must take into account a number of considerations. &amp;nbsp;Is the message content sensitive? &amp;nbsp;Do you need to minimize network traffic? &amp;nbsp;Do you need to be able to add and remove subscribers easily? &amp;nbsp;Is the predictive router becoming a bottleneck of message dissemination? &amp;nbsp;For further guidance on selecting among routing options, see (Hohpe, 2003), ppg. 241-242.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Message Endpoints&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Each messaging middleware option (e.g., CCR, ROS) has unique requirements for communicating with it. &amp;nbsp;Each has its own API, its own means of addressing channels, and its own rules for packaging messages. &amp;nbsp;Ideally, the components of the system should not be aware of the specifics of communicating with the messaging middleware. &amp;nbsp;Furthermore, while unlikely to occur, the middleware should be able to be replaced with another, requiring little, if any, changes to the components. &amp;nbsp;Accordingly, the components must be loosely coupled to the messaging middleware. &amp;nbsp;&lt;i&gt;Message endpoints&lt;/i&gt;&amp;nbsp;provide the bridge between the domain of each component and the API of the messaging middleware.&lt;/p&gt;
&lt;p&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/MessageEndpoints.png" style="border:0;float:right;margin-left:2px;margin-right:2px;" alt="" /&gt;Message endpoints are similar in nature to &lt;a href="http://domaindrivendesign.org/node/123"&gt;repositories&lt;/a&gt; when communicating with a database. &amp;nbsp;Repositories encapsulate the code required to communicate with a database to store and retrieve data while being able to convert information from the database into domain objects. &amp;nbsp;If the database changes, or if the mechanism for database communication changes (e.g., ADO.NET to NHibernate), then, ideally, only the repositories are affected. &amp;nbsp;The rest of the application knows little about database communications outside of the repository interfaces. &amp;nbsp;Likewise, components of a message-based system should not be aware of messaging details outside of the message endpoint interfaces, which provide the means to send and receive data. &amp;nbsp;The message endpoint accepts a command or data, converts it into a message, and publishes it onto the correct channel. &amp;nbsp;Additionally, the message endpoint receives messages from a channel, converts the content into the domain of the component, and passes the domain objects to the component for further action. &amp;nbsp;Internally, the message endpoint implements a &lt;i&gt;message mapper&lt;/i&gt; to convert between the component domain objects and the canonical data model.&lt;/p&gt;
&lt;p&gt;While posting to a channel is rather straight forward, a message endpoint may receive a message by acting as a:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;i&gt;Polling Consumer&lt;/i&gt;: &amp;nbsp;wherein the receiver looks at a channel on a regular basis for new messages and/or as soon as it completes the processing of a previous message. &amp;nbsp;This frees the receiver from having to deal with messages as soon as they arrive on the channel in favor of dealing with messages went it&amp;rsquo;s ready and willing. &amp;nbsp;A consideration to keep in mind is that messages may queue up on a channel while waiting to be retrieved by the polling consumer. &amp;nbsp;Additionally, a polling consumer may take up threads and resources while polling a channel, even if the channel is empty.&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Event-Driven Consumer&lt;/i&gt;: &amp;nbsp;wherein the message is given to the receiver as soon as it arrives on the channel. &amp;nbsp;The benefits to this include avoiding messages queuing up while being able to process messages asynchronously. &amp;nbsp;But the receiver is no longer in control of the timing in which it processes messages and must handle messages as soon as they arrive on a channel.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Many messaging middleware solutions include support for transactions that the message endpoints may leverage, making the endpoints &lt;i&gt;transactional clients&lt;/i&gt;. &amp;nbsp;To illustrate the need for this, suppose the receiver of a request-reply command crashes just moments after the command message is consumed and removed from the channel. &amp;nbsp;When it recovers, the command message is lost and the sender will never receive a reply. &amp;nbsp;Using a transaction, the command message is not removed from the channel until the response is completed and sent. &amp;nbsp;Committing the transaction removes the command message from the channel and adds the reply document message to the reply channel.&lt;/p&gt;
&lt;p&gt;There are a few recommendations which should be considered when developing message endpoints. &amp;nbsp;In accordance with the &lt;a href="http://www.objectmentor.com/resources/articles/srp.pdf"&gt;SRP&lt;/a&gt;, message endpoints should be able to receive messages or send messages, but not both in the same message endpoint. &amp;nbsp;Furthermore, a message endpoint should only communicate with one message channel. &amp;nbsp;If a component needs to send a message on separate channels, it would leverage multiple message endpoints to do so. &amp;nbsp;If your components are developed in line with &lt;a href="http://www.infoq.com/minibooks/domain-driven-design-quickly"&gt;DDD&lt;/a&gt;, it would be each component&amp;#39;s &lt;a href="http://martinfowler.com/eaaCatalog/serviceLayer.html"&gt;application services layer&lt;/a&gt; which would communicate with the message endpoints, preferably via their interfaces instead of concrete instances. &amp;nbsp;This facilitates swapping out the message endpoints with mock objects for unit testing. &amp;nbsp;Leveraging &lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2008/10/30/refactoring-service-dependencies-to-separated-interface.aspx"&gt;separated interfaces&lt;/a&gt;&amp;nbsp;and &lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2009/11/09/dependency-injection-101.aspx"&gt;dependency injection&lt;/a&gt;&amp;nbsp;helps enable this approach. &amp;nbsp;While these guidelines introduce more objects and indirection, they are proven practices for increasing maintainability of the component and reusability of the message endpoints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Performance of Message Based Systems&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;One would likely be quick to assume that developing a message-based system has a huge cost to performance. &amp;nbsp;Domain objects are converted to messages, messages are passed over channels, routers and filters intercept and forward messages, and messages are converted back into domain objects...this sounds like a heck of a lot going on. &amp;nbsp;But because each component executes in its own thread or process, they need not wait for other components to complete their job before being able to move on to handling another message or task.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/SynchVsAsynchProcessing.png" border="0" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;In the figure above (adapted from&amp;nbsp;Hohpe, 2003, pg. 73), note that a sequential process requires that each message life cycle is completed in full before moving on to the next message. &amp;nbsp;But in an asynchronous, message-based system, each component can move onto a subsequent message just as soon as it has completed its part in the last one. &amp;nbsp;This greatly compensates for the extra overhead imparted by the messaging infrastructure.&lt;/p&gt;
&lt;p&gt;With that said, there are some component responsibilities which take a long time to complete and may impede the speed by which messages are processed. &amp;nbsp;For example, imagine a component which takes images from a web cam and extracts information such as human figures or road signs. &amp;nbsp;This is likely a time consuming process and would impact the turn around time in which the component could process subsequent messages. &amp;nbsp;To alleviate this bottleneck, multiple instances of the same component may subscribe to the same point-to-point channel. &amp;nbsp;Recall that a point-to-point channel ensures that each message only has a single receiver. &amp;nbsp;The instances of the component then become &lt;i&gt;competing consumers&lt;/i&gt; of each message. &amp;nbsp;So if one instance of the component is still processing an earlier message, another instance can grab the next message that arrives for concurrent processing. &amp;nbsp;Power in numbers!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Monitoring and Debugging&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Due to the widely asynchronous nature of message-based systems, attention must be given to monitoring and debugging techniques to observe system behavior and iron out problems.&lt;/p&gt;
&lt;p&gt;Logging message content is an invaluable measure towards getting a clear look at what communications are taking place. &amp;nbsp;To facilitate this, logging could be added directly into sending and receiving components, but logging message content should not be a concern of components; accordingly, using a monitoring utility to log such information is a cleaner separation of concerns. &amp;nbsp;Certainly a benefit of publish-subscribe channels is that a monitoring component may subscribe to all messages and log the information to a file or console window. &amp;nbsp;While it&amp;rsquo;s just as valuable to monitor messages on point-to-point channels, if a monitor were to consume a message over a point-to-point channel, the message would be noted as consumed and would no longer be available to the intended receiver. &amp;nbsp;Because such monitoring capability is so helpful in developing and debugging, many messaging middleware options include a &amp;quot;peek&amp;quot; option which allows a monitoring utility to review message contents on a point-to-point channel without actually consuming the message. &amp;nbsp;This capability should be taken into consideration when comparing messaging middleware alternatives.&lt;/p&gt;
&lt;p&gt;In addition to monitoring message content sent over channels, it&amp;rsquo;s also assistive to monitor active subscriptions to various channels to accurately determine which components are receiving which messages. &amp;nbsp;This capability is typically built into messaging middleware solutions and is very assistive during development.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s possible that even if a message is routed appropriately, the receiving component may not know what to do with the message due to an incorrectly formatted header or body content. &amp;nbsp;Invalid messages such as this should be forwarded to an &lt;i&gt;invalid message channel&lt;/i&gt; which an error logging utility would monitor and log, accordingly. &amp;nbsp;An invalid message channel is setup just like any other channel but with the intention of exposing such messages for debugging purposes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Closing Thoughts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Developing asynchronous, message-based systems requires a paradigm shift from the more traditional, synchronously executed applications that most people are familiar with. &amp;nbsp;It is not appropriate for every application domain and should be seen as one additional architectural option to consider when developing applications. &amp;nbsp;But in some domains, such as robotics or in the integration of disparate applications, this approach to development is absolutely pivotal in providing responsive behavior without sacrificing maintainability of the overall system. &amp;nbsp;Indeed, by splitting responsibilities into discrete components, loosely coupled to each other via messaging middleware, immensely complex problems can be broken down into understandable chunks while being flexible enough to accommodate changes to the underlying middleware or introduction of new components.&lt;/p&gt;
&lt;p&gt;In the next couple of posts, we&amp;#39;ll look at a checklist for developing message-based systems followed with examples in CCR and ROS.&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;br /&gt;&lt;a href="http://wiki.sharparchitecture.net"&gt;http://wiki.sharparchitecture.net&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;P.S., for S#arp fans, we&amp;#39;re trying to get another release together for the coming week...and this ain&amp;#39;t no quarterly release. ;)&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Hohpe, G., Woolf, B. &amp;nbsp;2003. &amp;nbsp;&lt;a href="http://www.amazon.com/Enterprise-Integration-Patterns-Designing-Deploying/dp/0321200683"&gt;Enterprise Integration Patterns: &amp;nbsp;Designing, Building, and Deploying Messaging Solutions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Siciliano, B., Khatib, O. &amp;nbsp;2008. &amp;nbsp;&lt;a href="http://www.amazon.com/Springer-Handbook-Robotics-Bruno-Siciliano/dp/354023957X"&gt;Springer Handbook of Robotics&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=55589" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Architecture/default.aspx" /><category term="Robotics" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Robotics/default.aspx" /><category term="Patterns" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Patterns/default.aspx" /></entry><entry><title>Architectural Paradigms of Robotic Control</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2010/02/03/software-architectural-approaches-for-robotics.aspx" /><id>/blogs/billy_mccafferty/archive/2010/02/03/software-architectural-approaches-for-robotics.aspx</id><published>2010-02-03T22:39:00Z</published><updated>2010-02-03T22:39:00Z</updated><content type="html">&lt;p&gt;&lt;strong&gt;Introducing a New Blog Series&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Rarely do I make new year&amp;#39;s resolution. &amp;nbsp;But I found myself pinky swearing myself a single resolution for this year: &amp;nbsp;write a series of tractable posts concerning the development of software for robotics. &amp;nbsp;Accordingly, I intend to introduce background, techniques, and examples with respect to the following topics and more:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Software Architectural Approaches for Robotics (e.g., Deliberative, Reactive, Hybrid)&lt;/li&gt;
&lt;li&gt;Software Design Methodologies (e.g., for Behavior Based Robotics, for Hierarchical)&lt;/li&gt;
&lt;li&gt;Integration Patterns (Message Channels, Publish/Subscriber, RPC)&lt;/li&gt;
&lt;li&gt;Machine Learning and Adaptation Techniques (Neural Nets, Genetic Algorithms, Fuzzy Logic)&lt;/li&gt;
&lt;li&gt;Planning Algorithms (Discrete Planning, Continuous Spaces, Motion Planning)&lt;/li&gt;
&lt;li&gt;Simultaneous Localization and Mapping Techniques (Kalman Filters, Particle Filters)&lt;/li&gt;
&lt;li&gt;Tools &amp;amp; Frameworks (Microsoft Robotics Studio, Robot Operating System)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&amp;#39;m writing this series of posts for two reasons. &amp;nbsp;The first, as always, is to bounce ideas off readers as a sanity check. &amp;nbsp;The second is to instill interest in readers in this rapidly evolving field. &amp;nbsp;The sophistication of software algorithms within robotics is beginning to lag behind the potential capabilities of available hardware. &amp;nbsp;Accordingly, if this series acts as motivational tool to get others involved in tackling the current algorithmic challenges involved in robotics, then all the better.&lt;/p&gt;
&lt;p&gt;To make this series more interesting and applicable to the development audience that reads &lt;a&gt;http://devlicio.us&lt;/a&gt;, I will attempt, when appropriate, to focus on patterns and general techniques that could possibly be applicable to scenarios outside of robotics. &amp;nbsp;Additionally, due to this series&amp;#39; focus on software, I&amp;#39;ll concentrate on leveraging simulators over physical, often expensive, robotics hardware to make it easier for readers to duplicate provided examples.&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;Introduction to Architectural Paradigms of Robotic Control&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;While &amp;quot;architecture&amp;quot; is likely one of the least definable terms in software development, it is unavoidably a topic which has one of the greatest impacts on the extensibility and maintainability of an application. &amp;nbsp;Indeed, a frequent cause of an application re-write is due to the architectural decisions that were made early in the project, or the lack thereof. &amp;nbsp;Certainly one of the difficulties in instituting proper architectural practices in a project lies in the fact that architectural decisions must carefully be decided at many different levels of project development. &amp;nbsp;Obviously, the architectural decisions at each level should be made in context of the project requirements in order to facilitate a proper balance of speed of development, scalability of development, and maintainability of the application in the future.&lt;/p&gt;
&lt;p&gt;To illustrate, consider a basic eCommerce site vs. a corporate financial management application which integrates with various third party tools. &amp;nbsp;There are two aspects of architecture which must be carefully considered. &amp;nbsp;The first is deciding which project contexts will require architectural consideration. &amp;nbsp;The second is defining the architectural approach to meet the needs of the given project context, and implementing any necessary prefactoring, accordingly.&lt;/p&gt;
&lt;p&gt;For a basic eCommerce site, the project contexts requiring architectural consideration would likely include: &amp;nbsp;appropriate separation between presentation and control, separation between control and the domain, separation between the domain and data access, and appropriate integration with a payment gateway. &amp;nbsp;Perhaps the &lt;a href="http://en.wikipedia.org/wiki/Active_record_pattern"&gt;active record pattern&lt;/a&gt; might be chosen as the data access mechanism. &amp;nbsp;If&amp;nbsp;&lt;a href="http://martinfowler.com/articles/injection.html"&gt;inversion of control&lt;/a&gt;&amp;nbsp;seems overkill for such a small application, perhaps direct communications to the payment gateway via the domain objects might be chosen as the means of integration. &amp;nbsp;Accordingly, &lt;a href="http://devlicious.com/blogs/billy_mccafferty/archive/2006/09/20/Planning-for-vs.-Reacting-to-Change.aspx"&gt;judicious prefactoring&lt;/a&gt; would suggest that an &lt;a href="http://www.agile-architecting.com/Architecture%20Spikes.pdf"&gt;architectural spike&lt;/a&gt; be created demonstrating appropriate use of the active record pattern with a selected tool along with an example of how and where integration with the payment gateway would take place.&lt;/p&gt;
&lt;p&gt;For the more complex and demanding needs of the financial management application, architecture considerations, in addition to those taken into account for the basic eCommerce site, might include: &amp;nbsp;what integration patterns might be leveraged to facilitate client integration, messaging patterns that might facilitate server side integration with third party financial calculators, and where inversion of control is appropriate. &amp;nbsp;Perhaps &lt;a href="http://en.wikipedia.org/wiki/Representational_State_Transfer"&gt;RESTful services&lt;/a&gt; would be included to provide integration support for clients requiring such capabilities. &amp;nbsp;Messaging via a &lt;a href="http://en.wikipedia.org/wiki/Publish/subscribe"&gt;publish/subscribe&lt;/a&gt; mechanism using a &lt;a href="http://www.eaipatterns.com/DistributionAggregate.html"&gt;composed message processor&lt;/a&gt; might be selected as the ideal means for coordinating data with third party vendors on the server side. &amp;nbsp;Again, proper refactoring, from an architectural perspective would include developing architectural spikes along with the foundational pieces of such an application to provide appropriate guidance for developers assisting with the project.&lt;/p&gt;
&lt;p&gt;Developing software for robotics is no different, in this respect, than developing any other application. &amp;nbsp;One must decide where the key architectural decisions need to be made and then provide adequate decisions and guidance to facilitate development in accordance with those decisions. &amp;nbsp;As stated, these decisions must be made in consideration of the project needs. &amp;nbsp;This post focuses on one project context, the architectural context of determining the overall approach to robotic motor control. &amp;nbsp;We&amp;#39;ll delve into three major architectural approaches to motor control along with highlighting a few specific implementations.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Deliberative vs. Reactive Robotics&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Certainly the best source of intelligent systems to study and emulate are those found in nature. &amp;nbsp;From the humble ant to the&amp;nbsp;exalted&amp;nbsp;human, evolution has honed a variety of strategies for dealing with the physical world. &amp;nbsp;Accordingly, if we are to create intelligent systems, a good place to start is by emulating the behaviors and responses to stimuli demonstrated by living creatures.&lt;/p&gt;
&lt;p&gt;In the early days of robotics, it was presumed that the most effective approach to emulating intelligence was by taking in detailed measurements provided by sensors, creating an internal representation of the world (e.g., grid-based maps), making plans based on that internal representation, and sending pre-planned commands to &lt;i&gt;actuators&lt;/i&gt; (devices which convert energy into motion) for moving and interacting with the world. &amp;nbsp;Inevitable, this approach presumes that the internal representation of the world is highly accurate, that the sensor reading are precise, and that there is enough time to carry out a plan before the world changes. &amp;nbsp;In other words, this approach is highly &lt;i&gt;deliberative&lt;/i&gt;. &amp;nbsp;Obviously, the world is highly dynamic, sensor readings are sometimes&amp;nbsp;erroneous, and time is sometimes of the&amp;nbsp;essence&amp;nbsp;when interacting with the world. &amp;nbsp;E.g., you probably wouldn&amp;#39;t want to spend much time creating an internal representation of the world if there&amp;#39;s a Mac truck speeding towards you.&lt;/p&gt;
&lt;p&gt;At the other extreme from deliberative is a &lt;i&gt;reactive &lt;/i&gt;approach to interacting with the world. &amp;nbsp;Taking a purely reactive approach does away with plans and internal representations of the world altogether; instead, a reactive approach focuses on using the world as its own model and reacts, accordingly. &amp;nbsp;Instead of plans, reactive approaches often rely upon &lt;a href="http://en.wikipedia.org/wiki/Finite-state_machine"&gt;finite state machines&lt;/a&gt; to modify behavior as the world changes. &amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Rodney_Brooks"&gt;Rodney Brooks&lt;/a&gt; changed robotics thinking upside down when he introduced this paradigm shift in the 80s. &amp;nbsp;The robots he and his team produced were much faster in their reaction times, when compared to deliberative robots, and exhibited surprisingly complex behavior, appearing quite intelligent in many scenarios. &amp;nbsp;But as is any extreme, a purely reactive approach to the world had it&amp;#39;s own drawbacks; its difficulty in managing complex scenarios which demand careful planning is one such example.&lt;/p&gt;
&lt;p&gt;&lt;img style="border:0;float:right;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/DeliberativeVsReactive.PNG" border="0" alt="" /&gt;The&amp;nbsp;figure&amp;nbsp;at right illustrates some of the differences between deliberative and reactive systems. &amp;nbsp;Adapted from (Arkin, 1998).&lt;/p&gt;
&lt;p&gt;While there are certainly pros and cons to either approach, there are some scenarios which are appropriate to one or the other. &amp;nbsp;While still others may require more of a hybrid approach. &amp;nbsp;Let&amp;#39;s now take a look at each approach in more detail.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Deliberative/Hierarchical Style&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;One of the first successful architectural implementations on a mobile robotic platform was the well known robot known as &lt;a href="http://en.wikipedia.org/wiki/Shakey_the_robot"&gt;Shakey&lt;/a&gt;, created at Stanford University in the 1960s. &amp;nbsp;Of particular note was the robot&amp;#39;s architecture which was made up of three predominant layers: &amp;nbsp;a sensing layer, a planning layer, and an execution layer. &amp;nbsp;Accordingly, as information would be made available by sensors, Shakey would make plans on how to react to the perceived world and send those plans on to the execution layer for low level control of actuators. &amp;nbsp;This began the architectural paradigm known as &lt;i&gt;sense-plan-act (SPA)&lt;/i&gt;. &amp;nbsp;As the SPA approach matured, additional layers were introduced in a hierarchical style, typically all of which having access to a global world model. &amp;nbsp;The upper layers in the hierarchy would use the world model to make plans reaching into the future. &amp;nbsp;After plan development completed at the highest levels, the plans would be broken down into smaller commands and passed to lower levels for execution. &amp;nbsp;Lower layers would then further decompose the commands into more actionable tasks which would ultimately be passed on to actuators at the lowest level. &amp;nbsp;In a hierarchical approach, the sensing layers participate in keeping the internal representation of the world updated. &amp;nbsp;Furthermore, to better accommodate dynamic changes to the world environment, lower planning layers may suggest changes to plans based on recent changes to the world model.&lt;/p&gt;
&lt;p&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/HierarchicalParadigm.PNG" style="border:0;float:right;" alt="" /&gt;The diagram at right roughly illustrates this hierarchical architectural approach. &amp;nbsp;As should be noted, the sensing layers update the world model while a hierarchy of planning and execution layers formulate plans and breaks those plans down into actionable tasks. &amp;nbsp;Adapted from (Kortenkamp, 1998).&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/HierarchicalParadigm.PNG"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;i&gt;4D/RCS&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;The current pinnacle of hierarchical architectures may be found in the reference architecture known as &lt;a href="http://www.isd.mel.nist.gov/documents/albus/4DRCS_ver2.pdf"&gt;4D/RCS (4 Dimensional / Real-time Control System)&lt;/a&gt;. &amp;nbsp;4D/RCS is the latest version of the RCS reference model architecture for intelligent systems that has been evolving for decades. &amp;nbsp;With 4D/RCS, six (more or less) layers are defined for creating and decomposing plans into low level action:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Unit/Vehicle/Mission Layer: &amp;nbsp;this layer decomposes the overall plan into actions for individual modules.&amp;nbsp;&amp;nbsp;Plans are regenerated every 2 hours to account for changes to the world model.&lt;/li&gt;
&lt;li&gt;Module Layer: &amp;nbsp;converts actions into tasks to be performed on specific objects and schedules the tasks, accordingly.&amp;nbsp;&amp;nbsp;Plans are regenerated every 10 minutes.&lt;/li&gt;
&lt;li&gt;Task Layer: &amp;nbsp;converts actions on a single object into sequences of moves to carry out the action.&amp;nbsp;&amp;nbsp;Plans are regenerated every minute.&lt;/li&gt;
&lt;li&gt;Elemental Layer: &amp;nbsp;creates plans for each move, avoiding obstacles and collisions. &amp;nbsp;Plans are regenerated every 5 seconds.&lt;/li&gt;
&lt;li&gt;Primitive Layer: &amp;nbsp;determines motion primitives (speed, trajectory) to facilitate smooth movement. &amp;nbsp;Plans are&amp;nbsp;regenerated&amp;nbsp;every 0.5 second.&lt;/li&gt;
&lt;li&gt;Servo Layer: &amp;nbsp;sends servo control commands (velocity, force) to actuators. &amp;nbsp;Plans are&amp;nbsp;regenerated&amp;nbsp;every 0.05 seconds.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At the heart of each and every layer are one or more 4D/RCS nodes which contain a behavior generation process which accepts commands from the next higher level and issues subgoals to the behavior generation process at lower levels. &amp;nbsp;Furthermore, each node reports its task execution status up the chain for consideration into further planning. &amp;nbsp;While plans are being solidified and disseminated, a sensory processing process in each node receives sensory input from lower levels, updates the world model, and collates the sensory data into larger units which it passes on to the next higher level; e.g., points get converted to lines which get converted to surfaces which get converted to objects with each successive rise through the 4D/RCS layers &amp;nbsp;A &lt;a href="http://upload.wikimedia.org/wikipedia/commons/c/c6/RCS_NODE_Internal_structure.jpg"&gt;visual summary of a 4D/RCS node&lt;/a&gt; quickly shows just how complex such a system can become. &amp;nbsp;But sometimes the demands of a task require a respective amount of sophistication in the architectural approach. &amp;nbsp;&amp;nbsp;A comprehensive review of the 4D/RCS approach is discussed in (Albus, 2006).&lt;/p&gt;
&lt;p&gt;The primary advantage to deliberative, hierarchical approaches such as 4D/RCS is that competent plans for managing complex scenarios can be generated and broken down into smaller chunks for lower levels to execute. &amp;nbsp;But an obvious disadvantage to this comes in the form of much added complexity to the overall system while penalizing the speed at which the system can react to a changing environment.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reactive Style&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the 1980s, Rodney Brooks, in an effort to overcome some of the limitations of sense-plan-act that Shakey and other such robots exhibited, introduced the concept of reactive control. &amp;nbsp;&amp;quot;Simply put, reactive control is a technique for tightly coupling perception and action, typically in the context of motor behaviors, to produce timely robotic response in dynamic and unstructured worlds.&amp;quot; &amp;nbsp;(Arkin, 1998). &amp;nbsp;In other words, by eliminating the reliance on maintaining an internal world model and avoiding large amounts of time generating plans, simple responses can be executed in reaction to specific stimuli, thus exhibiting behavior similar to that of living organisms. &amp;nbsp;If there was any doubt in what Brooks was trying to imply, he boldly titled one of his works,&amp;nbsp;&lt;a href="http://people.csail.mit.edu/brooks/papers/Planning%20is%20Just.pdf"&gt;Planning is Just a Way of Avoiding Figuring Out What to do Next&lt;/a&gt;. &amp;nbsp;This paradigm shift away from planning turned sense-plan-act into a simpler&amp;nbsp;&lt;i&gt;sense-act&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;Accordingly, in a sense-act paradigm, the primary focus is in carefully defining behaviors and the environment stimulus which should invoke those behaviors. This focus is deeply rooted in the idea of &lt;a href="http://en.wikipedia.org/wiki/Behaviorism"&gt;behaviorism&lt;/a&gt;;&amp;nbsp;from a behaviorism perspective, behavior is defined in terms of stimulus and response by observing what an organism does. &amp;nbsp;This approach of developing robotics around such behaviors, unsurprisingly enough, is commonly referred to as&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Behavior-based_robotics"&gt;Behavior Based Robotics&lt;/a&gt;. &amp;nbsp;By leveraging simple state machines, which we&amp;#39;ll examine below, to define which behaviors are active for a given stimuli, the overall architectural complexity is greatly reduced while giving rise to responsive, seemingly intelligent behaviors. &amp;nbsp;(The question of whether or not the robot is truly intelligent and/or self-aware is a debate that I&amp;#39;ll leave to others such as &lt;a href="http://en.wikipedia.org/wiki/John_Searle"&gt;Searle&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Daniel_Dennett"&gt;Dennett&lt;/a&gt;.) &amp;nbsp;While the reactive approach took the spotlight for a number of years, and is still very appropriate in some cases, its limitations in managing complex scenarios and performing sophisticated planning indicated that this approach was not the end all panacea for all situations.&lt;/p&gt;
&lt;p&gt;&lt;img style="border:0;float:right;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/ReactiveParadigm.PNG" border="0" alt="" /&gt;The diagram at right clarifies that reactive systems remove planning and deal with sensory input within the context of each behavior. &amp;nbsp;I.e., &amp;quot;Behavior 1&amp;quot; has no knowledge of the feedback coming from &amp;quot;Sensor 3.&amp;quot; &amp;nbsp;Adapted from (Kortenkamp, 1998).&lt;/p&gt;
&lt;p&gt;&lt;i&gt;A Design Methodology for Reactive Systems&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Due to the simplistic nature of reactive systems, the corresponding design methodology for developing such systems is rather straight-forward (Kortenkamp, 1998):&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Choose the tasks to be performed. &amp;nbsp;E.g., find kitchen.&lt;/li&gt;
&lt;li&gt;Break down the tasks in terms of specific motor-behaviors necessary to accomplish each task. &amp;nbsp;E.g., find wall, follow wall, recognize kitchen.&lt;/li&gt;
&lt;li&gt;For each action, find a condition under which each action should be taken. &amp;nbsp;E.g., wall found.&lt;/li&gt;
&lt;li&gt;For each condition, find a set of perceptual measurements that are sufficient to determine the truth of the condition. &amp;nbsp;E.g., long straight line connected to obstacle.&lt;/li&gt;
&lt;li&gt;For each measurement, design a custom sensory process to compute it. &amp;nbsp;E.g., bumper sensor activated, edge length over threshold found.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When implemented, we find that the defined behaviors can be realized as states within a finite state machine and that found conditions act as the mechanism for changing from one behavior state to another. &amp;nbsp;What&amp;#39;s missing is an arbitration mechanism to determine which behavior wins out in light of competing conditions. &amp;nbsp;Let&amp;#39;s briefly look at implementation approaches to reactive systems along with how such arbitration is achieved.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;Subsumption &amp;amp;&amp;nbsp;&lt;span style="font-style:normal;"&gt;&lt;i&gt;Motor Schemas&lt;/i&gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;A key concern with reactive, behavior based robotics is determining which behavior should take&amp;nbsp;precedence&amp;nbsp;if conditions exist to activate two or more behaviors. &amp;nbsp;An arbitration mechanism needs to be introduced to resolve such competing situation. &amp;nbsp;Brooks dealt with this issue by proposing an architecture known as &lt;/span&gt;&lt;a href="http://ai.eecs.umich.edu/cogarch0/subsump/"&gt;subsumption&lt;/a&gt;&lt;span&gt;. &amp;nbsp;Simply enough, a subsumption architecture still uses a finite state machine to codify behaviors and transitions, but introduces behavioral layers which can provide input to other layers and override behaviors of lower layers. &amp;nbsp;The advantage is in facilitating more sophisticated behaviors as a sum of &amp;quot;lesser&amp;quot; behaviors. &amp;nbsp;It&amp;#39;s better understood by reviewing the following example from (Arkin, 1998):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/BehaviorBasedArchitectureExample.gif" alt="" /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In the example, there are three behavioral layers: &amp;nbsp;Avoid-Objects, Explore, and Back-Out-of-Tight-Situations. &amp;nbsp;The Avoid-Objects Layer&amp;#39;s responsibility is to avoid objects by moving away from any perceived obstacles. &amp;nbsp;The Explore Layer&amp;#39;s responsibility is to cover large areas of space in the absence of obstacles. &amp;nbsp;The highest level assists the robot in getting out of tight spaces if the lower layers are unable to do so. &amp;nbsp;Each discrete behavior can invoke transitions to other behaviors and/or provide input or advice to subsequent behaviors based on perceived conditions. &amp;nbsp;The &amp;quot;trick&amp;quot; of subsumption is that higher levels can suppress commands between lower level behaviors; consequently, higher layers are able to handle more complex scenarios by manipulating lower level behaviors in addition to its own. &amp;nbsp;To illustrate suppression, note, in the above example that the &amp;quot;Reverse&amp;quot; behavior in the&amp;nbsp;Back-Out-of-Tight-Situations Layer suppresses any commands that the &amp;quot;Forward&amp;quot; behavior is sending to the actuators. &amp;nbsp;By doing so, complex behaviors may emerge using a number of basic behaviors and a relatively simple architectural approach.&lt;/p&gt;
&lt;p&gt;A major challenge in using a subsumption architecture is deciding the appropriate hierarchy of behavioral layers. &amp;nbsp;In more complex scenarios, it quickly gets sticky deciding which layer should have the right to suppress which other layers. &amp;nbsp;(Otherwise known as spaghetti-prone.) &amp;nbsp;Additionally, since the layers have bi-directional dependencies amongst each other, in order to provide input and suppression, changes to layers can have large impacts on other layers, often resulting in shotgun surgery with any change. &amp;nbsp;Ronald Arkin introduced a subsequent architecture to address these, and other concerns, with an approach known as Motor-Schema based control which does away with arbitrating competing behaviors. &amp;nbsp;Each active behavior in a Motor-Schema based control system calculates a vector to be carried out by an actuator (e.g., wheels or arm). &amp;nbsp;Using vector addition, a final vector for each actuator is computed and sent for execution. &amp;nbsp;With this approach, the output of behaviors is combined instead of arbitrated. &amp;nbsp;This avoids the need to determine suppression hierarchies and makes a more extensible application, within the limits of behavior based robotics.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hybrid Style&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img style="border:0;float:right;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/HybridParadigm.PNG" border="0" alt="" /&gt;Most modern architectural approaches to robotics control attempt to combine the planning capabilities of deliberative systems with the responsiveness of reactive systems. &amp;nbsp;Appropriately enough, this is referred to as a hybrid style. &amp;nbsp;While the deliberative approach takes a sense-plan-act perspective and the reactive approach follows with plan-act, a hybrid approach typically takes the form of &lt;i&gt;plan, sense-act&lt;/i&gt;. &amp;nbsp;So while the sense-act layers carry out behaviors, the deliberative planning layer can observe the progress of reactive behaviors and suggest direction based on reasoning, planning and problem-solving. &amp;nbsp;The diagram at right crudely demonstrates this combination of the two approaches. &amp;nbsp;Adapted from (Kortenkamp, 1998).&lt;/p&gt;
&lt;p&gt;&lt;i&gt;Three-Layer Architecture (3T)&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;James Firby&amp;#39;s thesis proposing Reactive Action Packages (RAPs) (Firby, 1989) provided a solid approach for integrating deliberative planning with reactive behaviors in the form of a three layer architecture. &amp;nbsp;A multitude of subsequent architectures have emulated a similar approach, coming to be known as 3T architecture. &amp;nbsp;From Eran Gat&amp;#39;s essay &lt;i&gt;Three Layer Architecture&lt;/i&gt;&amp;nbsp;(Kortenkamp, 1998):&lt;/p&gt;
&lt;blockquote&gt;Three-layer architectures organize algorithms according to whether they contain no state, contain state&amp;nbsp;reflecting&amp;nbsp;memories about the past, or contain state&amp;nbsp;reflecting&amp;nbsp;predictions about the future. &amp;nbsp;Stateless sensor-based algorithms inhabit the control component. &amp;nbsp;Algorithms that contain memory about the past inhabit the sequencer. &amp;nbsp;Algorithms that make predictions about the future inhabit the deliberator. ... In 3T the components are called the &lt;i&gt;skill layer&lt;/i&gt;, the &lt;i&gt;sequencing layer&lt;/i&gt;, and the &lt;i&gt;planning [deliberative] layer&lt;/i&gt;.&lt;/blockquote&gt;
&lt;p&gt;As implied by being a hybrid approach, this 3T architecture is not mutually exclusive to behavior driven robotics. &amp;nbsp;Indeed, the skill layer itself is made up of unique behaviors which resemble that of reactive systems. &amp;nbsp;The sequencing layer then uses the world model to determine when a change in behavior is required. &amp;nbsp;The planning layer can then monitor the lower layers and perform more deliberative, time consuming processes such as path planning. &amp;nbsp;The primary variation amongst 3T implementations lies in the decision between whether the planning layer &amp;quot;runs the show&amp;quot; or if the sequencing layer takes command and invokes the planning layer only when needed. &amp;nbsp;&lt;a href="http://ai.eecs.umich.edu/cogarch3/Gat/Gat.html"&gt;Atlantis&lt;/a&gt; is one such example that leaves primary control to the sequencing layer to invoke the planning layer. &amp;nbsp;Other examples of 3T implementations include Bonasso&amp;#39;s &lt;i&gt;&lt;a href="http://dli.iiit.ac.in/ijcai/IJCAI-91-VOL2/PDF/089.pdf"&gt;Integrating Reaction Plans and Layered Competences through Synchronous Control&lt;/a&gt;&lt;/i&gt;&amp;nbsp;and &lt;a href="http://cs.stanford.edu/people/petrovsk/dn/publications/montemerlo_fsr08.pdf"&gt;Standford&amp;#39;s Junior&lt;/a&gt; which took 2nd place in &lt;a href="http://www.darpa.mil/grandchallenge/index.asp"&gt;Darpa&amp;#39;s Urban Challenge&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Wrapping Up&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The intention of this article has been to provide an introductory overview of paradigms of robotic control including deliberative/hierarchical systems, reactive systems, and hybrid systems. &amp;nbsp;While each approach is appropriate in select contexts, a hybrid&amp;nbsp;architecture&amp;nbsp;is very adaptable for accommodating a large variety of robotic control scenarios with sufficient planning and reactive capabilities. &amp;nbsp;The interested reader is encouraged to dig further into the references and links provided to learn more about these various approaches and design methodologies for implementation.&lt;/p&gt;
&lt;p&gt;In the &lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2010/03/01/message-based-systems-for-maintainable-asynchronous-development.aspx"&gt;next post&lt;/a&gt;, we will examine messaging strategies to facilitate the communications amongst the layers and components of robotic systems.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Albus, J., Madhavan, R., Messina, E. &amp;nbsp;2006. &amp;nbsp;&lt;a href="http://www.amazon.com/Intelligent-Vehicle-Systems-RCS-Approach/dp/1600212603"&gt;Intelligent Vehicle Systems: A 4D/RCS Approach&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Arkin, R. &amp;nbsp;1998. &amp;nbsp;&lt;a href="http://www.amazon.com/Behavior-Based-Robotics-Intelligent-Autonomous-Agents/dp/0262011654"&gt;Behavior Based Robotics&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Firby, J. &amp;nbsp;1989. &amp;nbsp;&lt;a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.17.9&amp;amp;rep=rep1&amp;amp;type=pdf"&gt;Adaptive Execution in Complex Dynamic Worlds&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Kortenkamp, D., Bonasso, R., Murphy, R. &amp;nbsp;1998. &amp;nbsp;&lt;a href="http://www.amazon.com/Artificial-Intelligence-Mobile-Robots-Successful/dp/0262611376"&gt;Artificial Intelligence and Mobile Robots&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=55261" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Architecture/default.aspx" /><category term="Robotics" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Robotics/default.aspx" /></entry><entry><title>Real World S#arp Architecture</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2010/02/03/using-s-arp-architecture-in-the-real-world.aspx" /><id>/blogs/billy_mccafferty/archive/2010/02/03/using-s-arp-architecture-in-the-real-world.aspx</id><published>2010-02-03T21:04:00Z</published><updated>2010-02-03T21:04:00Z</updated><content type="html">&lt;p&gt;With any framework, technology, tutorial, book, idea, &amp;lt;insert pedagogical sources here&amp;gt;, it&amp;#39;s often difficult to figure how the technique should be used in the real world. &amp;nbsp;Sure, things sound great when the scope of your project deals cleanly with Customers, Orders and Order Items; and sure, auto-binding works great in a nicely controlled scenarios...but how should things work in the &amp;quot;real world.&amp;quot; &amp;nbsp;No, not the Real World in which Brooke has a complete meltdown in season 18. &amp;nbsp;I&amp;#39;m talking about the real world in which many scenarios aren&amp;#39;t clean cut in determining where the controller should end and the application services layer should take over; the world in which you&amp;#39;re trying to bind from unique data collection mechanisms to appease odd requests from the client; the world in which even the GoF would retort with &amp;quot;heh, that&amp;#39;s a tricky one.&amp;quot; &amp;nbsp;Yes, we&amp;#39;re faced with these kinds of dilemmas on a frequent basis. &amp;nbsp;What I find to be truly helpful is to look at full blown, real-world applications. &amp;nbsp;While a real-world application is never perfect, you can often find great gems for dealing with tricky situations and using patterns and complex scenarios. &amp;nbsp;Howard van Rooijen has released just such a real-world application demonstrating the use of&amp;nbsp;&lt;a href="http://wiki.sharparchitecture.net/"&gt;S#arp Architecture&lt;/a&gt;,&amp;nbsp;&lt;a href="http://automapper.codeplex.com/"&gt;AutoMapper&lt;/a&gt;, &lt;a href="http://code.google.com/p/elmah/"&gt;ELMAH&lt;/a&gt;, &lt;a href="http://sparkviewengine.com/"&gt;Spark View Engine&lt;/a&gt;, and other great tools.&lt;/p&gt;
&lt;p&gt;In Howard&amp;#39;s own words... &amp;nbsp;&amp;quot;A few months ago I wrote an email to the community about a site we had just launched &amp;ndash; http://fancydressoutfitters.co.uk that used S#arp Architecture at its core along with a whole myriad of other Open Source Frameworks and Tools (Spark, AutoMapper, PostSharp, xVal...).&lt;/p&gt;
&lt;p&gt;&lt;a href="http://whocanhelpme.codeplex.com/"&gt;&lt;img style="border:0;float:right;margin:4px;" border="0" src="http://who-can-help.me/Views/_Content/img/logo.png" width="400" alt="" /&gt;&lt;/a&gt;In the run up to the festive period, myself and two of the development team &amp;ndash; Jonathan George &amp;amp; James Broome, decided that in the spirit of giving, we wanted to gift something back to the communities that gave so much to us throughout the year; so we decided to build a new sample web application to showcase the use of these various frameworks &amp;amp; tools called &amp;ldquo;Who Can Help Me?&amp;rdquo; which is based on the same architectural style as http://fancydressoutfitters.co.uk.&lt;/p&gt;
&lt;p&gt;Who Can Help Me? started out as a small web application I built a few years ago to solve a small and specific business problem within our consulting organisation (and to test out .NET 3.5, LINQ to SQL, ASP.NET WebForms &amp;amp; MS AJAX!). The problem was, that as the organisation grew and new members of staff started, they found it difficult to find the right people who could help them solve specific problems they&amp;rsquo;d encounter in their consulting gigs. As I have worked for the organisation for a long time (&amp;gt;9 years) I generally knew everyone, had worked with most of them and knew what their areas of expertise were, thus I&amp;rsquo;d get a few calls every day asking &amp;ldquo;Do you know anyone who knows about X that could help me?&amp;rdquo;. The solution was to create a searchable skills matrix that would allow people within an organisation find other people who had specific skills or expertise who could help them solve a particular problem.&lt;/p&gt;
&lt;p&gt;So Jonathan, James &amp;amp; I decided to re-write the Who Can Help Me? from scratch, using the architecture style, frameworks and tools we used to build http://fancydressoutfitters.co.uk - it might seem like we&amp;#39;ve massively over-complicated the architecture for such a simple application - but we really wanted this to demonstrate some of the concepts &amp;amp; techniques we used to build a full scale, public facing enterprise web application.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Who Can Help Me? utilises the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://wiki.sharparchitecture.net/"&gt;S#arp Architecture&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/(S(d35rmemuuono1wvm1gsp2n45))/mvc/"&gt;ASP.NET MVC&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.codeplex.com/MVCContrib"&gt;ASP.NET MVC Contrib&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.hibernate.org/343.html"&gt;NHibernate&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://fluentnhibernate.org/"&gt;Fluent NHibernate&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.castleproject.org/container/index.html"&gt;Castle Windsor&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="http://automapper.codeplex.com/"&gt;AutoMapper&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.codeplex.com/csd"&gt;Configuration Section Designer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.ohloh.net/p/dotnetopenauth"&gt;DotNetOpenAuth&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://code.google.com/p/elmah/"&gt;ELMAH&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.dotlesscss.com/"&gt;Less CSS for .NET&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://codebetter.com/blogs/aaron.jensen/archive/2008/05/08/introducing-machine-specifications-or-mspec-for-short.aspx"&gt;Machine.Specifications (MSpec) BDD Framework&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.codeplex.com/MEF"&gt;MEF&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.postsharp.org/"&gt;PostSharp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;RhinoMocks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://sparkviewengine.com/"&gt;Spark View Engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://tweetsharp.com/"&gt;TweetSharp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://xval.codeplex.com/"&gt;xVal Validation Framework &amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The project is currently hosted at Codeplex: &lt;a href="http://whocanhelpme.codeplex.com/"&gt;http://whocanhelpme.codeplex.com/&lt;/a&gt; and we&amp;rsquo;ve also released a live demo: &lt;a href="http://who-can-help.me"&gt;http://who-can-help.me&lt;/a&gt;. &amp;nbsp;We&amp;rsquo;ve added some documentation on the Codeplex homepage and will continue to refine this and augment it with blog posts covering some topics in more depth &amp;ndash; so if you&amp;rsquo;re interested &amp;ndash; please keep an eye on the following blogs / twitter:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://howard.vanrooijen.co.uk/blog/"&gt;http://howard.vanrooijen.co.uk/blog/&lt;/a&gt; | @HowardvRooijen&lt;/li&gt;
&lt;li&gt;&lt;a href="http://jonathangeorge.co.uk/"&gt;http://jonathangeorge.co.uk/&lt;/a&gt; | @jon_george1&lt;/li&gt;
&lt;li&gt;&lt;a href="http://jamesbroo.me/"&gt;http://jamesbroo.me/&lt;/a&gt; | @broomej&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Thanks very much for this contribution Howard! &amp;nbsp;Besides myself, I&amp;#39;m sure others have found this project helpful and will continue to do so in the future.&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=55260" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="S#arp Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Architecture/default.aspx" /></entry><entry><title>Your First S#arp Project in 15 Minutes</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2010/02/01/your-first-s-arp-project-in-10-minutes.aspx" /><id>/blogs/billy_mccafferty/archive/2010/02/01/your-first-s-arp-project-in-10-minutes.aspx</id><published>2010-02-01T23:11:00Z</published><updated>2010-02-01T23:11:00Z</updated><content type="html">&lt;p&gt;The most daunting task when trying out or learning a new framework or technology is figuring out where the heck to start. &amp;nbsp;Take &lt;a href="http://wiki.sharparchitecture.net"&gt;S#arp Architecture&lt;/a&gt; for instance (arguably, the world&amp;#39;s most insanely amazing framework ever written by mankind...more or less). &amp;nbsp;As incredibly cool as S#arp is, it can be intimidating trying to figure out where to start. &amp;nbsp;To make that first step a bit easier, the following will get you up and running with your first S#arp project in about 15 minutes or less. &amp;nbsp;(Steps verified on WinXP Pro.)&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s what you&amp;#39;ll have in less than 15 minutes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A new S#arp Architecture project generated using a VS template,&lt;/li&gt;
&lt;li&gt;CRUD pages for managing product categories for our crazy cool store,&lt;/li&gt;
&lt;li&gt;live communications with the application database,&lt;/li&gt;
&lt;li&gt;and unit tests verifying that everything is working.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ready?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Download S#arp Architecture and Generate Your Project&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and unzip the latest release at &lt;a href="http://cloud.github.com/downloads/codai/Sharp-Architecture/SharpArchitecture_1.0_2009_Q3.zip"&gt;http://cloud.github.com/downloads/codai/Sharp-Architecture/SharpArchitecture_1.0_2009_Q3.zip&lt;/a&gt;. &amp;nbsp;We&amp;#39;ll call the unzip location &amp;lt;ROOT&amp;gt;.&lt;/li&gt;
&lt;li&gt;Close all running instances of VS 2008.&lt;/li&gt;
&lt;li&gt;Install &amp;lt;ROOT&amp;gt;\tools\T4 Toolbox 9.5.20.1.msi. &amp;nbsp;(You&amp;#39;ll need to uninstall any existing version before doing so (not compatible with latest T4 Toolbox...yet.)&lt;/li&gt;
&lt;li&gt;Install &amp;lt;ROOT&amp;gt;\tools\NUnit-2.5.2.9222.msi. &amp;nbsp;(It&amp;#39;s likely compatible with later versions of NUnit as well.)&lt;/li&gt;
&lt;li&gt;Copy &amp;lt;ROOT&amp;gt;\VisualStudioTemplate\SharpArchApplicationTemplate.zip to &amp;quot;C:\Documents and Settings\&amp;lt;YOUR USERNAME&amp;gt;\My Documents\Visual Studio 2008\Templates\ProjectTemplates\Visual C#\Web&amp;quot; &amp;nbsp;(You&amp;#39;ll need to create the \Web folder. &amp;nbsp;Also, be sure to &lt;i&gt;copy&lt;/i&gt; the zip, don&amp;#39;t unzip it!)&lt;/li&gt;
&lt;li&gt;Copy&amp;nbsp;&amp;lt;ROOT&amp;gt;\VisualStudioTemplate\SharpArchApplicationWizard.dll to &amp;quot;C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE&amp;quot;&lt;/li&gt;
&lt;li&gt;Open VS 2008 and select File / New Project / Visual C# / Web / S#arp Architecture Application. &amp;nbsp;For the name, enter &amp;quot;MyCoolStore&amp;quot; (no spaces!), set the location to whatever you&amp;#39;d like (e.g., &amp;quot;C:\MyStuff\Projects\&amp;quot;), and keep &amp;quot;Create directory for solution&amp;quot; unchecked. &amp;nbsp;Then click OK. &amp;nbsp;(It&amp;#39;ll take about a minute to generate the solution...when the status changes back to &amp;quot;Ready&amp;quot; in the very bottom left of VS, you&amp;#39;re good to go.)&lt;/li&gt;
&lt;li&gt;Build the solution (should build without a problem).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Hook Up to a Database&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Run NUnit and select File / Open Project, to then open &amp;lt;ROOT&amp;gt;/MyCoolStore/tests/MyCoolStore.Tests/bin/Debug/MyCoolStore.Tests.dll&lt;/li&gt;
&lt;li&gt;Run the tests and &lt;strong&gt;see the Data tests fail&lt;/strong&gt; due to a DB connection timeout. &amp;nbsp;Time to create the database!&lt;/li&gt;
&lt;li&gt;Create a new SQL Server database called MyCoolStore. &amp;nbsp;(Other DBs are &lt;a href="https://www.hibernate.org/361.html"&gt;supported&lt;/a&gt;.)&lt;/li&gt;
&lt;li&gt;In VS 2008, open MyCoolStore.Web/NHibernate.config and modify the following bits in the DB connection string: &amp;nbsp;YOUR_SERVER, YOUR_USERNAME and YOUR_PASSWORD. &amp;nbsp;Save the file.&lt;/li&gt;
&lt;li&gt;In NUnit, run the tests to &lt;strong&gt;see all the tests pass&lt;/strong&gt;...if all is green, your project is talking to the database. &amp;nbsp;(If the tests under Data still fail, recheck your DB connection string.)&lt;/li&gt;
&lt;li&gt;In VS 2008, right click the MyCoolStore.Web project and &amp;quot;Set as Startup Project&amp;quot; and click F5 after doing so to run the project. &amp;nbsp;(Not much to see here yet beyond the landing page...let&amp;#39;s add some CRUD functionality.)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Generate CRUD Scaffolding&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;After verifying that the web project is running, stop the debugger.&lt;/li&gt;
&lt;li&gt;In VS 2008, open Code Generation/CrudScaffolding/ScaffoldingGeneratorCommand.tt and do the following:
&lt;ul&gt;
&lt;li&gt;Change line 19 to:&lt;br /&gt;
&lt;pre name="code" class="c-sharp"&gt;new EntityScaffoldingDetails(&amp;quot;ProductCategory&amp;quot;);&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Delete the four additions to entityScaffoldingDetails, lines 25-36, and replace them with:&lt;br /&gt;
&lt;pre name="code" class="c-sharp"&gt;entityScaffoldingDetails.EntityProperties.Add(
   new EntityProperty(&amp;quot;Name&amp;quot;, &amp;quot;string&amp;quot;, 
      &amp;quot;Seafood&amp;quot;, &amp;quot;[NotNull, NotEmpty]&amp;quot;, true)
);&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Uncomment the 2nd to last line (i.e., remove the &amp;quot;//&amp;quot; in front of &lt;code&gt;generator.Run()&lt;/code&gt;) and save the file...&lt;/li&gt;
&lt;li&gt;&lt;i&gt;The CRUD scaffolding generator will run for a few seconds...it&amp;#39;ll be done when the cursor no longer looks like an hour glass. &amp;nbsp;(Yes, that&amp;#39;s annoying that it runs when the file is saved...that&amp;#39;s T4.)&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;Recomment out the 2nd to last line (i.e., add the &amp;quot;//&amp;quot; in front of &lt;code&gt;generator.Run()&lt;/code&gt; back in) and save the file. &amp;nbsp;(This avoids premature generation - always embarrassing - when editing this file in the future.)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;In VS, rebuild the solution.&lt;/li&gt;
&lt;li&gt;In NUnit, run all of the tests...only CanConfirmDatabaseMatchesMappings should fail. &amp;nbsp;Time to add the ProductCategories table!&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Create the Database Table&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;In NUnit, run just Tests/MyCoolStore/Data/NHibernateMaps/MappingIntegrationTests/CanGenerateDatabaseSchema (which should pass), and change the NUnit tab to &amp;quot;Text Output.&amp;quot;&lt;/li&gt;
&lt;li&gt;Copy all of the SQL that you see on the Text Output tab in NUnit and run it against the MyCoolStore database using SQL Management Studio.&lt;/li&gt;
&lt;li&gt;In NUnit, run all of the tests again to see them pass.&lt;/li&gt;
&lt;li&gt;In VS, click F5 to run the project and change the URL, after it loads, to &lt;a href="http://localhost&amp;lt;DEBUG%20PORT&amp;gt;"&gt;http://localhost:&amp;lt;DEBUG PORT&amp;gt;/ProductCategories&lt;/a&gt;. &amp;nbsp;You can now CRUD product categories to your heart&amp;#39;s content!&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Enforce Object Uniqueness&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Let&amp;#39;s say you now want to enforce name uniqueness...
&lt;ul&gt;
&lt;li&gt;In VS, open MyCoolStore.Core/ProductCategory.cs and add an attribute as:&lt;br /&gt;
&lt;pre name="code" class="c-sharp"&gt;[HasUniqueDomainSignature(
   Message=&amp;quot;Provide a unique name dag nabbit!&amp;quot;)]
public class ProductCategory : Entity
{
   ...&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Build the solution, F5 into debug mode, and try to add two product categories with the same name...go ahead, try it, I triple dog dare you.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Wanna learn more? &amp;nbsp;Go to&amp;nbsp;&lt;a href="http://wiki.sharparchitecture.net/"&gt;http://wiki.sharparchitecture.net/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And for all you S#arp junkies out there...the next quarterly release is coming soon...and it&amp;#39;s going to have proper utilization of the application services layer during CRUD generation!&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=55243" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="S#arp Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Architecture/default.aspx" /></entry><entry><title>Offsetting Your Bad Code Footprint</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2009/11/23/offsetting-your-bad-code-footprint.aspx" /><id>/blogs/billy_mccafferty/archive/2009/11/23/offsetting-your-bad-code-footprint.aspx</id><published>2009-11-23T21:01:00Z</published><updated>2009-11-23T21:01:00Z</updated><content type="html">&lt;p&gt;Admit it, we&amp;#39;ve all written bad code now and again. &amp;nbsp;Unfortunately, your bad code over time can leave a lasting footprint on the technosphere. &amp;nbsp;If you&amp;#39;re beginning to feel guilty about your bad code footprint, do know that you have options! &amp;nbsp;While reducing your bad code footprint by writing better code is a logical approach; there are some that feel doing so will adversely affect timelines and budgets (regardless of quality). &amp;nbsp;But even then, you can still &lt;i&gt;offset&lt;/i&gt;&amp;nbsp;your bad code footprint by purchasing bad code offsets at&amp;nbsp;&lt;a href="http://codeoffsets.com/"&gt;http://codeoffsets.com/&lt;/a&gt;. &amp;nbsp;(A thanks to Roy Bradley for pointing me to this guilt reduction technique.) &amp;nbsp;&amp;quot;The Bad Code Offset provides a convenient and rational approach for balancing out the bad code we all have created at one time or another throughout our lifetime&amp;mdash;even when we can&amp;rsquo;t go back and fix it directly.&lt;/p&gt;
&lt;p&gt;Denominated in Source Lines of Code (SLOC), every purchase will offset the desired quantity of SLOC and pave the way toward future code excellence. &amp;nbsp;Money raised through the purchase of Bad Code Offsets supports the various Open Source Initiatives that are performing vital work towards the salvation of our future code base.&lt;/p&gt;
&lt;p&gt;Every purchase of an offset today will take us one line of code closer to our goal of universal code excellence. Banish the bugs in your past as you help usher in a new age of software development.&amp;quot;&lt;/p&gt;
&lt;p&gt;See, you&amp;#39;re only one PayPal transaction away from reducing your bad code footprint!&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;br /&gt;&lt;a href="http://wiki.sharparchitecture.net"&gt;http://wiki.sharparchitecture.net&lt;/a&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=54072" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="DeepThoughtsByJackHandey" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/DeepThoughtsByJackHandey/default.aspx" /></entry><entry><title>S#arp Architecture 1.0 2009 Q3 with NHibernate 2.1.1 GA Released</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2009/11/12/s-arp-architecture-1-0-2009-q3-with-nhibernate-2-1-1-ga-released.aspx" /><id>/blogs/billy_mccafferty/archive/2009/11/12/s-arp-architecture-1-0-2009-q3-with-nhibernate-2-1-1-ga-released.aspx</id><published>2009-11-13T01:08:00Z</published><updated>2009-11-13T01:08:00Z</updated><content type="html">&lt;p&gt;I&amp;#39;m pleased to announce that S#arp Architecture&amp;nbsp;1.0&amp;nbsp;2009 Q3 has been released at&amp;nbsp;&lt;a href="http://github.com/codai/Sharp-Architecture/downloads"&gt;http://github.com/codai/Sharp-Architecture/downloads&lt;/a&gt;. &amp;nbsp;This isn&amp;#39;t a major upgrade, just an incremental, quarterly release to help developers work with the latest, stable S#arp dependencies. &amp;nbsp;Along with updated documentation (at&amp;nbsp;&lt;a href="http://wiki.sharparchitecture.net/"&gt;http://wiki.sharparchitecture.net/&lt;/a&gt;) and migration&amp;nbsp;guidance (in /VersionHistory.txt), this release has a number of&amp;nbsp;incremental improvements to developing&amp;nbsp;your S#arp Architecture project, including:&lt;/p&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Upgraded dependencies including NHibernate 2.1.1 GA, Fluent NHibernate 1.0 and NHibernate LINQ 1.0&lt;/li&gt;
&lt;li&gt;New Many-to-Many convention added to VS project template (thanks John Ledger!)&lt;img src="http://devlicio.us/resized-image.ashx/__size/600x450/__key/CommunityServer.Components.UserFiles/00.00.00.21.06/logo_5F00_web_5F00_gif.gif" align="right" width="206" height="91" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;New DB schema generation (as a unit test) added to VS project template (thanks indomitablehef!)&lt;/li&gt;
&lt;li&gt;VS generated project now uses the HiLo algorithm for identity generation&lt;/li&gt;
&lt;li&gt;S#arp Architecture is now HornGet compatible (&lt;a href="http://preview.tinyurl.com/ydoolwh"&gt;http://preview.tinyurl.com/ydoolwh&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;S#arp Architecture has moved to GitHub since the last release (&lt;a href="http://github.com/codai/Sharp-Architecture"&gt;http://github.com/codai/Sharp-Architecture&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;The wiki documentation has gotten much more complete (&lt;a href="http://wiki.sharparchitecture.net"&gt;http://wiki.sharparchitecture.net&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;A contrib project, led by Tom Cabanski, is well underway (&lt;a href="http://wiki.sharparchitecture.net/SharpArchContrib.ashx"&gt;http://wiki.sharparchitecture.net/SharpArchContrib.ashx&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;WCF support within S#arp Architecture has been deprecated...it&amp;#39;s moving to Sharp Contrib&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you&amp;#39;re new to S#arp Architecture and want to learn more:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div&gt;&lt;span style="WORD-SPACING:0px;FONT:13px arial;TEXT-TRANSFORM:none;COLOR:#000000;TEXT-INDENT:0px;LETTER-SPACING:normal;BORDER-COLLAPSE:separate;orphans:2;widows:2;-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;What the heck is S#arp Architecture?&lt;br /&gt;&lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2008/04/21/asp-net-mvc-best-practices-with-nhibernate-and-spring-net.aspx"&gt;http://devlicio.us/blogs/billy_mccafferty/archive/2008/04/21/asp-net-mvc-best-practices-with-nhibernate-and-spring-net.aspx&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;&lt;span style="WORD-SPACING:0px;FONT:13px arial;TEXT-TRANSFORM:none;COLOR:#000000;TEXT-INDENT:0px;LETTER-SPACING:normal;BORDER-COLLAPSE:separate;orphans:2;widows:2;-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;Give me something meatier to read!&lt;br /&gt;&lt;a href="http://www.infoq.com/articles/Sharp-Arch-Billy-McCafferty"&gt;http://www.infoq.com/articles/Sharp-Arch-Billy-McCafferty&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;&lt;span style="WORD-SPACING:0px;FONT:13px arial;TEXT-TRANSFORM:none;COLOR:#000000;TEXT-INDENT:0px;LETTER-SPACING:normal;BORDER-COLLAPSE:separate;orphans:2;widows:2;-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;But I don&amp;#39;t have a second more than 10 minutes to even read about it!&lt;br /&gt;&lt;a href="http://www.dimecasts.net/Casts/CastDetails/75"&gt;http://www.dimecasts.net/Casts/CastDetails/75&lt;/a&gt;&amp;nbsp;(a&amp;nbsp;bit dated but a useful intro)&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;&lt;span style="WORD-SPACING:0px;FONT:13px arial;TEXT-TRANSFORM:none;COLOR:#000000;TEXT-INDENT:0px;LETTER-SPACING:normal;BORDER-COLLAPSE:separate;orphans:2;widows:2;-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;Well what are other people saying about this thing?&amp;nbsp; (I read &amp;quot;doesn&amp;#39;t suck&amp;quot; somewhere...that&amp;#39;s got to count for something!)&lt;br /&gt;&lt;a href="http://www.google.com/search?hl=en&amp;amp;q=s%23arp+architecture"&gt;http://www.google.com/search?hl=en&amp;amp;q=s%23arp+architecture&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;You don&amp;#39;t say...well where can I join in on the fun?&lt;/div&gt;
&lt;div&gt;&lt;span style="WORD-SPACING:0px;FONT:13px arial;TEXT-TRANSFORM:none;COLOR:#000000;TEXT-INDENT:0px;LETTER-SPACING:normal;BORDER-COLLAPSE:separate;orphans:2;widows:2;-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;&lt;a href="http://wiki.sharparchitecture.net/"&gt;http://wiki.sharparchitecture.net/&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="http://groups.google.com/group/sharp-architecture"&gt;http://groups.google.com/group/sharp-architecture&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are also plenty of other resources out there such as Virtual ALT.NET videos, DimeCasts and lots more referenced on the wiki.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:arial;font-size:13px;"&gt;Enjoy!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="WORD-SPACING:0px;FONT:13px arial;TEXT-TRANSFORM:none;COLOR:#000000;TEXT-INDENT:0px;LETTER-SPACING:normal;BORDER-COLLAPSE:separate;orphans:2;widows:2;-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;Billy McCafferty&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=53699" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="S#arp Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Architecture/default.aspx" /></entry><entry><title>Dependency Injection 101</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2009/11/09/dependency-injection-101.aspx" /><id>/blogs/billy_mccafferty/archive/2009/11/09/dependency-injection-101.aspx</id><published>2009-11-09T22:26:00Z</published><updated>2009-11-09T22:26:00Z</updated><content type="html">&lt;p&gt;&lt;i&gt;(It looks like &lt;/i&gt;&lt;a href="http://www.kevinwilliampang.com/"&gt;&lt;i&gt;Kevin Pang&lt;/i&gt;&lt;/a&gt;&lt;i&gt; and I must have seen the same vision this weekend or had a close encounter flashback as he&amp;#39;s concurrently putting together a great series on &lt;/i&gt;&lt;a href="http://www.kevinwilliampang.com/post/Dependency-Injection-For-Dummies.aspx"&gt;&lt;i&gt;Dependency Injection for Dummies&lt;/i&gt;&lt;/a&gt;&lt;i&gt;...be sure to check out his posts for another take on this subject!)&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:large;"&gt;S&lt;/span&gt;imply put, dependency injection is the technique of supplying an object with its service dependency(s) from an external source.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re unfamiliar with dependency injection or just starting down the road of object oriented programming, the above statement might have the same effect as a mathematics book when the author puts forth a new equation that is about to be analyzed. &amp;nbsp;Symptoms may include glassy eyes, nausea, and headaches. &amp;nbsp;But when taken apart, we find that the idea of dependency injection is a much simpler concept than its grandiose title implies, and is a technique that many developers use commonly, quite naturally in many cases, without even knowing it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Encapsulation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The place to start, in understanding the motivations and implementation of dependency injection, is with the basic idea of encapsulation. &amp;nbsp;Crudely explained, &lt;/span&gt;&lt;strong&gt;&lt;i&gt;encapsulation&lt;/i&gt;&lt;/strong&gt;&lt;span&gt; is the art of determining which bits of logic belong together and putting them into discrete objects. &amp;nbsp;This allows the details of the logic to be modified within the encapsulated object without necessarily impacting the objects which use it. &amp;nbsp;Furthermore, encapsulation provides a neat and clean way to package up reusable code to be used by many other objects without introducing code duplication.&lt;/span&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/Robot.png" style="border:0;float:right;margin:5px;" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;From a real-world perspective, encapsulation is a very useful technique when building a self-aware robot to take over the world. &amp;nbsp;When building any self-aware robot, you need to start with the basics, such as mobility and sensory perception. &amp;nbsp;For example, a robot might have a stereo camera for observing the visible world, sonar sensors for detecting nearby objects around it, and touch sensors to know if it has made contact with the object it&amp;rsquo;s trying to pick up. &amp;nbsp;Just by describing it, we&amp;rsquo;re already starting to imply areas of logic that could be nicely encapsulated: &amp;nbsp;visual perception, sonar object detection, touch feedback, and mobility.&lt;/p&gt;
&lt;p&gt;Assume for a moment that we&amp;rsquo;ll bypass the practice of encapsulation and throw all of the code for these various modules directly into the primary method which uses them, a benign method we&amp;rsquo;ll call &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt;, living in a .NET assembly called CommandAndControl.dll. &amp;nbsp;The code might look something like the following:&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;public class WorldDomination {
   public void TakeOverTheWorld() {
      // Code to open USB connection to use stereo camera
      // Code to process camera images to identify humans

      // Code to open serial port to legs for moving
      // Code to send legs serial commands to move over to location of human

      // Code to generate speech to user; e.g., &amp;quot;All your leaders are belong to us&amp;quot;
   }
}&lt;/pre&gt;
&lt;p&gt;In the code, we see the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method taking on a lot of responsibilities and given explicit knowledge about components which are subject to change when the robot components are upgraded. &amp;nbsp;Some of the responsibilities which we&amp;rsquo;re burdening the method with include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Knowing the stereo camera is connected via USB and how to open a connection to it;&lt;/li&gt;
&lt;li&gt;Knowing how to process camera images to identify humans;&lt;/li&gt;
&lt;li&gt;Knowing that the legs require communications to be sent via a specific serial port;&lt;/li&gt;
&lt;li&gt;Knowing how to send low level communications to the legs as serial commands.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/C3PO.png" style="border:0;float:right;margin:5px;" alt="" /&gt;So what&amp;rsquo;s the problem you ask? &amp;nbsp;For starters, the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method would likely be a few thousand lines long and would be immensely difficult to maintain. &amp;nbsp;Let&amp;rsquo;s suppose that we upgrade the camera to use a better stereo imaging device which now receives commands wirelessly. &amp;nbsp;You&amp;rsquo;d have to carefully, tediously walk through all of the lines of &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; to find any communications with the camera, upgrade the capabilities, and test, accordingly. &amp;nbsp;But now testing is much more challenging as well. &amp;nbsp;Since all of the other code, such as the mobility code, will also run whenever we invoke &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt;, then we&amp;rsquo;ll need to make sure that the rest of the robot is in perfect working order while we switch out one component. &amp;nbsp;In other words, it&amp;rsquo;s simply not possible now to test the various components in isolation of each other. &amp;nbsp;This approach quickly turns into spaghetti code, becoming a maintenance nightmare, difficult to debug, and exhibiting decreasing code quality as time progresses. &amp;nbsp;What started out as a simple project to develop a robot to conquer the world is turning out to be about as effective as C-3PO&amp;hellip;after being blasted by a storm trooper that is.&lt;/p&gt;
&lt;p&gt;The first step that we can take, to make our robot for world domination a little easier to understand and maintain, is to encapsulate the various modules into their own, respective classes. &amp;nbsp;Let&amp;rsquo;s look at the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; code again with a sprinkling of better encapsulation by moving the camera related responsibilities to a class called &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;StereoCamera.cs&lt;/span&gt; and the mobility related responsibilities to &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;MobilePlatform.cs&lt;/span&gt;. &amp;nbsp;Furthermore, let&amp;rsquo;s move these two classes to a new class library assembly called Services.dll.&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;// Within the Services package (or class library)

public class StereoCamera {
   public void Connect() {
      // Code to open USB connection to use stereo camera
   }

   public MeaslyHuman IdentifyHumanInFieldOfVision() {
      // Code to process camera images to identify humans
   }
}

public class MobilePlatform {
   public void Connect() {
      // Code to open serial port to legs for moving
   }

   public void MoveTo(Location location) {
      // Code to send legs serial commands to move over to location 
   }
}

// Within the CommandAndControl package

public class WorldDomination {
   public void TakeOverTheWorld() {
      StereoCamera camera = new StereoCamera();
      camera.Open();
      MeaslyHuman human = camera.IdentifyHumanInFieldOfVision();

      MobilePlatform legs = new MobilePlatform();
      legs.Connect();
      legs.MoveTo(human.Location);

      // Code to generate speech to user; e.g., &amp;ldquo;All your leaders are belong to us&amp;rdquo;
   }
}&lt;/pre&gt;
&lt;p&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/DI_5F00_RefactorToPackages.png" style="border:0;float:right;margin:5px;" alt="" /&gt;As demonstrated, we introduced two new classes, &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;StereoCamera&lt;/span&gt; and &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;MobilePlatform&lt;/span&gt;, and moved the related responsibilities to those classes. &amp;nbsp;We also introduced a new package as a new class library, called Services.dll, to contain the two new classes. &amp;nbsp;The package diagram now looks as the figure shown to the right.&lt;/p&gt;
&lt;p&gt;This basic refactoring has resulted in a number of benefits:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;There are far fewer lines of code in the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method and easier to digest for whomever may be looking at it next;&lt;/li&gt;
&lt;li&gt;The &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method is now much easier to understand the overall flow of control without getting bogged down in low level details;&lt;/li&gt;
&lt;li&gt;The &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method now knows nothing about the details related to connecting to the camera via the USB connection (other than simply calling Connect), nothing about the mathematics behind processing camera images to identify humans, nor the details of opening a serial port and sending low level serial commands to move from point A to point B; and&lt;/li&gt;
&lt;li&gt;The &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;StereoCamera&lt;/span&gt; and &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;MobilePlatform&lt;/span&gt; classes are now quite reusable from a variety of other objects that will surely need to use them.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Overall, this encapsulation refactoring has led to a design that is easier to maintain and will likely reflect in the general level of maintainability and quality. &amp;nbsp;So things are better, but we still have much to improve. &amp;nbsp;I suppose we&amp;rsquo;re beginning to resemble C-3PO, but only after Chewbacca put his head on backwards. &amp;nbsp;(OK, so C-3PO didn&amp;rsquo;t have diabolical plans for world dominion, but let&amp;rsquo;s ignore that subtlety for now.) &amp;nbsp;As we proceed, we&amp;rsquo;ll examine what other areas we&amp;rsquo;ll want to improve and learn a bit more about the definition of dependency injection along the way.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Services&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Earlier, we defined dependency injection as the technique of supplying an object with its service dependency(s)&amp;hellip; &amp;nbsp;What exactly is a service dependency? &amp;nbsp;A &lt;strong&gt;&lt;i&gt;service&lt;/i&gt;&lt;/strong&gt;, in the context of dependency injection, is any object which provides access to an external resource. &amp;nbsp;In the refactoring exercise we performed on our code, and as implied by the package diagram, two services were introduced. &amp;nbsp;The first provided access to the stereo camera; the second provided access to the legs for moving the robot around. &amp;nbsp;In your own project, services may include web services, data access objects, file manipulation utilities, third party APIs, and many others.&lt;/p&gt;
&lt;p&gt;Because services depend on external resources (e.g., a physical component or database), they are inherently more complicated to test, require that the external resource be available when we test our code, and are subject to change when the underlying resource being managed changes (e.g., when the component is upgraded). &amp;nbsp;When considering these arguments, we want to remain &lt;i&gt;&lt;strong&gt;loosely coupled&lt;/strong&gt;&lt;/i&gt; to the services themselves. &amp;nbsp;To contrast, the refactored code above, even after applying encapsulation, is &lt;strong&gt;&lt;i&gt;tightly coupled&lt;/i&gt;&lt;/strong&gt; to the underlying services.&lt;/p&gt;
&lt;p&gt;To illustrate the idea of tight coupling, the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method is creating each service directly with the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;new&lt;/span&gt; keyword; i.e., the method is creating a concrete instance of the service class. &amp;nbsp;Not only does the method have a concrete dependency on the service class itself, this inherently implies that the application layer that the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class resides in must have a direct dependency on the layer which the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;StereoCamera&lt;/span&gt; service class resides in. &amp;nbsp;This layer dependency is shown as a directional line in the above package diagram from the CommandAndControl layer to the Services layer.&lt;/p&gt;
&lt;p&gt;In addition to having a concrete dependency on the service itself, the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method must also know to call &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;Open&lt;/span&gt; for the respective services (and presumably &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;Close&lt;/span&gt;); i.e., the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method needs to know how to create and initialize the services before it&amp;rsquo;s even able to use them. &amp;nbsp;These are further responsibilities which this method should not be concerned with.&lt;/p&gt;
&lt;p&gt;Ideally, the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method would be loosely coupled to the underlying services and wouldn&amp;rsquo;t have to know about such low level details of the service itself outside of calling methods such as &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;IdentifyHumanInFieldOfVision&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Coding to Interface&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The first step we can take towards making &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; less tightly coupled to the service classes is to have the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method code to the interfaces of the services rather than to the concrete classes themselves. &amp;nbsp;One might argue against introducing two new interfaces for the objects; as we add new objects, we inherently introduce additional complexity to the overall system. &amp;nbsp;Also, with interfaces, not only are we introducing a small amount of additional complexity to the overall system, but we&amp;rsquo;re also introducing indirection. &amp;nbsp;In other words, in order for someone to fully understand what&amp;rsquo;s going on at runtime, the developer will need to determine which concrete object is being used under the interface. &amp;nbsp;It shouldn&amp;rsquo;t be difficult to do so, but it does add time to understanding the overall system. &amp;nbsp;These are fair arguments and ones not to be ignored. &amp;nbsp;Whenever we are faced with a decision that will lead to a better design at the expense of added complexity, we carefully need to consider if the benefits of refactoring outweigh the penalties of added complexity. &amp;nbsp;In this case, coding to interface (and another refactoring we will do next) will lead to a more maintainable system, provide a means to readily unit test the various components in isolation, and will result in a more loosely coupled design.&lt;/p&gt;
&lt;p&gt;Accordingly, we simply need to add two interfaces to the Services package, have the service classes implement them, and change the references in the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method to invoke the interfaces rather than the classes themselves. &amp;nbsp;But as we&amp;rsquo;ll see, this next refactoring still leaves us tightly coupled to the service classes. &amp;nbsp;Let&amp;rsquo;s look at the code.&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;// Within the Services package

public interface IStereoCamera {
   void Connect();
   MeaslyHuman IdentifyHumanInFieldOfVision();
}

public interface IMobilePlatform {
   void Connect();
   void MoveTo(Location location);
}

public class StereoCamera : IStereoCamera {
   public void Connect() {
      // Code to open USB connection to use stereo camera
   }

   public MeaslyHuman IdentifyHumanInFieldOfVision() {
      // Code to process camera images to identify humans
   }
}

public class MobilePlatform : IMobilePlatform {
   public void Connect() {
      // Code to open serial port to legs for moving
   }

   public void MoveTo(Location location) {
      // Code to send legs serial commands to move over to location 
   }
}

// Within the CommandAndControl package

public class WorldDomination {
   public WorldDomination() {
      camera = new StereoCamera();
      legs = new MobilePlatform();
   }

   public void TakeOverTheWorld() {
      camera.Open();
      MeaslyHuman human = camera.IdentifyHumanInFieldOfVision();

      legs.Connect();
      legs.MoveTo(human.Location);

      // Code to generate speech to user; e.g., &amp;ldquo;All your leaders are belong to us&amp;rdquo;
   }

   private readonly IStereoCamera camera;
   private readonly IMobilePlatform legs;
}&lt;/pre&gt;
&lt;p&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/DI_5F00_AddInterfaces.png" style="border:0;float:right;margin:5px;" alt="" /&gt;In the code above, note that the two interfaces were introduced and now the constructor of the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class now handles creating the service instances and setting private members, represented as interfaces, to the service instances. &amp;nbsp;The package diagram reflecting this refactoring is shown at right.&lt;/p&gt;
&lt;p&gt;Even though the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;TakeOverTheWorld&lt;/span&gt; method is now communicating with the interfaces of the services, the class containing is still tightly coupled to the service classes due to the local instantiation of the classes within its constructor. &amp;nbsp;To further alleviate this coupling, we&amp;rsquo;re going to have to move a step &amp;ldquo;up&amp;rdquo; and introduce a package which will be responsible for creating the service dependencies and injecting them into the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class. &amp;nbsp;Yes, we&amp;rsquo;re finally ready to see dependency injection in action.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Service Initialization and Injection&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/DI_5F00_RefactorToDI.png" style="border:0;float:right;margin:5px;" alt="" /&gt;While the previous refactorings have taken us closer towards a more loosely coupled design, we still need to remove the object creation from the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class altogether. &amp;nbsp;By introducing a new package which will contain the robot system&amp;rsquo;s &amp;ldquo;main&amp;rdquo; program, we can leverage that layer to also initialize the service dependencies and inject them into the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class. &amp;nbsp;Let&amp;rsquo;s look at the code of the new package, called Robot, along with modifications to the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class. &amp;nbsp;(The Services package, and the interfaces and classes therein, will remain unchanged.) &amp;nbsp;But before looking at the code, study the package diagram at right to see where the classes live and take particular note of the direction of package dependencies. &amp;nbsp;There&amp;rsquo;s one dependency that&amp;rsquo;s still problematic, which we&amp;rsquo;ll discuss after the code.&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;// Within the CommandAndControl package

public class WorldDomination {
   public WorldDomination(IStereoCamera camera, IMobilePlatform legs) {
      this.camera = camera;
      this.legs = legs;
   }

   public void TakeOverTheWorld() {
      // Same as before
   }

   private readonly IStereoCamera camera;
   private readonly IMobilePlatform legs;
}

// Within the new Robot package

public class Program {
   // This is the &amp;ldquo;main&amp;rdquo; method of the entire robot application.
   // I.e., it&amp;rsquo;s the method that gets called to kick off the application.
   public void Main() {
      IStereoCamera camera = new StereoCamera();
      IMobilePlatform legs = new MobilePlatform();

      WorldDomination worldDomination = new WorldDomination(camera, legs);

      worldDomination.TakeOverTheWorld();
   }
}&lt;/pre&gt;
&lt;p&gt;In the code above, the Main method initializes the services and &amp;ldquo;injects&amp;rdquo; them into the constructor of the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class. &amp;nbsp;As you can see, the injection of service dependencies into a class is really nothing more than passing the service instances as arguments to the class instantiation. &amp;nbsp;So when it comes right down to it, the intimidating phrase &amp;ldquo;dependency injection&amp;rdquo; is really quite simple in practice. &amp;nbsp;But there&amp;rsquo;s still a problem in our implementation that could lead to design &amp;ldquo;rot&amp;rdquo; over time. &amp;nbsp;The CommandAndControl package still has a dependency on the Services package. &amp;nbsp;This is bad because during later phases of the project, without a high level of discipline, developers may forego injecting the service dependencies into the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class, or other classes in the CommandAndControl package and simply instantiate the service classes directly using the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;new&lt;/span&gt; keyword. &amp;nbsp;This would make all the work we&amp;rsquo;ve done to this point to support dependency injection a moot point. &amp;nbsp;To enforce our design, we need to introduce a new pattern known as &lt;strong&gt;&lt;i&gt;dependency inversion&lt;/i&gt;&lt;/strong&gt;, also known as &lt;strong&gt;&lt;i&gt;separated interface&lt;/i&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Separated Interface&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/DI_5F00_RefactorToSeparatedInterface.png" style="border:0;float:right;margin:5px;" alt="" /&gt;To enforce our design, and to eliminate the package dependency from CommandAndControl to Services, we will move the service interfaces to the CommandAndControl package and reverse, or invert, the dependency direction between the two packages. &amp;nbsp; Accordingly, the refactoring steps to do this include:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Copy the service interfaces from the Services package to the CommandAndControl package.&lt;/li&gt;
&lt;li&gt;Change the interface references in the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class to refer to the interfaces now found within the CommandAndControl package.&lt;/li&gt;
&lt;li&gt;Remove the package dependency from CommandAndControl to Services. &amp;nbsp;Now add a package dependency from Services to CommandAndControl. &amp;nbsp;(In C#, this would be a matter of deleting the assembly reference from the CommandAndControl class library and adding a new assembly reference from the Services class library.)&lt;/li&gt;
&lt;li&gt;Modify the service classes, StereoCamera and MobilePlatform, to implement the interfaces now found in the CommandAndControl package.&lt;/li&gt;
&lt;li&gt;Modify the Main method, in the Main class to use the interfaces found within the CommandAndControl class.&lt;/li&gt;
&lt;li&gt;Delete the obsolete interfaces from the Services package.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;With the &lt;strong&gt;&lt;i&gt;separated interface&lt;/i&gt;&lt;/strong&gt; pattern implemented, the CommandAndControl package is only aware of the service interfaces without having any dependency, whatsoever, on the service implementations. &amp;nbsp;As performed in the previous refactoring, the CommandAndControl package, the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class specifically, is being given its service dependencies via &lt;strong&gt;&lt;i&gt;constructor injection&lt;/i&gt;&lt;/strong&gt; via initialization code found in the Robot&amp;rsquo;s main method.&lt;/p&gt;
&lt;p&gt;With this, we have implemented a basic example, including all the required pieces, for performing dependency injection. &amp;nbsp;It introduced a number of new pieces to the overall application, but our design is now loosely coupled to the underlying services and enables us easily swap out services with new implementations, without having to recompile the CommandAndControl package, and provides the ability for &lt;strong&gt;&lt;i&gt;test doubles&lt;/i&gt;&lt;/strong&gt;, such as mocks, stubs, and fakes, to be injected into the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class when unit testing. &amp;nbsp; (Unit testing with test doubles is beyond the scope of this article, but I encourage you to read &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;http://xunitpatterns.com/Test%20Double.html &lt;/a&gt;to begin learning more about this subject.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dependency Injection / Inversion of Control (IoC) Containers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s one final topic which is appropriate to discuss when describing dependency injection: &amp;nbsp;IoC containers. &amp;nbsp;(Dependency injection is also known as &amp;ldquo;Inversion of Control,&amp;rdquo; as originally described by Robert Martin. &amp;nbsp;Interestingly enough, the development community typically uses the phrase &amp;ldquo;dependency injection&amp;rdquo; when talking about the technique itself and &amp;ldquo;IoC&amp;rdquo; when talking about dependency injection containers. &amp;nbsp;But rest assured, there is no difference whatsoever between &amp;ldquo;Dependency Injection&amp;rdquo; and &amp;ldquo;Inversion of Control (IoC).&amp;rdquo;) &amp;nbsp;An IoC Container is a utility which is leveraged to initialize and inject dependencies into the objects that use them. &amp;nbsp;While it is a bit more difficult to visualize than the &amp;ldquo;manual&amp;rdquo; dependency injection we performed in the previous refactoring step, an IoC Container is nothing more than an object factory which initializes an object and satisfies its service dependencies in the process.&lt;/p&gt;
&lt;p&gt;With most IoC Containers (e.g., Ninject, Castle Windsor, LinFu, and many others), there are only a couple of steps required to setup and use the IoC Container:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Register all of the classes which may be injected into another object. &amp;nbsp;In our carefree efforts for world domination via our robot, this would include, at minimum, registering the service classes MobilePlatform and StereoCamera. &amp;nbsp;While each IoC Container has its own means of registering a class for later retrieval, the underlying mechanism is often as simple as adding the service class&amp;rsquo; object type to a hashtable for later lookup. &amp;nbsp; If we were using Castle Windsor with our project, the registration lines would look like the following:
&lt;pre name="code" class="c-sharp"&gt;container.AddComponent(&amp;quot;camera&amp;quot;, 
   typeof(IStereoCamera), typeof(StereoCamera));
container.AddComponent(&amp;quot;mobilePlatform&amp;quot;, 
   typeof(IMobilePlatform), typeof(MobilePlatform));
container.AddComponent(&amp;quot;camera&amp;quot;, typeof(WorldDomination));&lt;/pre&gt;
Note that the last line registers the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class itself.  In doing so, we can let the IoC Container take care of the work of instantiating it with its service dependencies already injected.  We don&amp;rsquo;t &lt;i&gt;have&lt;/i&gt; to hand off instantiation of this class to the IoC Container, but it provides a useful benefit.  Suppose the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class needs a third service dependency provided via its constructor somewhere down the road.  If we aren&amp;rsquo;t using an IoC Container to manage its creation, we&amp;rsquo;d have to look for every place where the class is created, via the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;new&lt;/span&gt; keyword, and pass in the extra dependency.  If we leave its creation to the IoC Container, then we simply add the new service interface parameter to its constructor and the IoC Container will take care of the rest, assuming you&amp;rsquo;ve registered the third dependency  as well.&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Invoke the IoC Container to instantiate a registered object with its dependencies already injected. &amp;nbsp; For our project, the following demonstrates getting a handle to the &lt;span style="font-family:&amp;#39;Courier New&amp;#39;, Courier, monospace;"&gt;WorldDomination&lt;/span&gt; class:
&lt;pre name="code" class="c-sharp"&gt;WorldDomination worldDomination = (WorldDomination) 
   ServiceLocator.Current.GetService(typeof(WorldDomination));&lt;/pre&gt;
This line of code tells the IoC Container to return an instance of the requested class with its dependencies injected.  This actually uses an IoC Container known as Common Service Locator.  Since almost all IoC Containers do the same actions &amp;ndash; registering and instantiating objects &amp;ndash; the major IoC Container authors worked together to establish a common interface for all IoC Containers.  In this way, you can register your objects with your favorite IoC Container and then register the IoC Container itself with the Common Service Locator.  It&amp;rsquo;s enough to make your head spin, but leads to further decoupling to the IoC Container itself so that you can easily swap out one for another (e.g., Castle Windsor for Ninject) without modifying the code which depends on the IoC Container.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;How you setup and use an IoC Container depends on the environment that you&amp;rsquo;re developing within. &amp;nbsp;For example, if you&amp;rsquo;re developing an ASP.NET MVC application, you can use an IoC Container to instantiate the controllers, injecting their dependencies via the controller&amp;rsquo;s constructor. &amp;nbsp;For example, the following two lines of code, within the Global.asax.cs file, instruct the ASP.NET MVC framework to use Castle Windsor to instantiate controllers:&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;IWindsorContainer container = new WindsorContainer();
ControllerBuilder.Current.SetControllerFactory(
   new WindsorControllerFactory(container));&lt;/pre&gt;
&lt;p&gt;The only thing left is to register the controller dependencies that they require for their constructors and the IoC Container takes care of the rest.&lt;/p&gt;
&lt;p&gt;While this will be enough to gain a basic understanding of what an IoC Container is and how it is used, a bit more studying will be required to learn how to actually set up and use one. &amp;nbsp;For more information about IoC Containers, including examples of setup and usage:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common Service Locator: &amp;nbsp;&lt;a href="http://www.codeplex.com/CommonServiceLocator"&gt;http://www.codeplex.com/CommonServiceLocator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Castle Windsor Tutorial: &amp;nbsp;&lt;a href="http://www.castleproject.org/container/gettingstarted/"&gt;http://www.castleproject.org/container/gettingstarted/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;LinFu in 5 Minutes: &amp;nbsp;&lt;a href="http://www.codeproject.com/KB/cs/LinFu_IOC.aspx"&gt;http://www.codeproject.com/KB/cs/LinFu_IOC.aspx&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Using Ninject: &amp;nbsp;&lt;a href="http://ninject.codeplex.com/wikipage?title=user%20guide&amp;amp;ProjectName=ninject"&gt;http://ninject.codeplex.com/wikipage?title=user%20guide&amp;amp;ProjectName=ninject&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;StructureMap: &amp;nbsp;&lt;a href="http://structuremap.sourceforge.net"&gt;http://structuremap.sourceforge.net&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;A classic IoC Overview: &amp;nbsp;&lt;a href="http://martinfowler.com/articles/injection.html"&gt;http://martinfowler.com/articles/injection.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Using IoC in a project: &amp;nbsp;&lt;a href="http://wiki.sharparchitecture.net/"&gt;http://wiki.sharparchitecture.net/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finally, for a much more thorough analysis of many of the concepts described above, I highly recommend reading Robert Martin&amp;#39;s&amp;nbsp;&lt;a href="http://www.amazon.com/Agile-Principles-Patterns-Practices-C/dp/0131857258/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1257809514&amp;amp;sr=1-1"&gt;Agile Principles, Patterns, and Practices in C#&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/RobotInvasion.png" style="border:0;float:right;margin:5px;" alt="" /&gt;Regardless of whether you&amp;rsquo;re looking to take over the world with a dominion of evil robots or simply trying to build a better web app, dependency injection and the use of an IoC Container can lead to an easy to maintain application that is loosely coupled to service dependencies. &amp;nbsp;These attributes naturally lead to better quality and happier developers. &amp;nbsp;(Both of which I would put squarely into a category known as &amp;ldquo;good.&amp;rdquo;) &amp;nbsp;But while these techniques provide many benefits, they also introduce a bit more complexity; especially when going from &amp;ldquo;manual&amp;rdquo; dependency injection, as we did in the coding example, to the use of an IoC Container. &amp;nbsp;Accordingly, your project team should carefully consider the benefits and added complexity to determine if it&amp;#39;s right for your project. &amp;nbsp;While the use of an IoC Container would be a bit overkill for a very simple application, its use is &lt;i&gt;obviously &lt;/i&gt;pivotal in creating self-aware robotic legions for conquering the world.&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;br /&gt;&lt;a href="http://wiki.sharparchitecture.net/"&gt;http://wiki.sharparchitecture.net/&lt;/a&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=53587" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Architecture/default.aspx" /><category term="Patterns" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Patterns/default.aspx" /></entry><entry><title>Speaking at Virtual ALT.NET Tonight</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2009/11/04/speaking-at-virtual-alt-net-tonight.aspx" /><id>/blogs/billy_mccafferty/archive/2009/11/04/speaking-at-virtual-alt-net-tonight.aspx</id><published>2009-11-04T17:07:00Z</published><updated>2009-11-04T17:07:00Z</updated><content type="html">&lt;p&gt;If you&amp;#39;re interested in...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Finding out what&amp;#39;s new in &lt;a href="http://wiki.sharparchitecture.net/"&gt;S#arp Architecture&lt;/a&gt; Q3 2009,&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Hearing about what the &lt;a href="http://wiki.sharparchitecture.net/SharpArchContrib.ashx"&gt;Contrib&lt;/a&gt; project has to offer,&lt;/li&gt;
&lt;li&gt;Going into a couple of S#arp topics in detail, such as...
&lt;ul&gt;
&lt;li&gt;Getting up and running quickly,&lt;/li&gt;
&lt;li&gt;Handling object associations,&lt;/li&gt;
&lt;li&gt;Using an &lt;a href="http://martinfowler.com/eaaCatalog/serviceLayer.html"&gt;Application Services&lt;/a&gt; layer and&lt;/li&gt;
&lt;li&gt;Practical implementation of &lt;a href="http://devlicio.us/blogs/casey/archive/2009/02/12/ddd-command-query-separation-as-an-architectural-concept.aspx"&gt;Command/Query Separation&lt;/a&gt; (or &amp;quot;Domain/Reporting Separation&amp;quot; from my perspective).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Then join us tonight at &lt;a href="http://virualaltnet.blogspot.com/2009/11/billy-mccafferty-talking-about-sarp.html"&gt;Virtual ALT.NET&lt;/a&gt;, 9 - 11 PM Eastern time.&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=53377" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="S#arp Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Architecture/default.aspx" /></entry><entry><title>The HornGet Project: Bringing "apt-get install" to .NET Projects</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2009/10/29/the-horn-project-bringing-quot-apt-get-install-quot-to-net-projects.aspx" /><id>/blogs/billy_mccafferty/archive/2009/10/29/the-horn-project-bringing-quot-apt-get-install-quot-to-net-projects.aspx</id><published>2009-10-30T02:34:00Z</published><updated>2009-10-30T02:34:00Z</updated><content type="html">&lt;p&gt;For years I used nothing but Windows. &amp;nbsp;When I started getting into robotics (for fun - let me know if you&amp;#39;d like me to do it professionally ;), I had to dive into the Linux world. &amp;nbsp;After only a couple of weeks with Ubuntu, I was stunned at how far behind our Microsoft-following community is in some respects. &amp;nbsp;Take Linux&amp;#39;s &amp;quot;apt-get install&amp;quot; command, for example. &amp;nbsp;Running this simple command, followed by an application name, finds the referenced application on the &amp;quot;Information Super Highway,&amp;quot; downloads the binaries and installs it locally. &amp;nbsp;No biggie, right? &amp;nbsp;Well it also looks to see what dependencies the application has, downloads the latest of all of them, and installs them as well before installing the target application. &amp;nbsp;It does all of that with just one command line (and sometimes even successfully! ;).&lt;/p&gt;
&lt;p&gt;There are two scenarios that come to mind where something like &amp;quot;apt-get install&amp;quot; is tremendously useful when developing a project. &amp;nbsp;The first, and most akin to the intent of &amp;quot;apt-get install&amp;quot; is when you&amp;#39;re managing an open source project (e.g., &lt;a href="http://wiki.sharparchitecture.net/"&gt;S#arp Architecture&lt;/a&gt;) and want to provide an easy way for developers to get the latest of the project while concurrently grabbing the latest of its dependencies (e.g., Rhino tools, NHibernate, Fluent NHibernate, MVCConrib) in the process. &amp;nbsp;The second scenario is if you have a project which has a large number of dependencies and dread checking for updates to the project dependencies. &amp;nbsp;Which dependencies do you download? &amp;nbsp;Where do you find the source? &amp;nbsp;Which order should you build them in? &amp;nbsp;How do you know, except through a lot of tedious snooping, which secondary dependencies are also involved in rebuilding the project dependencies?&lt;/p&gt;
&lt;p&gt;The &lt;a href="http://code.google.com/p/hornget/"&gt;HornGet project&lt;/a&gt; is attempting (and succeeding I might add, even in its pre-beta form) to greatly ease the burden of updating dependencies of complex projects. &amp;nbsp;With Horn installed and configured, it only takes a single command line to get the latest of your favorite OSS project or to rebuild your project&amp;#39;s dependencies. &amp;nbsp;Similarly to &amp;quot;apt-get install,&amp;quot; you simply tell Horn what you&amp;#39;d like to have built and it does the rest. &amp;nbsp;There&amp;#39;s a full listing of all of Horn&amp;#39;s supported packages found at&amp;nbsp;&lt;a href="http://code.google.com/p/hornget/wiki/SupportedPackages"&gt;http://code.google.com/p/hornget/wiki/SupportedPackages&lt;/a&gt;. &amp;nbsp;As you can see, Horn supports a wide variety of projects such as NHibernate, Castle, LinFu, Ninject, AutoMapper, Subsonice, MVCContib, S#arp Architecture, and many others. &amp;nbsp;Something quite unique about Horn is that it even checks for indirect, bi-directional library dependencies. &amp;nbsp;For instance, NHibernate depends on a Castle project while one of the Castle projects depends on NHibernate. &amp;nbsp;Horn is smart enough (through config files) to know which projects to build in which order to resolve such scenarios.&lt;/p&gt;
&lt;p&gt;The best way to experience and appreciate Horn is to try it out. &amp;nbsp;The following steps demonstrate using Horn to automatically download and build the latest of NHibernate with updated dependencies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Install &lt;a href="http://tortoisesvn.net/downloads"&gt;TortoiseSVN&lt;/a&gt; (if you haven&amp;#39;t already) to download Horn and to be used during the build process.&lt;/li&gt;
&lt;li&gt;Install &lt;a href="http://code.google.com/p/msysgit/downloads/list"&gt;msysgit&lt;/a&gt; as some of the dependencies will be downloaded from from Git distributed source repositories.&lt;/li&gt;
&lt;li&gt;Make sure that &amp;quot;C:\Program Files\Git\bin&amp;quot; and&amp;nbsp;&amp;quot;C:\Program Files\Git\cmd&amp;quot;&amp;nbsp;are included in your Environment Variable&amp;#39;s PATH (and add them if not)&lt;/li&gt;
&lt;li&gt;Install &lt;a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx"&gt;Windows PowerShell&lt;/a&gt; (used during the dependency build process - it&amp;#39;s included in Win 2008 automatically)&lt;/li&gt;
&lt;li&gt;Open a PowerShell command prompt and run &amp;quot;set-ExecutionPolicy RemoteSigned&amp;quot; to allow Horn to invoke PowerShell commands used during the dependency build process.&lt;/li&gt;
&lt;li&gt;Use SVN command line or TortoiseSVN to get latest of&amp;nbsp;&lt;a href="http://hornget.googlecode.com/svn/trunk/"&gt;http://hornget.googlecode.com/svn/trunk/&lt;/a&gt; to a folder we&amp;#39;ll call HornRoot&lt;/li&gt;
&lt;li&gt;(Here&amp;#39;s the only&amp;nbsp;kludgey&amp;nbsp;part, which I&amp;#39;m sure will be cleverly resolved very soon...) &amp;nbsp;Open /HornRoot/src/Horn.Console/Program.cs in Notepad, or whatever, and modify, under GetRootFolderPath(), the line &amp;quot;var ret = new DirectoryInfo(rootFolder);&amp;quot; to be &amp;quot;var ret = new DirectoryInfo(@&amp;quot;C:\...\HornRoot\&amp;quot; + PackageTree.RootPackageTreeName);&amp;quot; and save/close the file - replacing C:\...HornRoot with a real file path. &amp;nbsp;&lt;strong&gt;NOTE&lt;/strong&gt;: &amp;nbsp;Make sure there are &lt;strong&gt;NO spaces&lt;/strong&gt; in the file path that you provide. &amp;nbsp;PowerShell abhors&amp;nbsp;spaces in file paths.&lt;/li&gt;
&lt;li&gt;Open a command prompt, cd to the /HornRoot/src folder and run &amp;quot;hornbuild.bat&amp;quot;...you be triumphantly rewarded with a BUILD SUCCEEDED message...but that&amp;#39;s just the Horn project.&lt;/li&gt;
&lt;li&gt;In the same command prompt, cd to /HornRoot/src/build/net-3.5/debug and run &amp;quot;horn&amp;quot;
&lt;ul&gt;
&lt;li&gt;You should see the Horn usage guidelines...that&amp;#39;s a good sign.&lt;/li&gt;
&lt;li&gt;At this point, Horn is now installed, ready and configured to build any of the supported packages.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;As an example, run &amp;quot;horn -install:nhibernate&amp;quot; (and &lt;strong&gt;be patient as it&amp;#39;ll take &lt;/strong&gt;&lt;strong&gt;some&amp;nbsp;&lt;/strong&gt;&lt;strong&gt;to run&lt;/strong&gt;...30 minutes to an hour or more for a project with many dependencies is &lt;span style="text-decoration:line-through;"&gt;possible&lt;/span&gt; probable)
&lt;ul&gt;
&lt;li&gt;After executing this one liner, horn will spit out a whole bunch of &amp;quot;working......&amp;quot; statements as it figures out what NHibernate&amp;#39;s dependencies are, downloads the source of the dependencies, builds them, and then downloads and builds&amp;nbsp;NHibernate&amp;nbsp;with all of the updated dependencies.&lt;/li&gt;
&lt;li&gt;Keep waiting...Horn is doing a &lt;strong&gt;LOT &lt;/strong&gt;of tedious work for you right now. &amp;nbsp;Don&amp;#39;t be surprised if you see very long strings of &amp;quot;working......&amp;quot; statements at a time, throughout the build process; this is expected as it downloads each project dependency and builds them, accordingly. &amp;nbsp;Go get another cup of coffee or something.&lt;/li&gt;
&lt;li&gt;Assuming the OSS world is in a Zen-like state with each other you&amp;#39;ll get a number of BUILD SUCCEEDED messages for each project dependency and then eventually get a BUILD SUCCEEDED message for the target project itself.&lt;/li&gt;
&lt;li&gt;Horn outputs its results (the updated DLLs) into /HornRoot/.horn/result. &amp;nbsp;Don&amp;#39;t worry if there are more DLLs that you expected; just grab the updated ones that are applicable to your project and you&amp;#39;re good to go.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you&amp;#39;ve ever tried to update a bunch of project dependencies, you&amp;#39;ll appreciate the enormous time savings in using Horn to pull together the dependencies for you. &amp;nbsp;Doing this manually usually takes me at least a few hours to get latest of all dependencies, build them, and build them with S#arp Architecture. &amp;nbsp;With Horn, it takes a one liner and a coffee break.&lt;/p&gt;
&lt;p&gt;How does it do this magic you ask? &amp;nbsp;Good question! &amp;nbsp;Each project that Horn supports has a Boo configuration file; e.g., the S#arp Architecture Horn description is at&amp;nbsp;&lt;a href="http://hornget.googlecode.com/svn/trunk/package_tree/frameworks/sharp.architecture/sharp.architecture.boo"&gt;http://hornget.googlecode.com/svn/trunk/package_tree/frameworks/sharp.architecture/sharp.architecture.boo&lt;/a&gt;. &amp;nbsp;The file speaks for itself in describing what dependencies are necessary for the project and where to find the source of S#arp Architecture itself. &amp;nbsp;So when you run &amp;quot;horn -install:sharp.architecture&amp;quot; (which takes a very long while mind you) Horn references the respective project description file and off it goes. &amp;nbsp;It&amp;#39;s really quite a remarkable feat that Horn has pulled off.&lt;/p&gt;
&lt;p&gt;Horn is still maturing - it&amp;#39;s not even beta yet - but it&amp;#39;s quite a time saver already. &amp;nbsp;So if you have ideas, would like to get involved, or have a project that you&amp;#39;d like to have added to the roster, be sure to let the Horn team know.&lt;/p&gt;
&lt;p&gt;For more Horn:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Project home: &amp;nbsp;&lt;a href="http://code.google.com/p/hornget/"&gt;http://code.google.com/p/hornget/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Discussion forum: &amp;nbsp;&lt;a href="http://groups.google.co.uk/group/horn-development"&gt;http://groups.google.co.uk/group/horn-development&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=53217" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="Development Tools" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Development+Tools/default.aspx" /><category term="Agile Development" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Agile+Development/default.aspx" /><category term="S#arp Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Architecture/default.aspx" /></entry><entry><title>Where are we with P = NP?</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2009/10/08/where-are-we-with-p-np.aspx" /><id>/blogs/billy_mccafferty/archive/2009/10/08/where-are-we-with-p-np.aspx</id><published>2009-10-08T21:06:00Z</published><updated>2009-10-08T21:06:00Z</updated><content type="html">&lt;p&gt;&lt;a href="http://www.kurzweilai.net/news/frame.html?main=/news/news_single.html?id%3D11237"&gt;KurzweilAI.net&lt;/a&gt; led me to a terrific &lt;a href="http://cacm.acm.org/magazines/2009/9/38904-the-status-of-the-p-versus-np-problem/fulltext"&gt;article&lt;/a&gt; concerning the current status of answering the question of whether or not P = NP.&amp;nbsp; As the author, Lance Fortnow, aptly notes, &amp;quot;The P versus NP problem has gone from an interesting problem related to logic to perhaps the most fundamental and important mathematical question of our time, whose importance only grows as computers become more powerful and widespread.&amp;quot;&lt;br /&gt;&lt;br /&gt;The topic of P versus NP is certainly not a new one but one which is only vaguely familiar to many software developers.&amp;nbsp; If nothing else, gaining a better appreciation for P versus NP problems can better prepare you for recognizing when the problem you are working on turns out to be NP-complete and arm you with a &lt;a href="http://cacm.acm.org/magazines/2009/9/38904-the-status-of-the-p-versus-np-problem/fulltext#body-5"&gt;strong toolset&lt;/a&gt; for delivering an adequate solution.&amp;nbsp; But taking the appreciation a bit further, the question of whether or not P = NP sheds an aesthetic beauty on the fact that our software development practices are so firmly embedded in the realm of mathematics...and that our code is only a shallow shell around that world.&amp;nbsp; Any time spent gaining a better understanding of the &lt;a href="http://en.wikipedia.org/wiki/The_Art_of_Computer_Programming"&gt;underlying&lt;/a&gt; &lt;a href="http://www.amazon.com/Concrete-Mathematics-Foundation-Computer-Science/dp/0201558025"&gt;mathematics&lt;/a&gt; and &lt;a href="http://www.amazon.com/Introduction-Algorithms-Third-Thomas-Cormen/dp/0262033844"&gt;algorithmic&lt;/a&gt; nature of programming will quickly enlighten you to seeing development in a completely different light.&lt;br /&gt;&lt;br /&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=52476" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="DeepThoughtsByJackHandey" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/DeepThoughtsByJackHandey/default.aspx" /></entry><entry><title>A S#arp Project Case Study</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2009/10/08/a-s-arp-project-case-study.aspx" /><id>/blogs/billy_mccafferty/archive/2009/10/08/a-s-arp-project-case-study.aspx</id><published>2009-10-08T18:55:00Z</published><updated>2009-10-08T18:55:00Z</updated><content type="html">&lt;p&gt;Like &lt;a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/10/08/an-automapper-success-story.aspx"&gt;Jimmy Bogard&lt;/a&gt;, I was pleased to read &lt;a href="http://groups.google.com/group/sharp-architecture/browse_thread/thread/9df324a3dc36decb"&gt;a post from Howard van Rooijen&lt;/a&gt; on the S#arp Architecture forums concerning the use of &lt;a href="http://www.sharparchitecture.net/"&gt;S#arp Architecture&lt;/a&gt; and Bogard&amp;#39;s &lt;a href="http://www.codeplex.com/AutoMapper"&gt;AutoMapper&lt;/a&gt; in the timely development of &lt;a href="http://www.fancydressoutfitters.co.uk" rel="nofollow"&gt;http://www&lt;/a&gt;&lt;a href="http://www.fancydressoutfitters.co.uk"&gt;.fancydressoutfitters.co.uk&lt;/a&gt;.&amp;nbsp; With a big thanks to Howard, he has also taken the time to provide a number of great blog posts which walk the reader through his experiences, challenges, solutions, and lessons learned with respect to developing a commercial, greenfield application using ASP.NET MVC.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;Hello&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:13px;border-collapse:collapse;white-space:pre;"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;S#arp Architecture Community&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;,&lt;/span&gt;&lt;a href="http://www.sharparchitecture.net/"&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Components.UserFiles/00.00.00.21.06/logo_5F00_web_5F00_gif.gif" style="border:0;float:right;" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div style="padding-left:30px;"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;I just wanted to let you know that a S#arp Architecture based&amp;nbsp;e-commerce&amp;nbsp;site has just been released into the wild:&amp;nbsp;&lt;/span&gt;&lt;a target="_blank" href="http://www.fancydressoutfitters.co.uk/"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;http://www.&lt;/span&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;fancydressoutfitters.co.uk&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="padding-left:30px;"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="padding-left:30px;"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;For those who are interested - here&amp;#39;s a little background:&lt;/span&gt;&lt;/div&gt;
&lt;div style="padding-left:30px;"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="padding-left:30px;"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;- We ran the project using Scrum (managing the project with &lt;/span&gt;&lt;a target="_blank" href="http://scrumforteamsystem.com/"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;Scrum for Team System&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="padding-left:30px;"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;- Delivered in 20 weeks: 10 x 2 week iterations&lt;br /&gt;- We extended the core S#arp Architecture framework (support for multiple databases, ViewModels etc...)&lt;br /&gt;
- Integrated other OSS projects into the solution:&lt;br /&gt;&amp;nbsp;&amp;nbsp;-&amp;nbsp;N2CMS&lt;br /&gt;&amp;nbsp;&amp;nbsp;-&amp;nbsp;Spark View Engine&lt;br /&gt;&amp;nbsp;&amp;nbsp;-&amp;nbsp;xVal Validation Framework&lt;br /&gt;&amp;nbsp;&amp;nbsp;-&amp;nbsp;AutoMapper (for entity / view model mapping)&lt;br /&gt;&amp;nbsp;&amp;nbsp;-&amp;nbsp;PostSharp (for cross cutting concerns - logging, performance counters, caching)&lt;br /&gt;

&amp;nbsp;&amp;nbsp;-&amp;nbsp;MBUnit / Gallio for our BDD Framework&lt;br /&gt;&amp;nbsp;&amp;nbsp;-&amp;nbsp;Solr &amp;amp; SolrNet for Search and Faceted Navigation&lt;br /&gt;-&amp;nbsp;Over 700 BDD Specs&lt;br /&gt;-&amp;nbsp;Solution
performs very well - 1000 concurrent users per web server, generating
around 180 pages per second across 2x single quad core 64bit servers.&lt;br /&gt;

&lt;/span&gt;
&lt;div&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;If
you&amp;#39;re really interested two of the developers in the team (James
Broome &amp;amp; Jon George who have both posted to this forum) &amp;amp;
myself have blogged our thoughts &amp;amp; experiences developing this
solution and working with S#arp Architecture:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size:15px;"&gt;&lt;span style="font-family:&amp;#39;Times New Roman&amp;#39;;font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;font-size-adjust:none;font-stretch:normal;"&gt;&lt;span style="font-size:small;"&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/jamesbroome/archive/2009/08/24/asp-net-mvc-separation-of-concerns-amongst-team-members.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;ASP.NET
MVC - Separation of concerns amongst team members&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/jamesbroome/archive/2009/09/16/asp-net-mvc-controllers-bdd-the-perfect-match-part-1-the-homecontroller.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;ASP.NET
MVC - Controllers + BDD = The perfect match? (Part 1: The HomeController)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/jamesbroome/archive/2009/09/23/asp-net-mvc-controllers-bdd-the-perfect-match-part-2-log-on-and-log-off-with-the-accountcontroller.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;ASP.NET
MVC - Controllers + BDD = The perfect match? (Part 2: Log On and Log Off with
the AccountController)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/jamesbroome/archive/2009/10/06/the-importance-of-conventions-from-asp-net-mvc-to-a-successful-project-team.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;The
importance of conventions &amp;ndash; from ASP.NET MVC to a successful project team&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/howardvanrooijen/archive/2009/09/13/the-value-and-benefits-of-asp-net-mvc.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;The
Value and Benefits of ASP.NET MVC&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/jonathangeorge/archive/2009/10/03/optimising-an-asp-net-mvc-web-site-part-1-introduction.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;Optimising
an ASP.NET MVC web site part 1 - Introduction&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/jonathangeorge/archive/2009/10/03/optimising-an-asp-net-mvc-web-site-part-2-database-and-nhibernate.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;Optimising
an ASP.NET MVC web site part 2 - Database and NHibernate&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/jonathangeorge/archive/2009/09/09/how-to-improve-your-yslow-score-under-iis7.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;How
to improve your YSlow score under IIS7&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/howardvanrooijen/archive/2009/08/25/cloaking-your-asp-net-mvc-web-application-on-iis-7.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;Cloaking
your ASP.NET MVC Web Application on IIS 7&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/jamesbroome/archive/2008/07/22/why-bdd-works-for-agile.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;Why
BDD works for Agile&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/jamesbroome/archive/2008/12/15/behaviour-driven-development-not-just-for-acceptance-testing.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;Behaviour
Driven Development - not just for Acceptance Testing&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/jamesbroome/archive/2009/04/24/why-i-like-n2-cms.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;Why
I like N2 CMS&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://consultingblogs.emc.com/howardvanrooijen/archive/2009/09/11/microsoft-open-source-and-codeplex-foundation.aspx"&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;Microsoft,
Open Source and Codeplex Foundation&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;I wanted to say a HUGE
thank-you to everyone in this community for all the passion, energy,
knowledge and help you provide on a daily basis.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;S#arp Architecture is a wonderful framework that truly is greater than the sum of its parts.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:arial,helvetica,sans-serif;"&gt;/Howard&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Thanks for sharing Howard and good luck with the venture!&amp;nbsp; (There are other S#arp projects in the wild &lt;a href="http://groups.google.com/group/sharp-architecture/browse_thread/thread/9df324a3dc36decb"&gt;discussed in the S#arp forums&lt;/a&gt;...go take a look!)&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=52474" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="S#arp Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Architecture/default.aspx" /></entry><entry><title>S#arp Architecture Contrib...it's alive!</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2009/10/05/s-arp-architecture-contrib-it-s-alive.aspx" /><id>/blogs/billy_mccafferty/archive/2009/10/05/s-arp-architecture-contrib-it-s-alive.aspx</id><published>2009-10-05T22:02:00Z</published><updated>2009-10-05T22:02:00Z</updated><content type="html">&lt;p&gt;As if developing ASP.NET MVC&amp;nbsp;applications&amp;nbsp;with &lt;a href="http://wiki.sharparchitecture.net/"&gt;S#arp Architecture&lt;/a&gt; couldn&amp;#39;t get any more flippin&amp;#39; exciting... &amp;nbsp;I&amp;#39;m pleased to announce &lt;a href="http://wiki.sharparchitecture.net/SharpArchContrib.ashx"&gt;S#arp Architecture Contrib&lt;/a&gt; which promises to take S#arp Architecture into new territory, such as providing better support for WinForms, making available a number of optional add-ons (such as &lt;a href="http://www.postsharp.org/"&gt;PostSharp&lt;/a&gt; enhancements), and encouraging further opinionation (is that a word?) of S#arp Architecture with various alternatives such as specific security providers and view engines, such as CRUD generation for&amp;nbsp;&lt;a href="http://dev.dejardin.org/"&gt;Spark&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://wiki.sharparchitecture.net/"&gt;&lt;img src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Components.UserFiles/00.00.00.21.06/logo_5F00_web_5F00_gif.gif" border="0" style="border:0;float:right;" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It&amp;#39;s always been very important to me and the contributors of S#arp Architecture that the core framework remain tight and clean. &amp;nbsp;The introduction of S#arp Architecture Contrib now opens the door to many more options for its use without compromising that vision. &amp;nbsp;(To emphasize this, even the WCF modules in #Arch will be moved to the Contrib project to make the core of #Arch that much leaner.)&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tomcabanski.spaces.live.com/"&gt;Tom Cabanski&lt;/a&gt; is leading this Contrib effort and has already put a great deal of effort into making both the code and related documentation available for your review. &amp;nbsp;The source may be found at &lt;a href="http://github.com/codai/Sharp-Architecture-Contrib"&gt;http://github.com/codai/Sharp-Architecture-Contrib&lt;/a&gt; with supporting documentation to be found at &lt;a href="http://wiki.sharparchitecture.net/SharpArchContrib.ashx"&gt;http://wiki.sharparchitecture.net/SharpArchContrib.ashx&lt;/a&gt;. &amp;nbsp;Your feedback on the S#arp Architecture &lt;a href="http://groups.google.com/group/sharp-architecture"&gt;forums&lt;/a&gt; on its current direction is most welcome!&lt;/p&gt;
&lt;p&gt;On a related note, I&amp;#39;ll be speaking at the Nov. 4 &lt;a href="http://www.virtualaltnet.com/"&gt;Virtual ALT.NET&lt;/a&gt; meeting to discuss the first quarterly release of S#arp Architecture (coming soon!) along with a couple of specific topics related to developing S#arp projects. &amp;nbsp;Appropriately enough, Tom Cabanski will be joining me to give an introductory overview of what&amp;#39;s in the Contrib project and where it&amp;#39;s headed.&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;p&gt;Billy McCafferty&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=52188" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="S#arp Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Architecture/default.aspx" /></entry><entry><title>Drawing the Line Between MVC Controllers and Application Services</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2009/09/16/drawing-the-line-between-mvc-controller-and-ddd-application-service-layers.aspx" /><id>/blogs/billy_mccafferty/archive/2009/09/16/drawing-the-line-between-mvc-controller-and-ddd-application-service-layers.aspx</id><published>2009-09-16T17:29:00Z</published><updated>2009-09-16T17:29:00Z</updated><content type="html">&lt;p&gt;Frequently, when we adapt new technologies or techniques, the new fix&amp;nbsp;ends up introducing&amp;nbsp;new &amp;quot;yucky&amp;quot; stuff&amp;nbsp;or simply pushes the&amp;nbsp;previous yucky stuff&amp;nbsp;to another area.&amp;nbsp; Since &amp;quot;yucky&amp;quot; just &lt;i&gt;might&lt;/i&gt; be a bit subjective here, I consider yucky stuff to include&amp;nbsp;any bit of code that has no clear home or which is overly complicated for the sake of separtion of concerns.&amp;nbsp; If you encounter code such as this, it doesn&amp;#39;t necessarily mean that you have a weak understanding of the domain or task at hand, but it may be a sign that you have a grey area between application layers which needs to be better defined.&lt;/p&gt;
&lt;p&gt;These grey areas are frequently found between where the presentation layer ends and the controller and/or business logic begins.&amp;nbsp; For example, when we moved from Active Server Pages to ASP.NET, we liberated our views of business logic only to find our newly embraced&amp;nbsp;code-behinds to be a bear to maintain as they intertwined abstracted presentation concerns with logic.&amp;nbsp; Adding a pattern such as &lt;a href="http://www.object-arts.com/papers/TwistingTheTriad.PDF"&gt;M&lt;/a&gt; &lt;a href="http://www.codeproject.com/KB/architecture/ModelViewPresenter.aspx"&gt;V&lt;/a&gt; &lt;a href="http://www.martinfowler.com/eaaDev/SupervisingPresenter.html"&gt;P&lt;/a&gt; certainly made it all testable but then moved much of the &amp;quot;yucky&amp;quot; stuff to either the presenter class or&amp;nbsp;added new yucky stuff in the form of complicated event communications and/or fudgy controller logic between the code-behind and presenter classes.&amp;nbsp; (If you&amp;#39;ve used MVP in a major project, you&amp;#39;ll likely know what I&amp;#39;m talking about.)&amp;nbsp; So now&amp;nbsp;with the dawn of ASP.NET MVC with a &lt;a href="http://www.infoq.com/minibooks/domain-driven-design-quickly"&gt;DDD&lt;/a&gt; approach (nicely encapsulated within &lt;a href="http://github.com/codai/Sharp-Architecture"&gt;S#arp Architecture&lt;/a&gt;, of course), our concerns are over, right?&amp;nbsp; Well, not exactly...we&amp;nbsp;now have a new yucky bit to deal with:&amp;nbsp; the grey area between the controllers and &lt;a href="http://martinfowler.com/eaaCatalog/serviceLayer.html"&gt;application&lt;/a&gt; &lt;a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/4342"&gt;services&lt;/a&gt; (not to be confused with &lt;a href="http://devlicio.us/blogs/casey/archive/2009/02/17/ddd-services.aspx"&gt;domain&lt;/a&gt; &lt;a href="http://stochastyk.blogspot.com/2008/05/domain-services-in-domain-driven-design.html"&gt;services&lt;/a&gt;) .&amp;nbsp; This is exactly the grey area that I&amp;#39;ve struggled with since adopting ASP.NET MVC with an eye for DDD.&amp;nbsp; Where exactly do we draw the line between what goes into controllers and what goes into application services?&lt;/p&gt;
&lt;p&gt;Joe Wilson recently posted a &lt;a href="http://volaresystems.com/Blog/post/2009/09/14/Review-of-Sharp-Architecture.aspx"&gt;review on S#arp Architecture&lt;/a&gt; and had a useful critique of the framework which IMO provides a very clear explanation of where the line should be drawn between the MVC controller and DDD application service layers:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;i&gt;I&amp;#39;ve borrowed a metaphor from the SQL Server installer that talk about &amp;quot;surface area&amp;quot;. I think of the MyProject.ApplicationServices layer as a wrapper around &lt;b&gt;all&lt;/b&gt; the app logic. The methods in this layer are the surface area of my application, and they encapsulates all the implementation code my domain is dealing with. Calls into this layer can kick off multi-step processes, handle workflow, and usually have long, explicit method names. But this is the &lt;b&gt;only&lt;/b&gt; part of my domain&amp;#39;s surface area that is exposed externally.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;To put it another way, if you&amp;#39;re putting any logic into your controller class that you would have to copy and paste, almost verbatim,&amp;nbsp;into, e.g., a WCF service class to replicate the same sort of behavior, but for a different client&amp;nbsp;interface (e.g., WCF vs. MVC), then it&amp;#39;s likely a sign that your controller is&amp;nbsp;doing too much and&amp;nbsp;that the&amp;nbsp;code may instead belong in an application service.&amp;nbsp; In a different light, if you&amp;#39;re putting anything into the application service layer which would preclude a different client type (e.g., WCF vs. MVC) to leverage it, then you may be introducing controller concerns into the application services.&amp;nbsp; Following these rules of thumb, your controllers remain more&amp;nbsp;appropriately&amp;nbsp;paired&amp;nbsp;with the web tier of your application while your application services provide the true gateway to the rest of the application.&amp;nbsp; Keeping a&amp;nbsp;close watch on the application service layer&amp;#39;s&amp;nbsp;&amp;quot;surface area&amp;quot; (or more appropriately stated, &lt;a href="http://devlicio.us/blogs/casey/archive/2009/02/11/ddd-bounded-contexts.aspx"&gt;bounded context&lt;/a&gt;), and making sure it doesn&amp;#39;t leak into the controller layer or vice versa, is a good approach for drawing a firm line between these two layers and keeping the yucky stuff at bay.&lt;/p&gt;
&lt;p&gt;Billy McCafferty&lt;br /&gt;http://www.itsamuraischool.com&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=51432" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="MVC.NET" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/MVC.NET/default.aspx" /><category term="S#arp Architecture" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Architecture/default.aspx" /><category term="DDD" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/DDD/default.aspx" /></entry></feed>