Inversify is a lightweight inversion of control (IoC) container.
IoC container uses a class constructor to identify and inject its dependencies.
When you need another piece of code to run the current code.
A
relies onB
to work
class A
uses methods from class B
= class A
is dependent of class B
class A needs to have instance or copy of B inside of A to work
What is Common dependency
User
model is MySQL
persist
method is called to store data into MySQLUser
model needs Database
's persist
method.
// before inversion public class User { MySqlDatabase database; public User() { database = new MySqlDatabase(); } public void() add(String data) { database.persist(data); } }
IoC
)IoC is flipping of dependency.
Instead of defining the type of database
used in the User class
directly as concrete implementation, you instantiate the User
class and pass a Database
type to it.
// previously user = container.new User(); public class User { database = MySqlDatabase(); } // dependency inversion user = container.new User(MySql()); public class User { this.database = database }
Allows more abstraction over concrete implementation
Benefit - Not solidifying concrete implementation choice now which will become harder to rip out or change when the codebase grows large and requirements change
Injection is when you pass something into the code instead of reference/using it directly.
Passing the concrete decision as a parameter
Remove direct dependencies between components by injecting their dependencies from external sources
// Using Constructor class UserService { constructor(database) { this.database = database; } // ... } const database = new Database(); const userService = new UserService(database);
IoC
use interfaces
?The concrete implementations of the interfaces are not meant to be exported from the projects, which means they are not available to clients, and you have to use the interface.
This decouples our function implementations from the interfaces, and allows us to maintain the functions and different implementations without affecting the interface footprint in the rest of the system.
Callsites become a million times easier to maintain, and teams can refactor their internal project code without worrying about where and how the concrete function implementations are defined.
Illustration skipped as it is not a recommended pattern.