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

Dependency injection with Jersey 2.0

You need to define an AbstractBinder and register it in your JAX-RS application. The binder specifies how the dependency injection should create your classes. public class MyApplicationBinder extends AbstractBinder { @Override protected void configure() { bind(MyService.class).to(MyService.class); } } When @Inject is detected on a parameter or field of type MyService.class it is instantiated using the … 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

Spring JSF integration: how to inject a Spring component/service in JSF managed bean?

@ManagedBean vs @Controller First of all, you should choose one framework to manage your beans. You should choose either JSF or Spring (or CDI) to manage your beans. Whilst the following works, it is fundamentally wrong: @ManagedBean // JSF-managed. @Controller // Spring-managed. public class BadBean {} You end up with two completely separate instances of … Read more