Dependency Injection in Rust using Waiter DI

Dependency Injection in Rust using Waiter DI

Dependency injection is a technique which is largely used to avoid tight coupling inside the applications, by abstracting objects/modules and inverting the dependency flow. Of course this is a simplified explanation of it but, if you are here, I believe that we are familiarized with the technique and you might have googled “dependency injection in rust”, or something like that, in order to know specifically how to use it in Rust. Dependency Injection in Rust using Waiter DI

Dependency injection is a technique which is largely used to avoid tight coupling inside the applications, by abstracting objects/modules and inverting the dependency flow.

Of course this is a simplified explanation of it but, if you are here, I believe that we are familiarized with the technique and you might have googled “dependency injection in rust”, or something like that, in order to know specifically how to use it in Rust.

Otherwise, check this link out. The explanation about the topic is way more instructive =).

In this article I want to show how to use dependency injection using Waiter DI, a very useful DI crate for Rust.

Assuming that you have already installed Rust in your computer, select your workspace folder and run the following command:

cargo new di_poc --bin

A di_poc folder will be created. Open this folder inside of your preferred editor. I’m using VS Code.

The structure of the project is as follows:

Image for post

Project structure

Inside of src folder, the following folders were created:

  • abst: where I put the trait (abstract code).
  • implementation: where I put the implementation, the struct which depends on the abstract.
  • services: the struct where the implementation for the trait will be injected

dependency-injection design-patterns rust

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Strategy Design Pattern in Rust

One of the means to achieve decoupling software modules is following Dependency Inversion Principle (DIP). In this post I want to show how to implement the Strategy Design Pattern in order to achieve Dependency Inversion.

Design Patterns — Decorator Design Pattern

The decorator design pattern is a software design pattern that helps you add responsibilities to an object at runtime. This pattern is considered to be a structural design pattern. This type of pattern focuses on the relationships between classes and how they can help solve specific problems.

Design Patterns for Beginners with real-world Examples

Design Patterns for Beginners with real-world Examples. When do you use Design Patterns? How do you implement different Design Patterns in Java? What are Design Patterns? Why do you use Design Patterns? What are the different types of Design Patterns? When do you use Design Patterns? What are the real-world examples for Design Patterns.

Patterns: The Stories of our Design Systems

Design Patterns are some of the most over-used concepts in design today. And we all know what happens when you have some ideas all over…

Design Patterns: Exploring Factory Method in Modern C++

In software engineering, Creational Design Patterns deal with object creation mechanisms, i.e. try to create objects in a manner suitable to the situation. In addition to this basic or ordinary form of object creation could result in design problems or added complexity to the design.