Dependency Injection – Simple Injector in Onion Layer Architecture

dependency-injection

I'm trying to find a good resource, advice, etc in a project I've taken on for myself at work.

Background

When I started here we had a application that was part MVC part Webform (separate projects on solution) that was tightly coupled to it's DAL with business code all over the place.

I refactored the entire solution into the onion layer architecture almost two years ago using ninject. killed of it's SOAP services in favor of WebAPI and moved the vast majority of the webforms into MVC overtime. (still some left in webforms, but their days are numbered)

Overall this has been a major success in regards to maintainability, stability, and getting our business logic where it belongs. Lately though performance has been a growing issue. We've establish multiple causes, but we were surprised at how badly ninject was impacting our performance, after some research we found ninject was infact one of the worst DI frameworks from a performance stand point. With this in mind we're moving to simple injector as it impressed us.

The problem

We don't just rewrite our work, we gradually move it where able. We all know we can't realistic blend ninject with simple and not expect utter chaos. With that in mind we cleaned up our code to make sure it all would work with both ninject and simple and we're to that final step of actually wiring the whole thing up…

Except every guide, tutorial, and example I've found has the actual UI project (MVC) reference both the interface and the project I'll inject my classes from…

Example:

container.Register<ILoggingService, LoggingService>();

With Onion Architecture we strived to make sure our front end had no direct references to the supporting code. This was handled by having a project separate from my MVC project to handle injection thereby allowing me to never have a reference between my UI and my backend code. (Thus preventing you from accidentally bypassing your interface and DI)

The above would require me to directly reference the backend code and in my opinion fundamentally defeat what we've strived to do. Perhaps my heads not all together from being tired + mentally on holiday already, but I'm having a heck of a time trying to figure out how to accomplish what we had under ninject in simple injector (I'm sure it's stupid easy, but I've been beating my head on this for hours so any help would go a long way)

Best Answer

Usually, there is some kind of "Application" or "Launcher" project, which references every assembly the application needs to run. Only responsibility of this project is to wire up the dependencies and start up the application. It has no UI and no behavior. But I'm not sure how this would handle in MVC application.

Related Topic