Introducing dependency injection

Dependency Injection design pattern fulfills the dependency inversion principle of the SOLID design principles. There are three main forms of dependency injection:

  • Constructor injection: An example of this is shown in the DIP section
  • Setter injection: Let's look at an example code for setter injection:
    public class OrderProcessorWithSetter : IOrderProcessor     {         private IOrderRepository _orderRepository;         private IOrderNotifier _orderNotifier;         public IOrderRepository Repository         {             get { return _orderRepository; }             set { _orderRepository = value; }         }         public IOrderNotifier Notifier         {             get { return _orderNotifier; }             set { _orderNotifier = value; }         }         public void Process(IOrder order)         {  //Perform validations.. ...

Get Enterprise Application Architecture with .NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.