Search code examples
visual-studioinversion-of-controlioc-container

Ioc container placement within enterprise application


I've been looking into Ioc containers and AOP recently, and I'm pretty amazed by the concepts. I'm struggling however to decide how and where to implement the container.

The articles below suggest implementing the container in the 'application entry point':

Now - my thought-experiment application will consist of multiple visual studio projects ( one for data access, winforms application ). And let's say I want to use AOP for logging with Log4net, and so I setup log4net in the Ioc container. So WinForms application in entry point, that's where Ioc container should go.

Here's the question: if I want to log stuff in my data access project/layer, should I add a reference to my winforms application, get the ioc container from there, get the log4net instance out of it and use it for logging?

That would mean my data-layer depends on winforms application, that can't be right. How about I put the container is something like a 'Common' project within the solution. That way, all related projects (Data access/winformsa etc.) can access the container. What is the right way to go here?


Solution

  • Your application's Composition Root would be the Windows Forms project. This is the only project which should have a reference to a DI Container.

    In all other projects, dependencies should be injected via Constructor Injection. All decent DI Containers understand this pattern and use it to Auto-wire dependencies from the Composition Root.