An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
Dependency Inversion Principle in TypeScript
Depend upon abstractions, not concretions.
In our applications we can differentiate between two types of classes:
- Low level classes which do operations like reading from a database or saving a file.
- High level classes which implement the business logic and use those low level classes.
What this principle proposes is that high level classes depend on interfaces instead of concrete implementations. This is easier to understand with an example.
In the following bad example we have the OrderService
class that saves orders in a database. The OrderService
class depends directly on the low level class MySQLDatabase
.
If in the future we wanted to change the database that we are using we would have to modify the OrderService
class.
class OrderService { database: MySQLDatabase; // constructor save(order: Order): void { if (order.id === undefined) { this.database.insert(order); } else { this.database.update(order); } }}class MySQLDatabase { insert(order: Order) { // insert } update(order: Order) { // update }}
We can improve this by creating an interface and make the OrderService
class dependant of it. This way, we are inverting the dependency. Now the high level class depends on an abstraction instead of a low level class.
class OrderService { database: Database; // constructor save(order: Order): void { this.database.save(order); }}interface Database { save(order: Order): void;}class MySQLDatabase implements Database { save(order: Order) { if (order.id === undefined) { // insert } else { // update } }}
Now we can add new databases without modifying the OrderService
class.
Original Link: https://dev.to/jmalvarez/dependency-inversion-principle-in-typescript-4nm0
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To