concept composite in category dependency injection

appears as: Composites
Dependency Injection

This is an excerpt from Manning's book Dependency Injection.

  • Decorators or Composites are in use.
  • Not all ICommandService<TCommand> implementations will be registered, though. Simple Injector skips open-generic implementations, Decorators, and Composites, as they often require special registration. We’ll discuss this in section 14.4.

    Let’s take a look at how you can register Composites, such as the CompositeNotificationService from chapter 6 in Simple Injector. The following listing shows this class again.

    Listing 14.12 The CompositeNotificationService Composite from chapter 6
    public class CompositeNotificationService : INotificationService
    {
        private readonly IEnumerable<INotificationService> services;
    
        public CompositeNotificationService(
            IEnumerable<INotificationService> services)
        {
            this.services = services;
        }
    
        public void OrderApproved(Order order)
        {
            foreach (INotificationService service in this.services)
            {
                service.OrderApproved(order);
            }
        }
    }

    Because the Simple Injector API separates the registration of sequences from non-sequence registrations, the registration of Composites couldn’t be any easier. You can register the Composite as a single registration, while registering its Dependencies as a sequence: