Working with Sitecore, Part One: Onion Architecture

Wednesday, February 20, 2013 @ 02:27

By: Josh Jenkins, Lead Consultant

In 2008 Jeffery Palermo coined a new architectural pattern called the Onion Architecture. He published a series of posts on it here: The Onion Architecture. A few years later he followed up with a post, The Onion Architecture After Four Years, where he shared how it's been adopted over the last few years.

The Onion Architecture's premise is that it controls coupling by relying on the fundamental rule that all code can depend on inner layers but code cannot depend on layers further out. It moves all coupling towards the center.

The four tenets of the Onion Architecture are:
- The application is built around an independent object model.
- Inner layers define interfaces. Outer layers implement interfaces.
- Direction of coupling is toward the center.
- All application core code can be compiled and run separate from infrastructure.

At the the end of the day my goal 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. I believe that a solution modeled on the Onion Architecture helps get me there

So how do I apply this to Sitecore? Well, first off I need to mention that I use the Glass Sitecore Mapper, an object mapping framework for mapping Sitecore items directly onto an object model. This allows me to model my entire Sitecore solution in code.

To do this, we decorate our Glass Classes and Interfaces with attributes to help it understand what to do. In Glass we would define an Interface for a Sitecore Template like so:

The main issue I've run into is that my Model and Interface projects needs to reference Glass, and since my Model and Interface projects reference Glass then obviously I am breaking the tenets of the Onion Architecture.

I've attempted "solving" this by adjusting where my Core resides and moving Core.Model to Infrastructure.Glass.Model and Infrastructure.Glass.Interfaces to show a clear separation. It works, right? But...

Is it cheating? I mean, I'm just moving the namespace and doing the same thing. Did I solve anything? I've also tried creating Application.Core.Interfaces and placing the interfaces without the Glass attributes there, but I found that sometimes I have a field that is a Glass field (such as an Image) and in the end I always wind up at the same place I started.

In the end, I've learned to say, "Fine. Sitecore is in control and I am using Glass to model it, and to best represent what my goal is I am sticking my Glass Model and Interfaces in the Core namespace. Yes, even though it means I am breaking the Onion Architecture by referencing outside assemblies." Why? Well that part is easy: doing it like that seems to be easy to explain to other developers. "You create your template, then you'll need to create the Model/Interfaces for it in the Core." Simple, right? It seems to make sense.

Trust me, I can be convinced otherwise for sure. As a matter of fact, if you have a better solution or disagree, I'm all ears. Let me have it.

Anyways, in the end my project in Visual Studio looks something like this: