Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best IoC practices in large projects

What techniques have you found useful for managing dependencies of large projects with Inversion of Control containers?

Do you bootstrap everything in one place, or do you split it up?

Do you ever use multiple containers?

like image 635
James L Avatar asked Mar 17 '10 09:03

James L


People also ask

What is IoC example?

A great example of an implementation of IoC is Spring Framework. The Spring container instantiates and manages the lifecycle of the objects that are a part of the program. The user in the configuration file provides the information related to what objects and dependencies are required by the application.

Is IoC a framework?

The IoC container is a framework used to manage automatic dependency injection throughout the application, so that we as programmers do not need to put more time and effort into it. There are various IoC Containers for .

What are the important roles of an IoC container?

The IoC container is responsible to instantiate, configure and assemble the objects. The IoC container gets informations from the XML file and works accordingly. The main tasks performed by IoC container are: to instantiate the application class.

What are the beneficial effects of DI IoC?

Some benefits of using IoC. It is easy to switch between different implementations of a particular class at runtime. It increases the modularity of the program. It manages an object's life-cycle and configuration.


1 Answers

From an architectural viewpoint it's important to maintain a razor-sharp focus on where your Composition Root is. It should be as close to the application's entry point as at all possible, and you should compose the entire dependency graph in a single place.

Doing otherwise can create confusion about responsibilities, and you also risk to introduce all sorts of subtle bugs because the IFoo instance resolved in one place may or may not be the same as an IFoo instance resolved in another place.

If the application is so large that resolving an entire dependency graph in one go simply is prohibitively expensive, you can address this by using lazy loading lifetimes in strategic places (around Aggregate Services).

Conceptually, I always only have a single container. (I occasionally have multiple parent/child containers in play to break some circular dependencies, but that's an implementation detail.)

like image 87
Mark Seemann Avatar answered Dec 06 '22 23:12

Mark Seemann