Leveraging Dependency Injection for Clean and SOLID Code in Node.js

Arvind - Apr 15 - - Dev Community

In the realm of software development, maintaining clean and organized code is paramount for building scalable and maintainable applications. One powerful technique that facilitates this endeavor while adhering to the SOLID principles is Dependency Injection (DI). In this article, we'll explore how Dependency Injection fosters clean code and supports SOLID principles, using Node.js as our platform.

Introduction to Dependency Injection

Dependency Injection is akin to having a central repository for all the dependencies your code needs, analogous to a centralized storage area for construction tools in a building project. Traditionally, objects manage their dependencies internally, but with Dependency Injection, these dependencies are provided from the outside, typically through constructor injection, setter injection, or method injection.

// Without Dependency Injection
class DatabaseService {
  constructor() {
    this.connection = new DatabaseConnection(); // Creating dependency internally
  }
  // Other methods using this.connection
}

// With Dependency Injection
class DatabaseService {
  constructor(connection) {
    this.connection = connection; // Dependency provided from the outside
  }
  // Other methods using this.connection
}
Enter fullscreen mode Exit fullscreen mode

Benefits of Dependency Injection

  1. Improved Testability: By decoupling dependencies, unit testing becomes more straightforward. With Dependency Injection, you can easily substitute real dependencies with mock objects, enabling isolated testing of individual components.
// Without Dependency Injection
class UserService {
  constructor() {
    this.database = new DatabaseService(); // Creating dependency internally
  }
  getUser(id) {
    return this.database.getUser(id); // Difficult to test without database
  }
}

// With Dependency Injection
class UserService {
  constructor(database) {
    this.database = database; // Dependency provided from the outside
  }
  getUser(id) {
    return this.database.getUser(id); // Easily testable with mock database
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Enhanced Reusability: Dependency Injection promotes code reusability by making components more modular. Since dependencies are provided externally, they can be reused across different parts of the application, reducing duplication and promoting consistency.
// Dependency Injection allows reusability
class Logger {
  log(message) {
    console.log(message);
  }
}

class EmailService {
  constructor(logger) {
    this.logger = logger;
  }
  sendEmail() {
    this.logger.log('Email sent!');
  }
}

class PaymentService {
  constructor(logger) {
    this.logger = logger;
  }
  processPayment() {
    this.logger.log('Payment processed!');
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Simplified Maintenance: When dependencies are injected externally, changes to those dependencies don't require modifications to the consuming classes. This simplifies maintenance and reduces the risk of unintended side effects.
// Without Dependency Injection
class ProductService {
  constructor() {
    this.database = new DatabaseService(); // Creating dependency internally
  }
  updateProduct(id, newInfo) {
    // Some logic
    this.database.updateProduct(id, newInfo);
    // Some more logic
  }
}

// With Dependency Injection
class ProductService {
  constructor(database) {
    this.database = database; // Dependency provided from the outside
  }
  updateProduct(id, newInfo) {
    // Some logic
    this.database.updateProduct(id, newInfo);
    // Some more logic
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Facilitates SOLID Principles

    a. Single Responsibility Principle (SRP): With Dependency Injection, classes focus on their primary responsibilities without being burdened with the creation and management of dependencies, thus adhering to the SRP.

    b. Open/Closed Principle (OCP): Dependency Injection promotes the open/closed principle by allowing new functionality to be added through the addition of new components rather than modifying existing ones.

    c. Liskov Substitution Principle (LSP): Since Dependency Injection relies on interfaces, it enables adherence to the LSP by allowing objects to be replaced with instances of their subtypes without affecting the correctness of the program.

    d. Interface Segregation Principle (ISP): Dependency Injection encourages the use of interfaces, facilitating the segregation of interfaces based on client-specific needs.

    e. Dependency Inversion Principle (DIP): Dependency Injection inherently follows the Dependency Inversion Principle by decoupling high-level modules from low-level modules, allowing for easier substitution and flexibility.

Example: Dynamic Selection of Authentication Service

Let's delve into a non-trivial example where we build an authentication service for an e-commerce platform in Node.js. The service provides authentication functionality to both a mobile app and a public API endpoint. We'll use an if-else statement to decide which service to use based on a request header, while both services consume the same internal implementation for the core business logic through Dependency Injection.

// Core Authentication Service
class AuthService {
  constructor(userRepository) {
    this.userRepository = userRepository;
  }

  authenticate(username, password) {
    const user = this.userRepository.findByUsername(username);

    if (user && user.password === password) {
      return { success: true, message: 'Authentication successful', user };
    } else {
      return { success: false, message: 'Authentication failed' };
    }
  }
}

// UserRepository Interface
class UserRepository {
  findByUsername(username) {
    // Implementation to be provided by concrete classes
  }
}

// DatabaseUserRepository implements UserRepository
class DatabaseUserRepository extends UserRepository {
  findByUsername(username) {
    // Simulate fetching user from database
    return { username, password: 'hashedPassword' }; // Actual database retrieval would involve hashing and more
  }
}

// Mobile App Service
class MobileAppService {
  constructor(authService) {
    this.authService = authService;
  }

  handleAuthentication(username, password) {
    return this.authService.authenticate(username, password);
  }
}

// Public API Endpoint Service
class PublicAPIService {
  constructor(authService) {
    this.authService = authService;
  }

  handleAuthentication(username, password) {
    return this.authService.authenticate(username, password);
  }
}

// Main Authentication Handler
class AuthHandler {
  constructor(mobileAppService, publicAPIService) {
    this.mobileAppService = mobileAppService;
    this.publicAPIService = publicAPIService;
  }

  handleRequest(req) {
    const isMobileApp = req.headers['user-agent'].includes('MobileApp');

    if (isMobileApp) {
      return this.mobileAppService.handleAuthentication(req.body.username, req.body.password);
    } else {
      return this.publicAPIService.handleAuthentication(req.body.username, req.body.password);
    }
  }
}

// Setting up dependencies
const userRepository = new DatabaseUserRepository();
const authService = new AuthService(userRepository);
const mobileAppService = new MobileAppService(authService);
const publicAPIService = new PublicAPIService(authService);
const authHandler = new AuthHandler(mobileAppService, publicAPIService);

// Mock request objects
const mobileAppRequest = {
  headers: {
    'user-agent': 'MobileApp'
  },
  body: {
    username: 'johnDoe',
    password: 'password123'
  }
};

const publicAPIRequest = {
  headers: {
    'user-agent': 'Postman'
  },
  body: {
    username: 'janeDoe',
    password: 'password456'
  }
};

// Handle Mobile App authentication
const mobileAuthResult = authHandler.handleRequest(mobileAppRequest);
console.log('Mobile App Authentication:', mobileAuthResult);

// Handle Public API Endpoint authentication
const publicAPIAuthResult = authHandler.handleRequest(publicAPIRequest);
console.log('Public API Endpoint Authentication:', publicAPIAuthResult);
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this comprehensive guide, we explored how Dependency Injection empowers developers to write clean and SOLID code in Node.js applications. Through detailed code examples and explanations, we've demonstrated the practical benefits of Dependency Injection and its alignment with best practices in software design.

By embracing Dependency Injection, developers can build robust, scalable, and maintainable software systems that stand the test of time. The dynamic selection of authentication services example showcased the flexibility and adaptability of Dependency Injection, enabling us to handle different sources of requests while reusing the same core business logic.

Embrace Dependency Injection to unlock the full potential of your applications and pave the way for scalable and maintainable codebases.

. . . . . .
Terabox Video Player