<?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-11-09T15:26:00Z</updated><entry><title>S#arp Lite: The Basics</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2011/11/11/s-arp-lite-the-basicss.aspx" /><id>/blogs/billy_mccafferty/archive/2011/11/11/s-arp-lite-the-basicss.aspx</id><published>2011-11-12T00:01:00Z</published><updated>2011-11-12T00:01:00Z</updated><content type="html">&lt;h2&gt;&lt;strong&gt;What is S#arp Lite?&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="font-weight:normal;"&gt;&lt;i&gt;S#arp Lite is an architectural framework for the development of well-designed, custom-built, ASP.NET MVC applications.&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-weight:normal;"&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/SharpLite_5F00_Logo.png" style="border:0;float:right;" width="350" alt="" /&gt;ASP.NET MVC 3 is a terrific platform for delivering web-based applications. &amp;nbsp;But, similar to ASP.NET, it does not provide specific guidelines for how to best use it in different project contexts. &amp;nbsp;That&amp;#39;s certainly the point; it exists to provide a flexible platform which may be used in a variety of situations without being biased towards one architecture or another, beyond the fundamentals of Model-View-Controller. &amp;nbsp;The benefit of this is that you can structure MVC projects almost anyway you&amp;#39;d like; the drawback is that it&amp;#39;s possible to have almost no consistency among your organization&amp;#39;s projects, even if they&amp;#39;re all using ASP.NET MVC.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-weight:normal;"&gt;That&amp;#39;s where &lt;a href="https://github.com/codai/Sharp-Lite"&gt;S#arp Lite&lt;/a&gt; comes in to play. &amp;nbsp;S#arp Lite comes packaged with three primary assets to provide a turnkey solution for developing well-designed, MVC applications:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A project template to facilitate the creation of new S#arp Lite projects, pre-configured to communicate with your database using NHibernate;&lt;/li&gt;
&lt;li&gt;A set of reusable class libraries which encapsulates infrastructural concerns (including a base repository); and&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Architectural guidance on how to build out a S#arp Lite project.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Currently, the architectural guidance is demonstrated via the sample project which has been included in the S#arp Lite release package. &amp;nbsp;Architectural guidelines are also enforced by the direction of dependencies among the project layers. &amp;nbsp;(This will be discussed in more detail below.) &amp;nbsp;If you have technical questions, please post to the Google Group at&amp;nbsp;&lt;a href="http://groups.google.com/group/sharp-lite"&gt;http://groups.google.com/group/sharp-lite&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The overall objective is to allow your development team to more easily develop ASP.NET MVC applications which adhere to well founded principles, such as &lt;a href="http://www.infoq.com/minibooks/domain-driven-design-quickly"&gt;domain-driven design&lt;/a&gt; and &lt;a href="http://www.agiledata.org/essays/tdd.html"&gt;test-driven development&lt;/a&gt;; without being bogged down with infrastructural setup and without sacrificing long-term maintainability and scalability of the solution.&lt;/p&gt;
&lt;p&gt;As a quick side, the base repository class which S#arp Lite exposes is purposefully&amp;nbsp;&lt;i&gt;very &lt;/i&gt;simplistic. &amp;nbsp;The base repository only includes the following methods:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Get(id): &amp;nbsp;returns an entity from the database having the Id provided,&lt;/li&gt;
&lt;li&gt;GetAll(): &amp;nbsp;returns an IQueryable&amp;lt;&amp;gt; which may be further filtered/transformed via LINQ,&lt;/li&gt;
&lt;li&gt;SaveOrUpdate(entity): &amp;nbsp;persists an entity to the database, and&lt;/li&gt;
&lt;li&gt;Delete(entity): &amp;nbsp;deletes an entity from the database.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keeping the base repository very light reduces bloat in the reusable libraries and places greater emphasis on the use of LINQ for retrieving results from GetAll(). &amp;nbsp;Besides, you can extend it with your own code to bloat it as much as you&amp;#39;d like. &amp;nbsp;We&amp;#39;ll discuss this in more detail a bit later.&lt;/p&gt;
&lt;h2&gt;Who is this intended for?&lt;/h2&gt;
&lt;p&gt;The motivation for S#arp Lite came from working with many teams (including my own) who had been developing projects with &lt;a href="https://github.com/sharparchitecture/Sharp-Architecture"&gt;S#arp Architecture&lt;/a&gt;. &amp;nbsp;To many, S#arp Architecture is simply too big of an architectural framework to easily get your head around. &amp;nbsp;When I used to discuss S#arp Architecture with teams who were considering using it, I would always suggest that their developers be very experienced and well versed with topics such as dependency inversion, low-level NHibernate, and domain-driven design.&lt;/p&gt;
&lt;p&gt;The reality of business is that it&amp;#39;s not likely that your team will be made up of all senior level developers who are all experts in these topics. &amp;nbsp;S#arp Lite is intended to epitomize the underlying values of S#arp Architecture, strive to be equally scalable to large projects, all while being tractable to a larger audience. &amp;nbsp;In other words, you should be able to have a realistically skill-balanced team and still be able to successfully deliver a S#arp Lite application.&lt;/p&gt;
&lt;p&gt;S#arp Lite is recommended for any mid-to-large sized ASP.NET MVC project. &amp;nbsp;If you have a small mom &amp;amp; pop store, you&amp;#39;d likely be better off using a less-tiered application setup. &amp;nbsp;S#arp Lite scales well to very large projects; we&amp;#39;re currently using it effectively on applications which integrate with a half dozen other systems...so it certainly holds up to meatier challenges.&lt;/p&gt;
&lt;h2&gt;What does a S#arp Lite project look like?&lt;/h2&gt;
&lt;p&gt;Creating a new S#arp Lite project is trivially simple:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and unzip the &lt;a href="https://github.com/codai/Sharp-Lite/downloads"&gt;S#arp Lite release package&lt;/a&gt;&amp;nbsp;from GitHub.&lt;/li&gt;
&lt;li&gt;Follow the instructions within the &lt;a href="https://github.com/codai/Sharp-Lite/blob/master/README.txt"&gt;README.txt&lt;/a&gt; to create your S#arp Lite project with &lt;a href="http://opensource.endjin.com/templify/"&gt;Templify&lt;/a&gt;&amp;nbsp;(a brilliant little tool).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After you&amp;#39;ve created a S#arp Lite project, you&amp;#39;ll find the following directory structure under the root folder:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;app&lt;/strong&gt;: &amp;nbsp;This folder holds the source of your project; i.e., the code that you&amp;#39;re getting paid to write.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;build&lt;/strong&gt;: &amp;nbsp;This initially empty folder is a placeholder for your build-related artifacts; e.g., a &amp;quot;publish&amp;quot; folder, NAnt or MSBuild artifacts, etc.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;docs&lt;/strong&gt;: &amp;nbsp;This initially empty folder contains all of the documents for your project. &amp;nbsp;Keeping them here keeps all of your docs checked in with the code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;lib&lt;/strong&gt;: &amp;nbsp;This folder contains all of the DLL dependencies for your project, such as log4net.dll, SharpLite.Domain.dll, System.Web.Mvc.dll, etc.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;logs&lt;/strong&gt;: &amp;nbsp;This initially empty folder is intended to hold any generated log files. &amp;nbsp;The generated project&amp;#39;s web.config used this folder for dumping out log4net logs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;tools&lt;/strong&gt;: &amp;nbsp;This initially empty folder is intended to hold any third party install files or other dependencies which the team may need to work on the project. &amp;nbsp;For example, this is where we store the latest install files of Telerik ASP.NET MVC and NUnit, used by the project. &amp;nbsp;Having all of your installable dependencies, checked in with the code, makes it much easier to get &amp;quot;the new guy&amp;quot; up and running quickly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The auto-generated folder structure is just a means to help keep your digital assets and solution files organized. &amp;nbsp;The more interesting stuff is in the /app folder which houses the source code of your project. &amp;nbsp;But before we delve into each of the project layers in the /app folder of a S#arp Lite project, let&amp;#39;s take a birds eye view of the overall architecture.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/SharpLiteArchitecture.png"&gt;&lt;img src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/SharpLiteArchitecture.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The diagram above reflects the layers of a S#arp Lite project, implemented as separate class libraries and an ASP.NET MVC Web Project. &amp;nbsp;Having the tiers in separate class libraries allows you to enforce the direction of dependency among them. &amp;nbsp;For example, because YourProject.Tasks depends on YourProject.Domain, YourProject.Domain cannot have any direct dependencies on a calss within YourProject.Tasks. &amp;nbsp;This singled-directional dependency helps to enforce how the architecture is to remain organized.&lt;/p&gt;
&lt;p&gt;While the diagram above describes the basic purpose of each layer, it&amp;#39;s most assistive to look at an example project to have a clearer understanding of the scope of responsibilities of each layer. &amp;nbsp;Accordingly, let&amp;#39;s examine the tiers of the MyStore example application which was included in the S#arp Lite release package.&lt;/p&gt;
&lt;h3&gt;Examining the Layers of MyStore Sample Application&lt;/h3&gt;
&lt;p&gt;The MyStore sample application, included in the release zip, demonstrates the use of S#arp Lite for a fairly typical CRUD (create/read/update/delete) application. &amp;nbsp;It includes managing data and relationships such as one:one, one:many, many:many, and parent/child. &amp;nbsp;Let&amp;#39;s take a closer look at the relational model.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/MyStoreRelationalModel.png"&gt;&lt;img border="0" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/MyStoreRelationalModel.png" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The diagram above represents the relational object model, as implemented within a SQL Server database. &amp;nbsp;It&amp;#39;s a very simple model with basic relationships, but it still brings up a lot of interesting discussion points when we go to translate this relational model into the object-oriented design of our application.&lt;/p&gt;
&lt;p&gt;For example, each customer entity contains address information (stored as StreetAddress and ZipCode in the Customers table); in the domain, we want the address information pulled out into a separate object called Address. &amp;nbsp;Having this information as a separate object more easily allows us to add behavior to the Address object while keeping those concerns separate from the Customer object, such as integration with a USPS address validation service. &amp;nbsp;(Arguably, such a service would be in a stand-alone service class, but you get the idea.)&lt;/p&gt;
&lt;p&gt;As another example, the many:many Products_ProductCategories table shouldn&amp;#39;t have a similarly named class in our object model; instead, we would expect Products to have a listing of ProductCategories and/or vice-versa.&lt;/p&gt;
&lt;p&gt;The sample application includes examples of how all of this and mappings of the classes themselves have been achieved almost entirely via &lt;a href="http://en.wikipedia.org/wiki/Convention_over_configuration"&gt;coding-by-convention&lt;/a&gt;. &amp;nbsp;Let&amp;#39;s now look at the sample application, layer-by-layer, and interesting points along the way.&lt;/p&gt;
&lt;hr /&gt;
&lt;h4&gt;MyStore.Domain&lt;/h4&gt;
&lt;p&gt;This layer of the application contains the heart of the application; it represents the core domain of our product. &amp;nbsp;All the other layers exist simply to support the user&amp;#39;s need to interact with the domain. &amp;nbsp;In a S#arp Lite project, the domain layer contains four types of objects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Domain Objects&lt;/li&gt;
&lt;li&gt;Query Objects&lt;/li&gt;
&lt;li&gt;Query Interfaces&lt;/li&gt;
&lt;li&gt;Custom Validators&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&amp;#39;s review each in turn.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;i&gt;Domain Objects&lt;/i&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Alright, so maybe this is a whole bunch of types of objects, but they all have the same purpose - they exist to implement the domain of our application. &amp;nbsp;They consist of entities, value objects, services (e.g., calculators), factories, aggregates...all organized into modules, usually expressed as separate namespaces. &amp;nbsp;(I highly recommend Eric Evans&amp;#39; &lt;a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215"&gt;Domain-Driven Design&lt;/a&gt;, Jimmy Nilsson&amp;#39;s &lt;a href="http://www.amazon.com/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202"&gt;Applying Domain Driven Design and Patterns&lt;/a&gt;, and &lt;a href="http://www.amazon.com/Software-Development-Principles-Patterns-Practices/dp/0135974445"&gt;Agile Software Development&lt;/a&gt; by Robert Martin to help guide you.) &amp;nbsp;There are two namespaces in the sample project: &amp;nbsp;the &amp;quot;ProductMgmt&amp;quot; namespace which contains everything related to product management, and the &amp;quot;root&amp;quot; namespace for, well, everything else. &amp;nbsp;Your project will likely have others.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s now take a look at the Customer class within the sample project as an example of an entity. &amp;nbsp;There are some important items to note:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Customer inherits from Entity (which is from SharpLite.Domain.dll). &amp;nbsp;The Entity base class A) signals that this class is a persisted object which has an associated table in the database, B) provides an Id property (no biggie there), and C) facilitates comparing two entities to each other. &amp;nbsp;If two entities are of the same type and have the same Id, then you know they&amp;#39;re the same object. &amp;nbsp;But what if you&amp;#39;re comparing two &amp;quot;transient&amp;quot; entities; i.e., two entities which have not yet been persisted to the database. &amp;nbsp;As another example, how would you go about comparing a transient entity to entities that have been persisted.&lt;br /&gt;&lt;br /&gt;For this we need to compare &amp;quot;domain signatures.&amp;quot; &amp;nbsp;A domain signature is the fingerprint of what makes the entity unique &lt;i&gt;from a business perspective&lt;/i&gt;. &amp;nbsp;In other words, which property(s) of an object would make it identifiable without having an Id property? &amp;nbsp;Looking at the Customer class, we see that there are two properties decorated with the attribute &amp;quot;DomainSignature.&amp;quot; &amp;nbsp;Furthermore, the class itself is decorated with the attribute &amp;quot;HasUniqueDomainSignature.&amp;quot; &amp;nbsp;This means that no two objects may exist having the same first and last name. &amp;nbsp;(This will not be appropriate in all scenarios; but should reflect the domain signature of the object in the context of the application.) &amp;nbsp;The described attributes are included in SharpLite.Domain.dll and support automatic validation of the class&amp;#39; domain signature. &amp;nbsp;So if you try to add a new customer with the same first and last name as an existing customer, a validation message will let you know this is not allowed.&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;[HasUniqueDomainSignature(...
public class Customer : Entity
{
    [DomainSignature]
    ...
    public virtual string FirstName { get; set; }&lt;/pre&gt;
&amp;nbsp;&lt;/li&gt;
&lt;li&gt;The Customer class pulls encapsulates the address information from the Customers table into a separate Address class. &amp;nbsp;Interestingly, NHibernate &amp;quot;loquacious&amp;quot; mapping (aka - code by convention) automatically maps the related table columns into this &amp;quot;component&amp;quot; object.&lt;/li&gt;
&lt;li&gt;Customer has an IList of Orders with a protected setter; the collection is initialized in the constructor. &amp;nbsp;This is done for two reasons: &amp;nbsp;1) by design, the collection will &lt;i&gt;never&lt;/i&gt;&amp;nbsp;be null (which avoids a lot of object reference exception avoidance), and 2) we don&amp;#39;t have to worry about NHibernate losing its pointer to the original collection loaded from the database. &amp;nbsp;This collection &amp;quot;pattern&amp;quot; is simply good practice for exposing/protecting the collection.&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;public class Customer : Entity
{
    public Customer() {
        Orders = new List();
    }

    public virtual IList Orders { get; protected set; }&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Validation is enforced with standard, .NET &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx"&gt;data annotations&lt;/a&gt;. &amp;nbsp;No need for NHibernate.Validator or other validation mechanism when it&amp;#39;s all available via the .NET library. &amp;nbsp;And whenever you run into limitations, you can simply create a custom validator.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;i style="font-weight:bold;"&gt;Query Objects&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Regularly, we need to filter information returned from the database. &amp;nbsp;For example, we may just want to return &amp;quot;active&amp;quot; customers vs. all the customers in the database. &amp;nbsp;For performance reasons, it&amp;#39;s obviously better to put as much filtering work on the shoulders of the database. &amp;nbsp;Before LINQ providers, it was difficult to find the appropriate balance between filtering on the domain side or filtering on the database side. &amp;nbsp;But with LINQ and IQueryable, filtering can be developed within the domain while it&amp;#39;s still executed on the database. &amp;nbsp; Brilliant! &amp;nbsp;To facilitate this, every repository (e.g., IRepository&amp;lt;Customer&amp;gt;) exposes the method GetAll() which returns IQueryable&amp;lt;&amp;gt;.&lt;/p&gt;
&lt;p&gt;The abso-friggin-spectacular side effect of this is that we can avoid having specialty &amp;quot;repository&amp;quot; methods which exist simply to hide away the details of the underlying data-access mechanism. &amp;nbsp;There are two kinds of query objects in a S#arp Lite project:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Specification Query Objects&lt;/strong&gt;: &amp;nbsp;Specification query objects take a list and filter the results down to a smaller list, based on some criteria. &amp;nbsp;In the sample project, the specification query class, MyStore.Domain.FindActiveCustomers, provides an extension method to IQueryable&amp;lt;&amp;gt; with any filter parameters passed in to the method. &amp;nbsp;Alternatively, the query object could be a POCO class, accepting an IQueryable&amp;lt;&amp;gt; and filtering, accordingly. &amp;nbsp;The benefit to setting up the specification query object as an extension, albeit, with additional indirection, is that multiple queries may be chained, all while taking advantage of IQuerable&amp;lt;&amp;gt;&amp;#39;s delayed querying (i.e., only one query will be sent to the database even if you chain multiple queries.&lt;br /&gt;
&lt;pre name="code" class="c#"&gt;public static class FindActiveCustomersExtension
{
    public static IQueryable FindActiveCustomers(this IQueryable customers) {
        return customers.FindActiveCustomers(MINIMUM_ORDERS_TO_BE_CONSIDERED_ACTIVE);
    }

    public static IQueryable FindActiveCustomers(this IQueryable customers, int minimumOrders) {
        return customers.Where(c =&amp;gt;
            c.Orders.Count &amp;gt;= minimumOrders);
    }

    private const int MINIMUM_ORDERS_TO_BE_CONSIDERED_ACTIVE = 3;
}&lt;/pre&gt;
To me, the real beauty in this is that the query object may live in the domain, and be tested as a first class citizen of the domain, without introducing any dependencies to the underlying data-access layer (whether that be NHibernate, Entity Framework, etc.).&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Report Query Objects&lt;/strong&gt;: &amp;nbsp;Report query objects take a list, filtering if necessary, transforming and returning the results as a DTO or list of DTOs. &amp;nbsp;Imagine that you have a summary dashboard in your application; e.g., a page which shows how many orders each customer has placed and what is each customer&amp;#39;s most frequently purchased product. &amp;nbsp;In this scenario, we&amp;#39;d ultimately like a list of DTOs containing each customer&amp;#39;s name, his/her order count, and his/her favorite product. &amp;nbsp;There are a few options to tackling this:
&lt;ul&gt;
&lt;li&gt;Create a DB stored procedure, binding the results to the DTOs list (and thus put processing logic onto the DB),&lt;/li&gt;
&lt;li&gt;Use NHibernate Criteria, HQL or named query to retrieve the results (and tightly couple your data-access code to NHibernate),&lt;/li&gt;
&lt;li&gt;Traverse the object model on the domain side to collate the information (can you say &lt;a href="http://use-the-index-luke.com/sql/join/nested-loops-join-n1-problem"&gt;n+1&lt;/a&gt;?), or&lt;/li&gt;
&lt;li&gt;Use clean and simple, data-access agnostic LINQ (and keep it in the domain).&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;2 points if you guess which one I&amp;#39;m leaning towards.  Let&amp;#39;s look MyStore.Domain.Queries.QueryForCustomerOrderSummariesExtension for an example:
&lt;pre name="code" class="c#"&gt;public static class QueryForCustomerOrderSummariesExtension
{
    public static IQueryable QueryForCustomerOrderSummaries(this IQueryable customers) {
        return from customer in customers
                select new CustomerOrderSummaryDto() {
                    FirstName = customer.FirstName,
                    LastName = customer.LastName,
                    OrderCount = customer.Orders.Count
                };
    }
}&lt;/pre&gt;
Again, the advantage of this is that it can live within the domain layer and act as a reusable reporting query without introducing dependencies to the underlying data-access layer.
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You have a lot of flexibility on how you use query objects and where they live. &amp;nbsp;For example, you could use an ad-hoc report query (i.e., a LINQ query not encapsulated by a class) which lives within a method in the tasks layer. &amp;nbsp;Although I&amp;#39;d advise against it, you could even use an ad-hoc query within a controller&amp;#39;s method. &amp;nbsp;So the provided samples are just that, samples of a particular approach. &amp;nbsp;What&amp;#39;s most important is to agree as a team how you&amp;#39;ll encapsulate and organize query objects. &amp;nbsp;In the sample project, queries are encapsulated as query objects and stored within a &amp;quot;Queries&amp;quot; folder - one folder per namespace.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;strong&gt;Query Interfaces&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the &lt;i&gt;very &lt;/i&gt;unlikely event that you need to leverage the data-access mechanism directly, instead of LINQing IQueryable, the domain layer may also contain any query interfaces which define a query to be implemented by the data-access layer. &amp;nbsp;(This is akin to creating custom repository interfaces in S#arp Architecture.)&lt;/p&gt;
&lt;p&gt;The disadvantages to this approach are three-fold:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It introduces a layer of indirection to the developer,&lt;/li&gt;
&lt;li&gt;It more tightly couples your code to the underlying data-access layer (since some of your application&amp;#39;s logic is now &lt;i&gt;in&lt;/i&gt; the data-access layer), and&lt;/li&gt;
&lt;li&gt;It becomes trickier to unit test the query since you need an in-memory or live database to test the implementation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But, I can foresee that there may be a situation where this is necessary if you have a very complicated query, need to leverage NHibernate detached queries, or simply can&amp;#39;t do what needs to be done via LINQ. &amp;nbsp;Accordingly, three steps would need to be taken to support the query:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Define the query interface in YourAppProject.Domain (e.g.,&amp;nbsp;MyStore.Domain.ProductMgmt.Queries.IQueryForProductOrderSummaries.cs),&lt;/li&gt;
&lt;li&gt;Implement the concrete query class in YourAppProject.NHibernateProvider (e.g.,&amp;nbsp;MyStore.NHibernateProvider.Queries.QueryForProductOrderSummaries.cs), and&lt;/li&gt;
&lt;li&gt;Register the implementation with the IoC to resolve requests to the interface (e.g.,&amp;nbsp;MyStore.Init.DependencyResolverInitializer).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Obviously, not as clean as using Specification and Report Query Objects, but available if absolutely necessary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span&gt;&lt;/span&gt;Custom Validators&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As discussed previously, S#arp Lite uses .NET&amp;#39;s data annotations for supporting validation. &amp;nbsp;(You could use something else, like &lt;a href="http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation.aspx"&gt;NHibernate.Validator&lt;/a&gt; if you prefer.) &amp;nbsp;The data annotations are added directly to entity classes, but could instead be added to form DTOs if you feel that entities shouldn&amp;#39;t act as form validation objects as well.&lt;/p&gt;
&lt;p&gt;Sometimes, data annotations aren&amp;#39;t powerful enough for the needs of your domain; e.g., if you want to compare two properties. &amp;nbsp;Accordingly, you can develop &lt;a href="http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx"&gt;custom validators&lt;/a&gt; and store them within a &amp;quot;Validators&amp;quot; folder. &amp;nbsp;If the custom validator is specific to a class, and never reused, then I&amp;#39;ll usually just add the custom validator class as a private subclass to the class which uses it. &amp;nbsp;In this way, the class-specific validator is neatly tucked away, only accessible by the class which needs it. &amp;nbsp;S#arp Lite uses a custom validator to determine if an object is a duplicate of an existing object, using its domain signature: &amp;nbsp;\SharpLiteSrc\app\SharpLite.Domain\Validators\HasUniqueDomainSignatureAttribute.cs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;
&lt;/strong&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;
&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;MyStore.Tasks&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;
&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This layer of the application contains the task-coordination logic, reacting to commands sent from, e.g., a controller in the presentation layer. &amp;nbsp;(This layer is also described as a &lt;a href="http://martinfowler.com/eaaCatalog/serviceLayer.html"&gt;Service Layer&lt;/a&gt; in Martin Fowler&amp;#39;s &lt;a href="http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420"&gt;PoEAA&lt;/a&gt;.) &amp;nbsp;For example, let&amp;#39;s assume that your application integrates with a number of other applications. &amp;nbsp;This layer would communicate with all of the other applications (preferably via interfaces), collating the information, and handing it off to the domain layer for performing domain logic on the data. &amp;nbsp;As a simpler example, if your domain layer contains some kind of FinancialCalculator class, the tasks layer would gather the information needed by the calculator, from repositories or other sources, and pass the data via a method to FinancialCalculator.&lt;/p&gt;
&lt;p&gt;As a secondary responsibility, the tasks layer returns data, as view-models, DTOs, or entities, to the presentation layer. &amp;nbsp;For example, the presentation layer may need to show a listing of customers along with Create/Edit/Delete buttons if the logged in user has sufficient rights to do so. &amp;nbsp;The tasks layer would get the listing of customers to show and would determine what security access the user has; it would then return a view model containing the customers listing along with a bool (or security object) describing if the user has rights to modify the data.&lt;/p&gt;
&lt;p&gt;It&amp;#39;s important to note the difference between the logic found within the tasks layer and that found within the domain layer. &amp;nbsp;The tasks layer should contain minimal logic to coordinate activities among services (e.g., repositories, web services, etc.) and the domain layer (e.g., calculator services). &amp;nbsp;Think of the tasks layer as an effective boss (does that exist?)...the boss helps to facilitate communications among the team and tells the team members what to do, but doesn&amp;#39;t do the job itself.&lt;/p&gt;
&lt;p&gt;The tasks layer contains two kinds of objects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Task Objects&lt;/li&gt;
&lt;li&gt;View Models&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;&lt;i&gt;Task Objects&lt;/i&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;These are the tasks themselves. &amp;nbsp;The most common kind of task is coordinating CUD logic (CRUD without the read). &amp;nbsp;It&amp;#39;s so common, in fact, that S#arp Lite projects includes a (completely customizable) BaseEntityCudTasks class to encapsulate this common need. &amp;nbsp;Looking at the sample project, you can see how&amp;nbsp;BaseEntityCudTasks&amp;nbsp;is extended and used; e.g., within MyStore.Tasks.ProductMgmt.ProductCudTasks.&lt;/p&gt;
&lt;p&gt;As the project grows, the task-layer responsibilities will inevitably grow as well. &amp;nbsp;For example, on a current project which integrates with multiple external applications, a task class pulls schedule information from Primavera 6, cost information from Prism, and local data from the database via a repository. &amp;nbsp;It then passes all of this information to a MasterReportGenerator class which resides in the domain. &amp;nbsp;Accordingly, although the task class is non-trivial, it&amp;#39;s simply pulling data from various sources, leaving it up to the domain to the heavy processing of the data.&lt;/p&gt;
&lt;p&gt;It&amp;#39;s important to note that a task object&amp;#39;s service dependencies (repositories, web services, query interfaces, etc.) should be injected via &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;This facilitates the ability to unit test the task objects with &lt;a href="http://martinfowler.com/articles/mocksArentStubs.html"&gt;stubbed/mocked&lt;/a&gt; services. &amp;nbsp;With MVC 3, setting up dependency injection is very simple and makes defining your task objects dependencies a breeze:&lt;/p&gt;
&lt;pre name="code" class="c#"&gt;public ProductCategoryCudTasks(IRepository&amp;lt;ProductCategory&amp;gt; productCategoryRepository) : base(productCategoryRepository) {
    _productCategoryRepository = productCategoryRepository;
}&lt;/pre&gt;
&lt;p&gt;Here we see that the&amp;nbsp;&lt;span&gt;ProductCategoryCudTasks class requires a&amp;nbsp;&lt;/span&gt;&lt;span&gt;IRepository&amp;lt;ProductCategory&amp;gt; injected into it, which will be provided at runtime by the IoC container or by you when unit testing.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;i&gt;View Models&lt;/i&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A view model class encapsulates information to be shown to the user. &amp;nbsp;It doesn&amp;#39;t say &lt;i&gt;how&lt;/i&gt;&amp;nbsp;the data should be displayed, only &lt;i&gt;what&lt;/i&gt;&amp;nbsp;data should be displayed. &amp;nbsp;Frequently, it&amp;#39;ll also include supporting information for the presentation layer to then decide &lt;i&gt;how&lt;/i&gt;&amp;nbsp;the information is displayed; e.g., permissions information.&lt;/p&gt;
&lt;p&gt;There&amp;#39;s a lot of debate about where view model classes should reside. &amp;nbsp;In my projects, I keep them in the tasks layer, with one ViewModels folder per namespace. &amp;nbsp;But arguably, view model classes could live in a separate class library; that&amp;#39;s for your team to decide at the beginning of a project.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;MyStore.Web&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There&amp;#39;s not much to say here. &amp;nbsp;A S#arp Lite project uses all out-of-the-box MVC 3 functionality for the presentation layer, defaulting to Razor view engines, which you may change if preferred. &amp;nbsp;The only S#arp Lite-isms (totally a word) in this layer are as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;MyStore.Web.Global.asax invokes &lt;code&gt;DependencyResolverInitializer.Initialize();&lt;/code&gt; to initialize the IoC container (discussed below),&lt;/li&gt;
&lt;li&gt;MyStore.Web.Global.asax uses&amp;nbsp;SharpModelBinder to act as the preferred form/model binder, and&lt;/li&gt;
&lt;li&gt;Web.config includes an HttpModule to leverage a &lt;a href="http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx"&gt;session-per-request, NHibernate HTTP module&lt;/a&gt;, found within the S#arp Lite source at \SharpLiteSrc\app\SharpLite.NHibernateProvider\Web\SessionPerRequestModule.cs.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;SharpModelBinder extends the basic form/model binding with capabilities to populate relationships. &amp;nbsp;For example, suppose you have a Product class with a many:many relationship to ProductCategory. &amp;nbsp;When editing the Product, the view could include a list of checkboxes for associating the product with one or more product categories. &amp;nbsp;SharpModelBinder looks for such associations in the form and populates the relationships when posted to the controller; i.e., the Product which gets posted to the controller will have its ProductCategories populated, containing one ProductCategory for each checkbox that was checked. &amp;nbsp;You can take a look at MyStore.Web/Areas/ProductMgmt/Views/Products/Edit.cshtml as an example.&lt;/p&gt;
&lt;p&gt;Like task objects, controllers also accept dependencies via injection; e.g.&amp;nbsp;MyStore.Web.Areas.ProductMgmt.Controllers.ProductsController.cs:&lt;/p&gt;
&lt;pre name="code" class="c#"&gt;public ProductsController(IRepository&amp;lt;Product&amp;gt; productRepository,
    ProductCudTasks productMgmtTasks, IQueryForProductOrderSummaries queryForProductOrderSummaries) {

    _productRepository = productRepository;
    _productMgmtTasks = productMgmtTasks;
    _queryForProductOrderSummaries = queryForProductOrderSummaries;
}&lt;/pre&gt;
&lt;p&gt;In the example above, the controller requires an instance of&amp;nbsp;&lt;span&gt;IRepository&amp;lt;Product&amp;gt;,&amp;nbsp;&lt;/span&gt;&lt;span&gt;ProductCudTasks, and&amp;nbsp;&lt;/span&gt;&lt;span&gt;IQueryForProductOrderSummaries passed to its constructor from the IoC container. &amp;nbsp;&lt;/span&gt;IQueryForProductOrderSummaries&amp;nbsp;is an example of using a query interface, defined in the domain, for providing data-access layer specific needs. &amp;nbsp;It&amp;#39;s a very exceptive case and has only been included for illustrive purposes. &amp;nbsp;You&amp;#39;d almost always be able to use specification and report query objects instead...or simply LINQ right off of&amp;nbsp;IRepository&amp;lt;Product&amp;gt;.GetAll().&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d like to learn more about dependency injection in ASP.NET MVC 3, check out &lt;a href="http://bradwilson.typepad.com/blog/2010/07/service-location-pt1-introduction.html"&gt;Brad Wilson&amp;#39;s series of posts&lt;/a&gt;&amp;nbsp;on the subject. &amp;nbsp;And for learning more about the basics of developing in the web layer, Steve Sanderson&amp;#39;s &lt;a href="http://www.amazon.com/Pro-ASP-NET-MVC-3-Framework/dp/1430234040/ref=sr_1_1?ie=UTF8&amp;amp;qid=1321046999&amp;amp;sr=8-1"&gt;Pro ASP.NET MVC 3 Framework&lt;/a&gt;&amp;nbsp;is a great read.&lt;/p&gt;
&lt;hr /&gt;
&lt;h4&gt;&lt;strong&gt;MyStore.Init&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;This nearly anemic layer has one responsibility: &amp;nbsp;perform generic, application initialization logic. &amp;nbsp;Specifically, the initialization code included with a S#arp Lite project initializes the IoC container (&lt;a href="http://structuremap.net/structuremap/"&gt;StructureMap&lt;/a&gt;) and invokes the initialization of the NHibernate session factory. &amp;nbsp;Arguably, this layer is so thin that its responsibilities could easily be subsumed by MyStore.Web. &amp;nbsp;The great advantage to pulling the initialization code out into a separate class library is that MyStore.Web requires far fewer dependencies. &amp;nbsp;Note that MyStore has no reference to NHibernate.dll nor to StructureMap.dll. &amp;nbsp;Accordingly, there is very little coupling&amp;nbsp;(i.e., none)&amp;nbsp;to these dependencies from the web layer...we like that. &amp;nbsp;Among other things, this prevents anyone from invoking an NHibernate-specific function from a controller. &amp;nbsp;This, in turn, keeps the controllers very decoupled from the underlying data-access mechanism as well.&lt;/p&gt;
&lt;hr /&gt;
&lt;h4&gt;MyStore.NHibernateProvider&lt;/h4&gt;
&lt;p&gt;The next stop on our tour of the layers of a S#arp Lite project is the NHibernate provider layer. &amp;nbsp;With S#arp Architecture, this layer would frequently get quite sizable with custom repositories and named queries. &amp;nbsp;With the alternative use of query objects and LINQ on IQueryable&amp;lt;&amp;gt;, this class library should remain very thin. &amp;nbsp;This class library contains three kinds of objects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;NHibernate Initializer,&lt;/li&gt;
&lt;li&gt;NHibernate Conventions,&lt;/li&gt;
&lt;li&gt;Mapping Overrides, and&lt;/li&gt;
&lt;li&gt;(very&amp;nbsp;occasionally) Query Implementations.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&amp;#39;s look at each in turn.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;i&gt;NHibernate Initializer&lt;/i&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;NHibernate 3.2.0 introduces a built-in fluent API for configuration and mapping classes, nicknamed&amp;nbsp;&lt;a href="http://nhforge.org/wikis/howtonh/a-fully-working-skeleton-for-sexy-loquacious-nh.aspx"&gt;NHibernate&amp;#39;s &amp;quot;Loquacious&amp;quot; API&lt;/a&gt;. &amp;nbsp;This is a direct&amp;nbsp;&lt;a href="http://lostechies.com/jamesgregory/2011/04/13/me-on-nhibernate-3-2/"&gt;affront&lt;/a&gt;&amp;nbsp;to &lt;a href="http://fluentnhibernate.org/"&gt;Fluent NHibernator&lt;/a&gt; which (as much as I have truly loved it...a sincere thank you to James Gregory) I feel is headed for obsolescence with these capabilities now being built right in to NHibernate. &amp;nbsp;NHibernate 3.2&amp;#39;s Loquacious API isn&amp;#39;t yet as powerful as Fluent NHibernate, but will get there soon as more of &lt;a href="http://code.google.com/p/codeconform/"&gt;ConfORM&lt;/a&gt; is ported over to Loquacious API. &amp;nbsp;On with the show...&lt;/p&gt;
&lt;p&gt;There is one NHibernate initialization class with a S#arp Lite project; e.g.,&amp;nbsp;MyStore.NHibernateProvider.NHibernateInitializer.cs. &amp;nbsp;This class sets the connection string (from web.config), sets the dialect, tells NHibernate where to find mapped classes, and invokes convention setup (discussed next). &amp;nbsp;Initializing NHibernate is very expensive and should only be performed once when the application starts. &amp;nbsp;Accordingly, take heed of this if you decide to switch out the IoC initialization code (in MyStore.Init) with another IoC container.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;i&gt;NHibernate Conventions&lt;/i&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The beauty of conventions is that we no longer need to include a class mapping (HBM or otherwise) for mapping classes to the database. &amp;nbsp;We simply define conventions, adhere to those conventions, and NHibernate knows which table/columns to go to for what. &amp;nbsp;S#arp Lite projects come prepackaged with the following, &lt;i&gt;customizable&lt;/i&gt; conventions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Table names are a plural form of the entity name. &amp;nbsp;E.g., if the entity is Customer, the table is Customers.&lt;/li&gt;
&lt;li&gt;Every entity has an Id property mapped to an &amp;quot;Id&amp;quot; identity column (which can easily be changed to HiLo, Guid, or otherwise).&lt;/li&gt;
&lt;li&gt;Primitive type column names are the same name as the property. &amp;nbsp;E.g., if the property is FirstName, the column name is FirstName.&lt;/li&gt;
&lt;li&gt;Foreign keys (associations) are the name of the property suffixed with &amp;quot;Fk.&amp;quot; &amp;nbsp;E.g., if the property is Order.WhoPlacedOrder, the column name (in the Orders table) is WhoPlacedOrderFk with a foreign key to the respective type&amp;#39;s table (e.g., Customers).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is typically just one convention-setup class in a S#arp Lite project; e.g., MyStore.NHibernateProvider.Conventions. &amp;nbsp;The only convention that isn&amp;#39;t supported &amp;quot;out of the box&amp;quot; is a many:many relationship, which we&amp;#39;ll discuss more below.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;i&gt;Mapping Overrides&lt;/i&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are times when conventions don&amp;#39;t hold up. &amp;nbsp;Examples include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Many-to-many relationships,&lt;/li&gt;
&lt;li&gt;Enum as a property type,&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Legacy databases which don&amp;#39;t stick to (your) conventions, and&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Anytime a convention is not followed for one reason or another.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;On the upside, this isn&amp;#39;t too many cases...but we need to be able to handle the exceptions. &amp;nbsp;Any exceptions to the conventions are defined as &amp;quot;mapping overrides.&amp;quot; &amp;nbsp;Examples of overrides may be found in MyStore.NHibernateProvider/Overrides. &amp;nbsp;To make things easy, if an override needs to be added, simply implement MyStore.NHibernateProvider.Overrides.IOverride and include your override code. &amp;nbsp;The MyStore.NHibernateProvider.Conventions class looks through the assembly for any classes which implements IOverride and applies them in turn. &amp;nbsp;As a rule, I create one override class for each respective entity which requires an override.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;i&gt;Query Implementations&lt;/i&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Lastly, the .NHibernateProvider layer contains any NHibernate-specific queries, which are implementations of respective query interfaces defined in the .Domain layer. &amp;nbsp;97.6831% of the time, this will not be necessary as querying via LINQ, off of IQueryable&amp;lt;&amp;gt;, is the preferred approach to querying. &amp;nbsp;But in the rare case that you need to implement a query using NHibernate Criteria, HQL, or otherwise, this is the layer to house it. &amp;nbsp;A sample has been included as&amp;nbsp;MyStore.NHibernateProvider.Queries.QueryForProductOrderSummaries.&lt;/p&gt;
&lt;hr /&gt;
&lt;h4&gt;MyStore.Tests&lt;/h4&gt;
&lt;p&gt;At the end of our tour of the layers of a S#arp Lite project is the .Tests layer. &amp;nbsp;This layer holds all of the unit tests for the project. &amp;nbsp;S#arp Lite projects are generated with two unit tests out of the box:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;MyStore.Tests.NHibernateProvider.MappingIntegrationTests.CanGenerateDatabaseSchema(): &amp;nbsp;This unit tests initializes NHibernate and generates SQL to reflect those mappings. &amp;nbsp;This is a great test to run to verify that a class is being mapped to the database as expected; i.e., you can look at the generated SQL (in the Text Output tab in NUnit) to verify how a class is being mapped. &amp;nbsp;As a side-benefit, you can copy the generated SQL and run it to make modifications to the database. &amp;nbsp;Finally, the unit tests saves the generated SQL into /app/MyStore.DB/Schema/UnitTestGeneratedSchema.sql for additional reference.&lt;/li&gt;
&lt;li&gt;MyStore.Tests.NHibernateProvider.MappingIntegrationTests.CanConfirmDatabaseMatchesMappings(): &amp;nbsp;This unit tests initializes NHibernate and verifies that every entity maps successfully to the database. &amp;nbsp;If you have a missing column, this test will let you know. &amp;nbsp;It doesn&amp;#39;t test everything, such as many-to-many relationships, but certainly 97.6831% of everything else.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For an introduction to test-driven development, read Kent Beck&amp;#39;s&amp;nbsp;&lt;a href="http://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530"&gt;Test Driven Development: By Example&lt;/a&gt;. &amp;nbsp;Going a step further, go with Gerard Meszaros&amp;#39; &lt;a href="http://www.amazon.com/xUnit-Test-Patterns-Refactoring-Code/dp/0131495054/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1321055197&amp;amp;sr=1-1"&gt;xUnit Test Patterns&lt;/a&gt; and Michael Feathers&amp;#39; &lt;a href="http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1321055177&amp;amp;sr=1-1"&gt;Working Effectively with Legacy Code&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In S#arp Architecture, SQLLite was used to provide an in-memory database for testing custom repository methods. &amp;nbsp;Since custom repositories have been mostly relegated to&amp;nbsp;obsolescence, keeping SQLLite testing built-in to S#arp Lite would have been overkill and has been removed to keep things simpler. &amp;nbsp;(Besides, you can always look at the S#arp Architecture code for that functionality if needed.)&lt;/p&gt;
&lt;h2&gt;What&amp;#39;s in the S#arp Lite Libraries?&lt;/h2&gt;
&lt;p&gt;Most of what&amp;#39;s relevant in the S#arp Lite class libraries has already been discussed while going through the sample project, but let&amp;#39;s take a moment to see what all is in the reusalbe, S#arp Lite class libraries.&lt;/p&gt;
&lt;h4&gt;SharpLite.Domain&lt;/h4&gt;
&lt;p&gt;This class library provides support for the domain layer of your S#arp Lite project.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ComparableObject.cs: &amp;nbsp;Provides robust, hash code generator for any object which inherits from it. &amp;nbsp;(It&amp;#39;s much trickier to implement than you think. ;)&lt;/li&gt;
&lt;li&gt;DomainSignatureAttribute.cs: &amp;nbsp;An attribute used to decorate properties which make up a class&amp;#39; domain signature.&lt;/li&gt;
&lt;li&gt;EntityWithTypedId (defined in Entity.cs): &amp;nbsp;Provides a (non-mandatory)&amp;nbsp;generic&amp;nbsp;base class for your entities, including an Id property and the inclusion of domain signature properties when comparing like objects. &amp;nbsp;It takes a single, generic parameter declaring the type of the Id property; e.g., int or Guid.&lt;/li&gt;
&lt;li&gt;Entity.cs: &amp;nbsp;Provides an&amp;nbsp;EntityWithTypedId base class with an Id assumed to be of type int.&lt;/li&gt;
&lt;li&gt;IEntityWithTypedId.cs: &amp;nbsp;Provides an interface which may be used to implement your own entity base class.&lt;/li&gt;
&lt;li&gt;/DataInterfaces/IDbContext.cs: &amp;nbsp;Exposes an interface for controlling transactions.&lt;/li&gt;
&lt;li&gt;/DataInterfaces/IEntityDuplicateChecker.cs: &amp;nbsp;Exposes an interface for checking if an entity is a duplicate of one already in the database.&lt;/li&gt;
&lt;li&gt;/DataInterfaces/IRepository.cs: &amp;nbsp;Exposes a very basic repository including Get, GetAll (returning IQueryable), SaveorUpdate, and Delete.&lt;/li&gt;
&lt;li&gt;/Validators/HasUniqueDomainSignatureAttribute.cs: &amp;nbsp;An attribute which may be used to decorate a class to ensure that duplicates do not exist in the database having the same domain signature.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;&lt;strong&gt;SharpLite.EntityFrameworkProvider&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;The idea of this library is to provide a pluggable replacement for the NHibernateProvider (discussed next), if the team so chooses. &amp;nbsp;This library has not been fully developed yet. &amp;nbsp;Let me know if you&amp;#39;re interested in contributing with this effort.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;SharpLite.NHibernateProvider&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;This infrastructural class library provides everything necessary for S#arp Lite projects to communicate with the database via NHibernate.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DbContext.cs: &amp;nbsp;Implements IDbContext for transaction management.&lt;/li&gt;
&lt;li&gt;EntityDuplicateChecker.cs: &amp;nbsp;Implements&amp;nbsp;IEntityDuplicateChecker for checking for entity duplicates.&lt;/li&gt;
&lt;li&gt;LazySessionContext.cs: &amp;nbsp;Supports NHibernate open-session-in-view; &lt;a href="http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx"&gt;originally written by&amp;nbsp;Jose Romaniello&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Repository.cs: &amp;nbsp;Provides a minimalist base repository...feel free to extend as needed.&lt;/li&gt;
&lt;li&gt;/Web/SessionPerRequestModule.cs: &amp;nbsp;Provides the HTTP module in support of NHibernate open-session-in-view.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;SharpLite.Web&lt;/h4&gt;
&lt;p&gt;This class library provides MVC-specific needs for S#arp Lite projects...which consists entirely of SharpModelBinder.cs. &amp;nbsp;This may be viewed as completely optional for your use in S#arp Lite projects.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;/Mvc/ModelBinder/EntityCollectionValueBinder.cs: &amp;nbsp;Used by SharpModelBinder for translating a collection of inputs from the form into a collection of entities bound to the containing object.&lt;/li&gt;
&lt;li&gt;/Mvc/ModelBinder/EntityRetriever.cs: &amp;nbsp;Used by SharpModelBinder related classes for retrieving an entity from the database without knowing which repository to use &lt;i&gt;a priori&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;/Mvc/ModelBinder/EntityValueBinder.cs: &amp;nbsp;Used by SharpModelBinder for translating a drop-down selection into an entity association bound to the containing object.&lt;/li&gt;
&lt;li&gt;/Mvc/ModelBinder/SharpModelBinder.cs: &amp;nbsp;Extends the ASP.NET MVC model binder with additional capabilities for populating associations with entities from the database.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Well, that&amp;#39;s it in a nutshell for now. &amp;nbsp;I sincerely hope that S#arp Lite will prove helpful to you and your team in developing well-designed, maintainable ASP.NET MVC applications that scale well as the project evolves. &amp;nbsp;This architectural framework reflects lessons learned from years of &lt;span style="text-decoration:line-through;"&gt;experience&lt;/span&gt; blood, sweat, &amp;amp; tears and countless ideas shamelessly stolen from those much smarter than I.&lt;/p&gt;
&lt;p&gt;Enjoy!&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=68379" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="S#arp Lite" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Lite/default.aspx" /></entry><entry><title>Introducing S#arp Lite ...S#arp Architecture's pompous little sister</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2011/10/26/s-arp-lite-like-beer-but-better.aspx" /><id>/blogs/billy_mccafferty/archive/2011/10/26/s-arp-lite-like-beer-but-better.aspx</id><published>2011-10-26T22:59:00Z</published><updated>2011-10-26T22:59:00Z</updated><content type="html">&lt;p&gt;&lt;i&gt;[This particular post is intended for people who are at least a little familiar with S#arp Architecture and explains the motivations for S#arp Lite. &amp;nbsp;For a comprehensive 101 on S#arp Lite, &lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2011/11/11/s-arp-lite-the-basicss.aspx"&gt;read the next post&lt;/a&gt;. &amp;nbsp;To get right to the code, visit the &lt;a href="https://github.com/codai/Sharp-Lite"&gt;S#arp Lite GitHub&lt;/a&gt; repository, download &lt;a href="https://github.com/downloads/codai/Sharp-Lite/SharpLite_0.42.zip"&gt;SharpLite_0.42.zip&lt;/a&gt; and follow &lt;a href="https://github.com/codai/Sharp-Lite/blob/master/README.txt"&gt;README.txt&lt;/a&gt;.]&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;In April of 2008, I &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;announced&lt;/a&gt; a project I had been working on called S#arp Architecture. &amp;nbsp;This framework, for developing scalable, ASP.NET MVC applications with NHibernate, strove to adhere to the following key principles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Facilitate Domain Driven Design in project development&lt;/li&gt;
&lt;li&gt;Enforce Loose Coupling in project structure&lt;/li&gt;
&lt;li&gt;Preconfigure infrastructure (to take care of all the tedious plumbing)&lt;/li&gt;
&lt;li&gt;Support open-ended presentation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&amp;#39;ve been pleased to have seen S#arp Architecture downloaded thousands of times and used on many many real world projects. &amp;nbsp;(I can count at least a dozen cradle-to-grave S#arp projects under my own belt.) &amp;nbsp;It has also had some terrific leaders and contributors along the way, including Alec Whittington,&amp;nbsp;Seif Attar,&amp;nbsp;Lee Carter, Jon George,&amp;nbsp;Simone Busoli, Luis Abreu, James Gregory, Martin Hornagold, Jay Oliver, Frank Laub, Geoffrey Smith, Dan Smith, Kyle Baley, Chris Richards, and Howard&amp;nbsp;van Rooijen.&lt;/p&gt;
&lt;p&gt;Over the years, I&amp;#39;ve reflected upon what could be done to make S#arp easier to understand, more tenable for a larger audience, simpler to develop with...all the while, keeping it scalable for larger projects. &amp;nbsp;S#arp Lite reflects this effort...&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/SharpLite_5F00_Logo.png"&gt;&lt;img style="border:0;float:right;" src="http://devlicio.us/resized-image.ashx/__size/300x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/SharpLite_5F00_Logo.png" border="0" alt="" /&gt;&lt;/a&gt;S#arp Architecture with a Lapband&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To create S#arp Lite, I started with S#arp Architecture, grabbed a machete, and went to town to whittle the code down to the absolute essentials. &amp;nbsp;After the dust settled, here&amp;#39;s what was left of the S#arp libraries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ASP.NET MVC 3 with Razor as the underlying framework and initial presentation layer (can be swapped out for ASPX);&lt;/li&gt;
&lt;li&gt;A base domain object for providing &amp;quot;domain signature,&amp;quot; comparison capabilities;&lt;/li&gt;
&lt;li&gt;A minimal, base repository including Get, GetAll (returning IQueryable), SaveOrUpdate, and Delete;&lt;/li&gt;
&lt;li&gt;Inclusion of NHibernate ORM suppor for convention-based, data mapping; and&lt;/li&gt;
&lt;li&gt;An MVC model binder for binding entity associations to an object from a form.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That&amp;#39;s about it. &amp;nbsp;What about Fluent NHibernate you ask? &amp;nbsp;Gone. &amp;nbsp;NHibernate Validator? &amp;nbsp;Gone. &amp;nbsp;MvcContrib and SQLLite? &amp;nbsp;Both gone. &amp;nbsp;AutoMapper, Newtonsoft.Json, WCF, Caliburn.Micro, MEF, PostSharp, RavenDB, Specifications? &amp;nbsp;All gone. &amp;nbsp;Although all of those features are handy, I felt that all the extras began to stand in the way of understanding the core principles of S#arp and should be included at the project level (not in the reusable S#arp Lite libraries).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The&amp;nbsp;Disappearing&amp;nbsp;Repository&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;One of the biggest pain points that my S#arp Architecture projects (and other teams&amp;#39; projects) have experienced over the years is the explosion of repository interfaces, repositories and &amp;quot;pass through&amp;quot; service classes which do nothing but re-route a request from a controller to a repository method. &amp;nbsp;Because the queries were NHibernate-dependent, the repositories wrapped the NHibernate-gory details in concrete repositories, only exposing their querying capabilities via interfaces. &amp;nbsp;Accordingly, you&amp;#39;d end up with hundreds of repository interfaces on large projects, all in the name of hiding NHibernate from the other layers. &amp;nbsp;Although previously necessary, the game has changed.&lt;/p&gt;
&lt;p&gt;With the mature, LINQ provider that NHibernate provides, there is no longer a need to wrap queries within repositories. &amp;nbsp;The LINQ queries, run against the repository&amp;#39;s GetAll() method, can now be added directly to application-service/task-layer classes or as stand-alone query objects in the domain layer. &amp;nbsp;They don&amp;#39;t necessitate a reference to NHibernate and so your layers still remain cleanly separated while being able to do away with all those annoying repository classes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Adherence to the Original Architecture&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;One of the primary goals of this effort was to simplify S#arp while remaining in strict adherence with the original architecture. &amp;nbsp;Let&amp;#39;s take a step back to see how the architecture looks on a S#arp Lite project.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/SharpLiteArchitecture.png"&gt;&lt;img src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/SharpLiteArchitecture.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is very similar to the overall architecture of S#arp Architecture. &amp;nbsp;The biggest changes in S#arp Lite, with respect to the architectural approach, are as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Controllers have been moved into the .Web project to better reflect their role as part of the presentation layer. &amp;nbsp;(I believe this may have been done in the most recent S#arp as well.)&lt;/li&gt;
&lt;li&gt;An .Init project has been introduced to remove extraneous dependencies from .Web. &amp;nbsp;(Although .Init is very small in size, it helps to eliminate &lt;i&gt;many&lt;/i&gt;&amp;nbsp;initialization related dependencies from .Web.)&lt;/li&gt;
&lt;li&gt;Since there are no more custom repositories, the data-access layer, .NHibernateProvider is very thin and only has the bare essentials for initializing NHibernate. &amp;nbsp;In fact, its role is so reduced, that it could be swapped out with an .EntityFrameworkProvider (more on this below).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you&amp;#39;ve used S#arp Architecture before, you&amp;#39;ll quickly notice that the overall architecture is almost identical to before...this is very intentional. &amp;nbsp;The architecture has and always will be a pivotal aspect of keeping the layers cleanly separated. &amp;nbsp;The original, architectural layout of S#arp Architecture worked well and has been preserved, for the most part, in S#arp Lite. &amp;nbsp;(S#arp Lite has just been severely trimmed down to the bare essentials.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Who is S#arp Lite intended for?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;S#arp Lite should be usable to a much wider audience. &amp;nbsp;In fact, because of the switch from custom repositories to using LINQ against IQueryable, you hardly even need to know NHibernate. &amp;nbsp;I hate to imply a one-size-fits-all architecture, but S#arp Lite can be used effectively on small projects (mom &amp;amp; pop stores) up to very large &amp;quot;enterprise&amp;quot; applications (e.g., an application which integrates with half a dozen other systems).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Is S#arp Lite backward compatible with S#arp Architecture?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Hypothetically yes, but it would likely be cost and time prohibitive to migrate a S#arp Architecture project to S#arp Lite. &amp;nbsp;With that said, on our existing S#arp Architecture projects, we&amp;#39;ve added an IQueryable method to our base repository to help with getting rid of new repositories.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Is S#arp Lite going to change very much over the next year?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Absolutely not. &amp;nbsp;It is going to remain almost exactly as is; with only upgrades to internal components when necessary (NHibernate, ASP.NET MVC, etc.). &amp;nbsp;It is very much intended for S#arp Lite to hardly grow at all for the foreseeable future to avoid any bloat. &amp;nbsp;Sure, contrib projects are welcome, but S#arp Lite will not be growing in size. &amp;nbsp;The only anticipated growth of the project is the inclusion of CRUD scaffolding.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What about S#arp Architecture?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;S#arp Architecture is still alive and well under Alec Whittington&amp;#39;s guiding light; it&amp;#39;s primary advantage now and forever more will be its wide array of options and solutions for various scenarios, such as WCF, SQLLite unit testing, PostSharp, etc. &amp;nbsp;These options simply aren&amp;#39;t built into S#arp Lite and won&amp;#39;t be...if you use S#arp Lite, it is expected that you&amp;#39;ll extend it as necessary to meet the specific needs of your project.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Didn&amp;#39;t you say something about .EntityFrameworkProvider?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Because S#arp Lite greatly de-emphasizes the need for custom repositories, NHibernate almost becomes a minor infrastructural concern, something akin to log4net. &amp;nbsp;Well, perhaps not that minor of a role, but certainly closer than before. &amp;nbsp;Because of this, S#arp Lite projects are very lightly coupled to NHibernate....so much so that I feel the SharpLite.NHibernateProvider layer could be (easily?) interchanged with a SharpLite.EntityFrameworkProvider using Entity Framework 4.1. &amp;nbsp;I&amp;#39;ve put the beginnings of such a layer into the S#arp Lite solution; unfortunately, I&amp;#39;m not versed enough with Entity Framework to know the best way to develop this layer, nor how to properly use it to effectively replace MyStore.NHibernateProvider in the MyStore sample project. &amp;nbsp;If anyone is up for an interesting challenge, I would be interested to see if SharpLite.EntityFrameworkProvider could be built out and used as the data-access infrastructure for the MyStore sample project.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;I&amp;#39;m bored with all this chatter...what next?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Alright, let&amp;#39;s get to the fun stuff. &amp;nbsp;For starters, the S#arp Lite source (and pretty good sample app) is available at&amp;nbsp;&lt;a href="https://github.com/codai/Sharp-Lite"&gt;https://github.com/codai/Sharp-Lite&lt;/a&gt;. &amp;nbsp;Let&amp;#39;s create a S#arp Lite project so you can see what it&amp;#39;s all about...&lt;/p&gt;
&lt;hr /&gt;
&lt;h3&gt;&lt;strong&gt;Check out the Sample Project &amp;amp; Create your first S#arp Lite project&lt;/strong&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Download&amp;nbsp;SharpLite_0.42.zip from the downloads tab of&amp;nbsp;&lt;a href="https://github.com/codai/Sharp-Lite"&gt;https://github.com/codai/Sharp-Lite&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Follow the instructions in README.txt&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Subsequent posts will go into more detail about S#arp Lite (for a 101 overview) and will begin providing a cookbook for developing your own S#arp Lite projects. &amp;nbsp;(If you&amp;#39;re at all interested in helping with the addition of some ASP.NET MVC scaffolding with &lt;a href="http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/"&gt;MvcScaffolding&lt;/a&gt;, you&amp;#39;re most welcome to contribute!) &amp;nbsp;I look forward to hearing any and all feedback, for better or worse.&lt;/p&gt;
&lt;p&gt;Enjoy!&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=68335" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="S#arp Lite" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/S_2300_arp+Lite/default.aspx" /></entry><entry><title>S#arp Architecture 1.6 Released!</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2010/08/19/s-arp-architecture-1-6-released.aspx" /><id>/blogs/billy_mccafferty/archive/2010/08/19/s-arp-architecture-1-6-released.aspx</id><published>2010-08-19T15:24:00Z</published><updated>2010-08-19T15:24:00Z</updated><content type="html">&lt;p&gt;The enthusiastic and dedicated Alec Whittington took over as lead developer of S#arp Architecture earlier this year. &amp;nbsp;Since then, he&amp;#39;s worked hard to maintain a clear direction, to further improve S#arp Architecture while adhering to its original&amp;nbsp;principles, and to include additional great talent in the core development team including the guys behind the &lt;a href="http://whocanhelpme.codeplex.com/"&gt;Who Can Help Me&lt;/a&gt; project sample (Howard van Rooijen, James Broome, and Jon George). &amp;nbsp;Through a lot of hard work, the team has released S#arp Architecture 1.6. &amp;nbsp;Alec&amp;#39;s announcement:&lt;/p&gt;
&lt;blockquote&gt;&lt;a href="http://wiki.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/sharp_5F00_arch_5F00_logo.gif" style="border:0;float:right;" alt="" /&gt;&lt;/a&gt;It has been a while since the last release and we have had some nice features added. The biggest items in this release are:&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Fluent NHibernate 1.1 - We are now using version 1.1.0.635. The AutoMappingPersistenceGenerator has been updated to use the new bits as well.&lt;/li&gt;
&lt;li&gt;NHibernate Configuration caching has now been added&lt;/li&gt;
&lt;li&gt;CRUD forms now enable client side validation&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;This release was almost 100% done by the community, that is amazing! I would like to thank everyone who contributed to this release. I truly appreciate your help and feel that it benefits the community greatly.&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;strong&gt;Whats Next? &lt;/strong&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;This will be the last version to support VS 2008 as well as .NET 3.5. We will keep a branch available for the community to maintain, but no further development will be done against this branch. The next version will be tagged 2.0, support only VS 2010 and .NET 4.0. &lt;/blockquote&gt;
&lt;blockquote&gt;As you may have heard, we are in the process of a partial rewrite. We are working on increasing the code coverage as well as some new bits. While I will not go into details, lets just say one of our focuses is to make sure the installation and solution creation become easier and less problematic. We are also going with a more Who Can Help Me solution structure for not only the S#arp Architecture project, but also the default template. We all felt that this structure further reinforced the overall project goal of good design practices. We are also going to try and deliver better documentation as well as an expanded line of samples. On that note, the Who Can Help Me application as joined the Northwind sample as an official sample. There are many many other things as well, but they are still in the early stages, so we will leave them there for now. Once they get better defined, we will let everyone know.&lt;/blockquote&gt;
&lt;p&gt;I needn&amp;#39;t state how pleased I am to see S#arp Architecture not just surviving, but thriving. &amp;nbsp;I have immense appreciation to Alec, the core team, and the S#arp community for the continued support and progress of this project.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;re new to S#arp Architecture, I invite you to learn more at&amp;nbsp;&lt;a href="http://wiki.sharparchitecture.net/"&gt;http://wiki.sharparchitecture.net/&lt;/a&gt;.&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=61555" 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>Are additional layers of abstraction warranted?</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2010/08/18/are-additional-layers-of-abstraction-warranted.aspx" /><id>/blogs/billy_mccafferty/archive/2010/08/18/are-additional-layers-of-abstraction-warranted.aspx</id><published>2010-08-18T22:27:00Z</published><updated>2010-08-18T22:27:00Z</updated><content type="html">&lt;p&gt;I was presented with the following discussion opener concerning the management of complexity via the introduction of abstraction...
&lt;/p&gt;
&lt;blockquote&gt;How do we handle complexity? I asked a few software architects I know and all of them answered, &amp;ldquo;Abstraction.&amp;rdquo; Basically they&amp;#39;re right, but being a math major in college, there is a principal I believe that software architects and designers miss - complexity is constant. In other words, if you&amp;rsquo;re designing a system that is inherently complex, you can&amp;rsquo;t reduce it.  In chemistry I remember learning that energy is constant, it can&amp;rsquo;t be created or destroyed, it&amp;#39;s just there.  In software, if the solution to a problem is complex, the complexity is always going to be there.&lt;/blockquote&gt;
&lt;blockquote&gt;What about abstraction?  Abstraction basically hides complexity. This is good, right? The problem is, in a lot of designs, once abstraction hides complexity the designers tend to forget about it.  If you work hard at a good design to hide the complexity and forget about it, it will come back and haunt you some day. So what does one do?&lt;/blockquote&gt;
&lt;p&gt;
I love this discussion!  The heart of this is determining if the introduction of abstractions, for the sake of hiding complexity, is truly an overall benefit.  In robotics, we&amp;#39;re ever dealing with increasing levels of abstraction for this very reason.  For example, in &lt;a href="http://www.sharprobotica.com/?p=13"&gt;Architectural Paradigms of Robotic Control&lt;/a&gt;, I briefly discussed the 3T architecture which has three separate layers, implemented as increasing levels of abstraction.  E.g., the skill/servo layer would likely be implemented in C++, the sequencing/execution layer might be implemented as a sequence modeling language, such as &lt;a href="http://www.sharprobotica.com/?p=179"&gt;ESL&lt;/a&gt; or &lt;a href="http://code.google.com/p/europa-pso/wiki/NDDLReference"&gt;NDDL&lt;/a&gt;, while the planning/deliberative layer might be implemented with a higher abstraction yet, such as with the &lt;a href="http://cs-www.cs.yale.edu/homes/dvm/"&gt;Planning Domain Definition Language&lt;/a&gt; or &lt;a href="http://cs-www.cs.yale.edu/homes/dvm/papers/opt-manual.pdf"&gt;Ontology with Polymorphic Types (Opt)&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In response to the concerns put forth, I would tend to agree that abstraction hides complexity and that it does make it more likely that you may be bitten by the hiding of the complexity.  With that said, encapsulation of complexity into well formed abstractions is an inevitable step to facilitate taking on increasingly complex problems.  For example, in the .NET world of data access, &lt;a href="http://fluentnhibernate.org/"&gt;Fluent NHibernate&lt;/a&gt; is really just a way of hiding the complexities of &lt;a href="http://nhforge.org/Default.aspx"&gt;NHibernate&lt;/a&gt;.  NHibernate is really just a way of hiding the complexities of &lt;a href="http://msdn.microsoft.com/en-us/library/h43ks021(VS.71).aspx"&gt;ADO.NET&lt;/a&gt;.  ADO.NET is really just a way of hiding the complexities of communicating to a database via TCP/IP sockets, or whatever underlying mechanism is employed.  Along the same vein, tools such as &lt;a href="http://acs.ist.psu.edu/herbal/"&gt;Herbal&lt;/a&gt;, NDDL, and ESL are similarly provided as a means to provide an abstraction for hiding complexity.&lt;/p&gt;
&lt;p&gt;Because these layers of complexity have been encapsulated in a manageable fashion, we&amp;#39;re now able to take on project work which would be far too complex to manage if we were using a lower level of implementation, e.g., pure C++, or Assembly for that matter.  Indeed, there will be times when the added layers of abstraction will make it more difficult to tweak a low level capability, but the improved complexity management that the abstractions provide &lt;em&gt;should&lt;/em&gt; far outweigh the sacrifice of losing &lt;em&gt;some&lt;/em&gt; low level capabilities.&lt;/p&gt;
&lt;p&gt;I think the crux for determining if an abstraction is worthwhile:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Does the abstraction reduce complexity of interacting with the underlying layer that it encapsulates?&lt;/li&gt;
&lt;li&gt;Does the abstraction make it easier to tackle increasingly complex problems?&lt;/li&gt;
&lt;li&gt;Does the abstraction provide enough tweak points to accommodate the 5-10% of times that more low level control is needed?&lt;/li&gt;
&lt;li&gt;Does the abstraction increase maintainability and ease of understanding of the overall goals of the system?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
If the answers to the above are yes, then I believe that the encapsulation of complexity, codified as a new layer of abstraction, is pulling its weight.  Otherwise, you might not want to throw that Assembly language reference away just yet.&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=61535" 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>Announcing SharpRobotica.com ...the softer side of robotics</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2010/07/12/announcing-sharprobotica-com-for-the-softer-side-of-robotics.aspx" /><id>/blogs/billy_mccafferty/archive/2010/07/12/announcing-sharprobotica-com-for-the-softer-side-of-robotics.aspx</id><published>2010-07-13T03:47:00Z</published><updated>2010-07-13T03:47:00Z</updated><content type="html">&lt;p&gt;After confidentaly turning over the reigns of &lt;a href="http://wiki.sharparchitecture.net"&gt;S#arp Architecture&lt;/a&gt; to the adept and au fait hands of&amp;nbsp;&lt;a href="http://weblogs.asp.net/alecwhittington/"&gt;Alec Whittington&lt;/a&gt;, I&amp;nbsp;capriciously&amp;nbsp;&lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2010/02/03/software-architectural-approaches-for-robotics.aspx"&gt;announced&lt;/a&gt; that I&amp;#39;d be turning my writing attentions towards robotics...with the diabolical intentions of taking over the world (obviously).&lt;/p&gt;
&lt;p&gt;It became apparent after a few posts that the direction I began taking was diverging far from the subject matter of my&amp;nbsp;esteemed&amp;nbsp;colleagues at devlicio.us. &amp;nbsp;Rather than dilute the theme of&amp;nbsp;devlicio.us, I figured it was time to create the proper context for such topics...&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.sharprobotica.com/"&gt;&lt;img src="http://www.sharprobotica.com/wp-content/themes/starscape/styles/logos/sharp_robotica.png" border="0" style="max-width:550px;border:0;float:right;" alt="" /&gt;&lt;/a&gt;It&amp;#39;s my great pleasure to announce the humble beginnings of &lt;a href="http://www.sharprobotica.com/"&gt;SharpRobotica.com&lt;/a&gt;. &amp;nbsp;This blogging community (with an author population of uno at the moment, being yours truly) emphasizes and explores the software side of robotics. &amp;nbsp;While there are many robotic sites, the focus of SharpRobotica.com, accordingly, is in bringing professional software techniques and insight to this vernal, yet burgeoning, industry.&lt;/p&gt;
&lt;p&gt;For years, hardware lagged far behind software in the world of robotics; developers were tirelessly constrained by the performance and capabilities of the available hardware platforms. &amp;nbsp;But with the &lt;a href="http://upload.wikimedia.org/wikipedia/commons/6/68/Shakey.png"&gt;hardware&lt;/a&gt; of &lt;a href="http://www.aldebaran-robotics.com/en"&gt;robotics&lt;/a&gt; &lt;a href="http://www.bostondynamics.com/robot_littledog.html"&gt;becoming&lt;/a&gt;&amp;nbsp;&lt;a href="http://robotic.media.mit.edu"&gt;startlingly&lt;/a&gt;&amp;nbsp;&lt;a href="http://www.robotcub.org/"&gt;sophisticated&lt;/a&gt;, there is now incredible opportunity and demand for solid algorithms and software solutions for balancing the scale once again, by creating software which can adeptly take advantage of available robotics hardware to conquer ever challenging contexts such as &lt;a href="http://www.france24.com/en/20100712-robots-replace-cap-over-gushing-bp-oil-well"&gt;underwater&lt;/a&gt;, &lt;a href="http://robonaut.jsc.nasa.gov/default.asp"&gt;space&lt;/a&gt;, &lt;a href="http://www.pcworld.com/article/186576/true_companion_sexbot_alters_the_dating_landscape.html"&gt;entertainment&lt;/a&gt;, &lt;a href="http://www.scientificamerican.com/article.cfm?id=three-mile-island-robots"&gt;hazardous environments&lt;/a&gt;, and &lt;a href="http://www.zygbotics.com/2009/03/27/robot-nurses-to-care-for-japanese-elderly-within-five-year/"&gt;in the home&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So if you have an interest in robotics and/or interested in applying your software skills to a completely new context, many of the posts and discussions might be right up your alley. &amp;nbsp;Also, &lt;strong&gt;if you&amp;#39;re interested in being an author on SharpRobotica.com&lt;/strong&gt;, &lt;a href="http://www.sharprobotica.com/wp-login.php?action=register"&gt;sign up&lt;/a&gt; as a user and &lt;a href="http://www.sharprobotica.com/?page_id=186"&gt;let me know you&amp;#39;re interested&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;So while I&amp;#39;ll still be showing up now and again on devlicio.us...I invite you to also&amp;nbsp;&lt;a href="http://www.sharprobotica.com/?feed=rss2"&gt;subscribe&lt;/a&gt; for a gander to SharpRobotica.com.&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;br /&gt;Billy McCafferty&amp;nbsp;&lt;/p&gt;
&lt;p&gt;P.S. Thanks &lt;a href="http://codebetter.com/blogs/brendan.tompkins/"&gt;Mr. Tompkins&lt;/a&gt; (audacious&amp;nbsp;founder of &lt;a href="http://codebetter.com/"&gt;CodeBetter.com&lt;/a&gt; and &lt;a href="http://devlicio.us/"&gt;devlicio.us&lt;/a&gt;) for the support of this announcement!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=61075" width="1" height="1"&gt;</content><author><name>Billy</name><uri>http://devlicio.us/members/Billy/default.aspx</uri></author><category term="Robotics" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Robotics/default.aspx" /></entry><entry><title>Checklist for Developing Message-Based Systems</title><link rel="alternate" type="text/html" href="/blogs/billy_mccafferty/archive/2010/03/23/checklist-for-developing-message-based-systems.aspx" /><id>/blogs/billy_mccafferty/archive/2010/03/23/checklist-for-developing-message-based-systems.aspx</id><published>2010-03-23T21:22:00Z</published><updated>2010-03-23T21:22:00Z</updated><content type="html">&lt;p&gt;Even when developing the most basic CRUD application, we ask ourselves a number of questions - whether we realize it or not - during the initial phases of development concerning the architecture and construction of the project.&amp;nbsp; Where will the data be persisted?&amp;nbsp; What mechanism will be used to communicate with the database?&amp;nbsp; How will data from the database be transformed into business objects?&amp;nbsp; Should separated interfaces and dependency injection be employed to maintain a clean separation of concerns between application logic and data access objects?&amp;nbsp; What UI components will be leveraged to speed development of the UI layer?&amp;nbsp; When developing message-based systems (see &lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2010/03/01/message-based-systems-for-maintainable-asynchronous-development.aspx"&gt;Message-Based Systems for Maintainable, Asynchronous Development&lt;/a&gt; for an introduction) it&amp;#39;s immensely helpful to formalize such questions about how the systems will be designed and developed.&amp;nbsp; Creating a checklist of such items to decide upon, and formalizing answers for the application context, helps to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Standardize how components (applications participating in the message-based system) will communicate with the messaging middleware;&lt;/li&gt;
&lt;li&gt;Provide architectural guidance throughout project development concerning how components will leverage message end-points;&lt;/li&gt;
&lt;li&gt;Define the data model that will be used for sending commands and exchanging data among components;&lt;/li&gt;
&lt;li&gt;Assist new developers on the team with getting up to speed with maintaining and extending the message-based system; and&lt;/li&gt;
&lt;li&gt;Support consistency (and predictable maintainability) amongst the components of the system.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This post provides an addendum, if you will (I&amp;#39;m sure you will), to &lt;a href="http://devlicio.us/blogs/billy_mccafferty/archive/2010/03/01/message-based-systems-for-maintainable-asynchronous-development.aspx"&gt;Message-Based Systems for Maintainable, Asynchronous Development&lt;/a&gt;&amp;nbsp;for complementing the article with a checklist of questions and architectural topics that should be discussed, decided upon, and/or spiked before beginning development of your budding message-based system. &amp;nbsp;Accordingly, this does not describe a methodology for developing message-based systems, but simply a checklist of topics which should be taken into consideration before development begins.&amp;nbsp; Many of the checklist items below serve as good starting points for team discussion and for the development of architectural spikes for demonstrating implementation details.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Messaging Middleware&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What messaging middleware solution will form the background of the message-based system?&lt;/li&gt;
&lt;li&gt;How are channels addressed in the selected middleware? &amp;nbsp;(E.g., as a string or as a port number?)&lt;/li&gt;
&lt;li&gt;How does the nomenclature of the middleware match up to standard elements of message-based systems and design patterns? &amp;nbsp;E.g., in &lt;a href="http://www.ros.org/wiki/"&gt;Robot Operating System (ROS&lt;/a&gt;), a channel is called a &amp;quot;topic.&amp;quot; &amp;nbsp;Likewise, is their middleware specific nomenclature for elements such as component, publisher, subscriber, point-to-point channel, publish/subscribe channel, message, command message, request-reply message, router, message end-point, and other messaging design patterns?&lt;/li&gt;
&lt;li&gt;What mechanism is available for peeking at messages going over a point-to-point channel?&lt;/li&gt;
&lt;li&gt;What utilities are available for viewing active channels, publishers, and subscribers?&lt;/li&gt;
&lt;li&gt;What other tools does the middleware provide for debugging and observing the messaging system? &amp;nbsp;Will custom utilities need to be developed to autment development and debugging capabilities?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Integration with Messaging Middleware&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;How will message end-points publish a messages?&lt;/li&gt;
&lt;li&gt;How will message end-points subscribe to a channel for messages?&lt;/li&gt;
&lt;li&gt;How will message end-points poll a channel?&lt;/li&gt;
&lt;li&gt;What technique will be used to decouple the component (participating application) from the message end-point when sending a message? &amp;nbsp;E.g., dependency injection, separated interface, or other?&lt;/li&gt;
&lt;li&gt;What technique will be used to decouple the component from the message end-point when receiving a message or polling a channel? &amp;nbsp;E.g., callback, separated interface, or other?&lt;/li&gt;
&lt;li&gt;Should exceptions raised by the messaging middleware be wrapped in component specific exceptions for looser coupling? &amp;nbsp;If so, what will be the standard exceptions? &amp;nbsp;Additionally, how will such exceptions be logged and tracked for debugging; e.g., email alerts, logging, exception message channel?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Channels and Routing&lt;/strong&gt;&lt;/p&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;What channels will be created and what data type should each channel carry?&lt;/li&gt;
&lt;li&gt;Will a hierarchical naming strategy be leveraged for naming and organizing channels? &amp;nbsp;If so, what are the naming conventions for the project?&lt;/li&gt;
&lt;li&gt;Has an invalid message channel been setup with a logger to capture and record invalid messages?&lt;/li&gt;
&lt;li&gt;How should a predictive router be implemented, when needed?&lt;/li&gt;
&lt;li&gt;How should a reactive router (e.g., a message filter) be implemented, when needed?&lt;/li&gt;
&lt;/ul&gt;
&lt;strong&gt;Message Elements&lt;/strong&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;What data elements may the header of a message contain and how should each be specified? &amp;nbsp;E.g., return address, message expiration, timestamp, origin, format indicator, etc.&lt;/li&gt;
&lt;li&gt;How should a message identifier and correlation identifier be generated and included in the header?&lt;/li&gt;
&lt;li&gt;What is the messaging data model supported and/or enforced by the messaging middleware? &amp;nbsp;E.g., ROS supports&amp;nbsp;&lt;a href="http://www.ros.org/wiki/msg"&gt;http://www.ros.org/wiki/msg&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;What canonical data model will be used to imbue message content with domain specific semantic meaning? &amp;nbsp;E.g., &lt;a href="http://en.wikipedia.org/wiki/JAUS"&gt;JAUS&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;How will message translators and/or message mappers be leveraged for converting between each component&amp;#39;s domain model and the canonical data model?&lt;/li&gt;
&lt;li&gt;How will a command, document, event and request-reply message be composed?&lt;/li&gt;
&lt;/ul&gt;
&lt;strong&gt;Other Topics&lt;/strong&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;How will components maintain information that is associated with a correlation identifier?&lt;/li&gt;
&lt;li&gt;Are there existing components which need to interact with the messaging middleware which cannot be modified?&amp;nbsp; If so, how will each communicate with the messaging middleware?&lt;/li&gt;
&lt;li&gt;How will each component participate with the message based system; e.g., as a polling consumer, as an event-driven consumer, as strictly an event publisher, as a combination of these?&lt;/li&gt;
&lt;li&gt;Should exceptions raised by a component participate in a global exception tracking and debugging mechanism?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As stated previously, this checklist is not intended to provide a methodology for developing message-based systems, but should serve as a good basis to make sure &amp;quot;T&amp;#39;s are crossed and I&amp;#39;s are dotted&amp;quot; when deciding upon the major architectural aspects and development techniques that will be leveraged throughout the development of your (team&amp;#39;s) message-based system.&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;br /&gt;Billy McCafferty&lt;/p&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=56236" 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="Quality Assurance" scheme="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Quality+Assurance/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>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 width="400" src="http://who-can-help.me/Views/_Content/img/logo.png" border="0" style="border:0;float:right;margin:4px;" 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 class="c-sharp" name="code"&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 class="c-sharp" name="code"&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 class="c-sharp" name="code"&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 height="91" width="206" align="right" src="http://devlicio.us/resized-image.ashx/__size/600x450/__key/CommunityServer.Components.UserFiles/00.00.00.21.06/logo_5F00_web_5F00_gif.gif" 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 style="border:0;float:right;margin:5px;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/Robot.png" border="0" 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 class="c-sharp" name="code"&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 style="border:0;float:right;margin:5px;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/C3PO.png" border="0" 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 class="c-sharp" name="code"&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 style="border:0;float:right;margin:5px;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/DI_5F00_RefactorToPackages.png" border="0" 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 class="c-sharp" name="code"&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 style="border:0;float:right;margin:5px;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/DI_5F00_AddInterfaces.png" border="0" 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 style="border:0;float:right;margin:5px;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/DI_5F00_RefactorToDI.png" border="0" 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 class="c-sharp" name="code"&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 style="border:0;float:right;margin:5px;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/DI_5F00_RefactorToSeparatedInterface.png" border="0" 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 class="c-sharp" name="code"&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 class="c-sharp" name="code"&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 class="c-sharp" name="code"&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 style="border:0;float:right;margin:5px;" src="http://devlicio.us/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/billy_5F00_mccafferty/RobotInvasion.png" border="0" 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></feed>
