Programming Stuff

Onion Architecture in Action – Get Started

Get Started: How to Implement Onion Architecture Successfully


In this post, I am going to create a demo implementing Onion Architecture. Here some links about this topic.


  • IDE: Visual Studio 2010 / C#
  • Data: Dummy List
  • IoC: StructureMap

The ToDo Application will allow you to create a task (with a random time on the future), edit it (mark as completed) or delete it.

First, I was unsure what .Net DI Container to use for IoC but after I checked this website ( I was pretty sure that I want to use either StrucuteMap or CastleWindsor. Jeffrey Palermo suggested those as well.

1. Create an empty Class Library project for Core and don’t add any assemblies or libraries

Here is where I create Interfaces and domain logic. Here you will find things like Data Repository, Domain Service, Domain Object etc.

2. Create another Class Library project for Infrastructure and add reference to Core project.

In this case, I only used this project to implement the Data Repository. But it is suitable for Web Services, I/O, etc. A real application may use Entity Framework (EF), then you probably have to add another project like Infrastructure.Data and add EF references to it. *

3. Create an ASP.Net MVC Project for UI/Presentation and add only reference to Core. Make it a Default Project.

I also create another project for Model (UI.ViewModel) and add the Annotation reference to it*. This is a ideal for mapper so you may don’t have to reference the whole UI project on an external mapper.

The constructor of the MVC Home controller was changed. We implemented the strategy pattern adding the domain service for later injection.

4. Create another Class Library project for Dependency Resolution and add references to the Core, Infrastructure and maybe the UI.ViewModel (if it required – no in this case).

Using NuGet, add the StructureMap for ASP.Net MVC 4. This required ASP.Net MVC references, but ignore that, and remove any reference to that. Since I will use ASP.Net MVC built-in feature called controller factory, that will “replace” the removed logic and work!


Use IoCControllerFactory** to implement the ASP.Net MVC default controller factory.

Map or resolve interfaces with concrete classes and set the Service Locator**


In Dependency Resolution project, Go to Properties > Build > and change Output path to the bin folder of the UI as I show you in the Picture, and when you run your (UI) application, it will pick this project up on start.


What is next?:

  • Map: Using AutoMapper
  • Test: Unit test

* Here a nice structure:

** IoCControllerFactory Source:

Demo Source: Demo Onion-Architecture