What is a composition root in the context of dependency injection?

The composition root is the single place in your application where the composition of the object graphs for your application take place, using the dependency injection container (although how this is done is irrelevant, it could be using a container or could be done manually using pure DI). There should only be one place where … Read more

Why do I need an IoC container as opposed to straightforward DI code? [closed]

Wow, can’t believe that Joel would favor this: var svc = new ShippingService(new ProductLocator(), new PricingService(), new InventoryService(), new TrackingRepository(new ConfigProvider()), new Logger(new EmailLogger(new ConfigProvider()))); over this: var svc = IoC.Resolve<IShippingService>(); Many folks don’t realize that your dependencies chain can become nested, and it quickly becomes unwieldy to wire them up manually. Even with factories, … Read more

Why not use an IoC container to resolve dependencies for entities/business objects?

The first question is the most difficult to answer. Is it bad practice to have Entities depend on outside classes? It’s certainly not the most common thing to do. If, for example, you inject a Repository into your Entities you effectively have an implementation of the Active Record pattern. Some people like this pattern for … Read more

Is there a pattern for initializing objects created via a DI container

Any place where you need a run-time value to construct a particular dependency, Abstract Factory is the solution. Having Initialize methods on the interfaces smells of a Leaky Abstraction. In your case I would say that you should model the IMyIntf interface on how you need to use it – not how you intent to … Read more

Ioc/DI – Why do I have to reference all layers/assemblies in application’s entry point?

If I wasn’t using a DI container, I wouldn’t have to reference EntityFramework library in my MVC3 app, only my business layer which would reference my DAL/Repo layer. Yes, that’s exactly the situation DI works so hard to avoid 🙂 With tightly coupled code, each library may only have a few references, but these again … Read more