Working with Sitecore, Part Two: Component-based Architecture

Wednesday, February 20, 2013 @ 11:35

By: Josh Jenkins, Lead Consultant

Component-based Architecture " a reuse-based approach to defining, implementing and composing loosely coupled independent components into systems. This practice aims to bring about an equally wide-ranging degree of benefits in both the short-term and the long-term for the software itself and for organizations that sponsor such software." - [Wikipedia]

Jens Mikkelsen wrote a series of articles on Sitecore, and in Part 3 he touches on Component-based architecture as it relates to Sitecore:

"A component is an independent group of classes, renderings, templates, database and/or items, which represents a logically grouped set of functionality and is independently deployable."

My goal, as I stated when going over The Onion Architecture, is "to reduce coupling as much as I can. What I am really aiming for is the ability to swap out components as painlessly as possible." So it makes sense that I agree with Mr. Mikkelsen.

Let's take an example. I have a Template in Sitecore called IDocument and it has three fields: Document Title, Document Summary and Document Text. Any other template that needs to represent a Document item will inherit this template. Along with the template I have a Sublayout that references a User Control that I'll use to render the contents of the template's fields.

First off, in Sitecore I place the template in a folder called Document under \Templates\User Defined\Components:

Second, I follow the same convention and place the Sublayout in a folder called Document, this time under the \Layout\Sublayouts\Components item :

Now for the slightly more complicated part. I have created a Template Project in VS that copies its output to my main Website project when it's built. For each new component I add a new project to my solution that is based on my template project.

I do this by modifying the AfterBuild Target in the .csproj file:

It is possible that the Solution structure will change, so if it does you'll need to be mindful and change the paths in the Copy task. Other than that, it's pretty simple.

The Template project itself looks something like this:

I should note that I am relying on TDS and its File Replacement functions. The Build on the solution will copy the contents of the Component projects into my Website project. From there TDS will move it out to my working IIS directory.