Skip to main content

DI : The Buzzword

On the last Monday morning, when I was reading "this blog" I came across this term 'injector' for enough time to give it a thought and dig into it. and That's how I got started with it.

DI: stands for Dependency Injection (ugh! what a fancy and confusing name!)

It is a very famous code pattern to make the codebase more cohesive and loosely coupled. Often, while coding, we write some classes which internally initialize the objects of other classes. And thus the earlier class become dependent on the object creation of the later class. But, thoughtfully speaking, a class should be cohesive and should do nothing more than it's a purpose.


For example, we have a class Employee and a class Address. Where an object of class Address is aggregated inside Employee class. Thus, Employee class, while providing a blueprint of an Employee object, now manages the creation of Address object too. This adds up the dependency and makes the class, less cohesive. What if, we provide the initialized object of Address class to Employee class's constructor while object initialization, provided that Employee class has provision to accept an Address object as an argument? Well, That's what dependency injection is! 
Dependency Injection is a 25-dollar term for a 5-cent concept
                                                                                              -- A Reddit User

In the above example(before the quote :P), when we say Employee class provides a way to accept an Address object in the constructor means Employee class has a way to inject an Address class. This is an entire idea of dependency injection. But there are many ways a class can provide a way to inject the object of other classes. For this purpose, some brilliant minds have come up with advanced frameworks too.

Advantages of Dependency Injection:

  • High Cohesion - Code becomes modular, less complex, more reusable, and of course easy to debug
  • Flexibility - As the object attributes are configured outside, there is nice flexibility in providing various definitions of the same component.
  • Easy Testing - Mocking objects and providing them to the class becomes easy. 
Disadvantage:

  • If overused, it can lead to management issues and other problems.
  • Many compile-time errors are pushed to run-time. As for IDE sometimes finding references becomes complex.
Often while discussing DI, another popular term that is used is IoC i.e. inversion of control.

IoC (Inversion of Control)

Inversion of control is an Architectural Pattern where the flow control of a system is provided by a generic library that calls into custom business logic. The inversion is the delegation of the control flow. Whereas in comparison, Dependency Injection is a code pattern that severs the creation of stateful dependencies from the use of those dependencies.

Thus, The Dependency Injection frameworks are the ones, on whom the code is dependent for object creation. Thus, DI is achieved through IoC, or DI is a form of IoC, the later sounds better.

Comments

Popular posts from this blog

System Design #1: Designing Live Commenting!

All of us surely have come across bunch of systems that supports live commenting. For example, facebook live commenting, twitch/youtube live stream commenting, reddit live stream commenting etc. Lets deep dive in the system that support the live commenting feature. Requirements: User should be able to see active real time comments on the post/video/stream across the globe. System should be highly available, fault tolerant.  Due to CAP theorem, we will need to trade the consistency. Consider our system to be eventually consistent. If the comment is made, its okay for us if it takes few seconds to appear everywhere else. Goal: to build a system to sync live comments across the demographies & data centers to build a system that supports the real time pushing of comments to the web/mobile clients. Estimation: Consider 100M Daily Active Users (DAU), 400M daily posts/videos/streams on the system and daily 10B comments being made on different streams/videos/posts.  To support such high sc

Behind the "Multiplexing of user threads over kernel threads" | Goroutines & Green Threads

Introduction I have been working on Golang for quite a time now. I explored a lot of features. The few that caught up my eye was 'Scalability' & 'Concurrency'. Scalability & Concurrency have been some of the major objectives behind the design of Golang. Let's dive in a bit. Threads  A thread is the unit of execution within a process. A process can have anywhere from just one thread to many threads. On a machine, we have multiple processes running and in these processes, we have independent or dependent threads aggregating computations.  Contextually, these threads are further broken down into two types, namely  User-Level Threads and Kernel Level Threads . The basic difference between these threads is that the kernel-level threads are managed, operated, and scheduled by the operating system(kernel), and user-level threads are managed, operated, and scheduled by the application layer.  Just to have more understanding about them, let's list dow

The stuff you should know about InnoDB | MySQL storage engine

It's been quite a while after the first blog about Storage Engines . But after that blog, the thing that hit me was how the databases like the great MySQL and the legend PostgreSQL works(subjective). While exploring MySQL I came across the famous, and default storage engine of MySQL , i.e. InnoDB . Whenever you create a table without mentioning 'ENGINE' attribute in a query, you are telling MySQL to go and use InnoDB to create the table. Well, there are many amazing/awesome/mind-forking storage engines that can be used instead of InnoDB . But, as InnoDB is the default, we should not hesitate to explore it. What is InnoDB?               InnoDB is the general-purpose storage engine that balances high reliability and high performance. Reliability is the fault tolerance quotient of the system. In MySQL 8.0 , InnoDB is the default MySQL storage engine, unless you configure it with other storage engines. What the hell InnoDB has? B-Tree indexes (u