Nicolas Metaye

.NET SENIOR DEVELOPER

The main technologies I'm working with are .NET, Azure, JavaScript and SQL Server. I'm always happy to learn new patterns, concepts, technologies and languages (and not only the programming ones!). You will find on this web site my resume and some of my personal projects / mockups /experiments.

Tilburg, Netherlands

nicolas.metaye@gmail.com

Dependency Injection and IoC

SOURCE CODE

I decided to focus on modular design this time.
In the AutoMapper project, we had a single project containing all the views, business logic and data layer.

IOC

We now have:

  • a Common project with the basic extensions and helpers
  • a Domain project with the entities objects and the repositories (including data layer base management)
  • a Models project with the models and the mappings definition that are used in the controllers
  • and finally the MVC project with only the controllers and the views

Refactoring was the first step of making this project more "modular".
The second step of my approach was to implement in my code DI (Dependency Injection) and IoC (Inversion of Control).

DI

To make it short, Dependency Injection and Inversion of Control are object-oriented practices that helps you to implement your objects more independently as you "inject" the dependencies to the objects you are going to use.
It's called "inversion" as it doesn't follow the flow of the business logic usually determined by objects that are statically assigned to one another.
With DI and IoC, object interactions are defined through abstractions and the flow of business logic is instantiated by an assembler object at run-time.

It's a very powerful pattern that can allow to load dynamically the dependencies as "plugins". However in a more common way it also forces the developer to think his implementation as independent and simple units of code which obviously make everything more readable, maintainable, understandable and testable.

To achieve that in this project, I used StructureMap. The only subtle part is to set a controller factory in the global.asax to allow the controllers to be instantiated using StructureMap.
I defined only one IoC registry (in the Domain project) that defines the bindings between the interfaces and their implementations. It's basically using the defaut conventions.

Get in touch !