Table of Contents
Item 5: Ruby Best Practices - Prefer dependency injection to hardwiring resources
Introduction to Dependency Injection in [[Ruby]]
Dependency injection is a design pattern that encourages the decoupling of components by injecting dependencies into a class or method rather than hardwiring them. In Ruby, adopting dependency injection can lead to more flexible, testable, and maintainable code. By preferring dependency injection over hardwiring resources, you can create software that is easier to modify, extend, and test, especially in complex applications.
Why Prefer Dependency Injection to Hardwiring Resources in [[Ruby]]?
Preferring dependency injection in Ruby offers several significant advantages: 1. **Improved Testability**: By injecting dependencies, you can easily substitute them with mocks or stubs during testing, which makes unit tests more isolated and focused. 2. **Flexibility**: Dependency injection allows you to swap out different implementations without modifying the core logic, providing greater flexibility and adaptability to changing requirements. 3. **Decoupling**: It reduces the coupling between components, resulting in more modular code that is easier to maintain and extend.
Example 1: Hardwiring Resources vs. Dependency Injection
- Hardwiring Resources (Anti-Pattern)
```ruby class ReportGenerator
def generate data = CSV.read("data.csv") # Hardwired dependency data.summary endend
- Usage
generator = ReportGenerator.new generator.generate ```
In this example, the `ReportGenerator` class is tightly coupled with the `CSV` library and the “data.csv” file, making it difficult to test or adapt to different data sources.
- Dependency Injection
```ruby class ReportGenerator
def initialize(data_loader) @data_loader = data_loader # Injected dependency end
def generate data = @data_loader.load_data data.summary endend
- Usage with a CSV loader
class CsvLoader
def load_data CSV.read("data.csv") endend
generator = ReportGenerator.new(CsvLoader.new) generator.generate
- Usage with a mock data loader for testing
class MockLoader
def load_data [["column1", "column2"], [1, 2], [3, 4]] endend
test_generator = ReportGenerator.new(MockLoader.new) test_generator.generate ```
In this example, the `ReportGenerator` class is more flexible because the data-loading dependency is injected. This makes the class adaptable to different data sources and more easily testable.
Example 2: Injecting Dependencies into Methods
- Hardwiring Dependencies in Methods
```ruby class ReportPrinter
def print_report report = generate_report # Hardwired dependency puts report end
private
def generate_report "This is a report." endend
- Usage
printer = ReportPrinter.new printer.print_report ```
In this example, the `ReportPrinter` class is tightly coupled to the `generate_report` method, limiting its flexibility and testability.
- Dependency Injection in Methods
```ruby class ReportPrinter
def print_report(report_generator) report = report_generator.call # Injected dependency puts report endend
- Usage with a lambda
report_generator = → { “This is a report.” } printer = ReportPrinter.new printer.print_report(report_generator)
- Usage with a different report generator for testing
test_generator = → { “This is a test report.” } printer.print_report(test_generator) ```
In this example, the `print_report` method is more flexible because it accepts a report generator as an argument, making the method adaptable to different report generation strategies and easier to test.
Example 3: Dependency Injection in Rails Applications
In Ruby on Rails, dependency injection can be used to inject services, repositories, or other dependencies into controllers, models, or services.
- Hardwiring Dependencies in a Rails Controller
```ruby class OrdersController < ApplicationController
def create order = Order.new(order_params) payment_service = PaymentService.new # Hardwired dependency if payment_service.process(order) redirect_to order else render :new end endend ```
In this example, the `OrdersController` is tightly coupled to the `PaymentService`, making it difficult to test and maintain.
- Dependency Injection in a Rails Controller
```ruby class OrdersController < ApplicationController
def initialize(payment_service = PaymentService.new) @payment_service = payment_service # Injected dependency super() end
def create order = Order.new(order_params) if @payment_service.process(order) redirect_to order else render :new end endend
- Usage in Rails (with automatic injection in a test)
class MockPaymentService
def process(order) true endend
- In test cases
controller = OrdersController.new(MockPaymentService.new) controller.create ```
In this example, the `OrdersController` accepts the `PaymentService` as a dependency, making it easier to test the controller with different payment service implementations.
When to Prefer Dependency Injection in [[Ruby]]
Dependency injection should be preferred in the following scenarios: - **Testing**: When you want to create unit tests that are independent of external resources, dependency injection allows you to inject mocks or stubs. - **Flexibility**: When your code needs to work with different implementations of a dependency (e.g., different data sources or service objects), dependency injection makes it easier to switch between them. - **Decoupling**: When you aim to reduce the coupling between different parts of your code, dependency injection helps to create a more modular and maintainable codebase.
Conclusion
In Ruby, preferring dependency injection over hardwiring resources leads to more flexible, testable, and maintainable code. By injecting dependencies rather than hardcoding them, you can decouple components, improve testability, and adapt your code to different contexts with ease. This approach aligns with best practices in modern software development, where flexibility and maintainability are key considerations.