Text Analysis on the reviews data of Indian products in Amazon

The objective of the article is to explore and analyze the reviews dataset of Indian products on Amazon with different NLP methodologies such as NLTK and Spacy. Also touch upon the Sentiment analysis…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




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.

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:

The structure of the project is as follows:

Project structure

Inside of src folder, the following folders were created:

Inside of you cargo.toml file, include the following dependencies under the dependency section:

Waiter DI is the crate with the features we need in order to add the injection functionalities.

So far we have the Service that depends on the trait TUserRepo, which is the abstraction to the repository.

UserService, the struct that has TUserRepo as a property

As TUserRepo is a trait, it must be implemented by another struct. UserService doesn’t know this concrete implementation, as it’s the rule when we’re talking about the dependency inversion principle. That’s the way we decouple these modules in order to be able to inject test mock classes or other implementations when needed without affecting the UserService (and all the other services) code.

Note that UserService struct has the module attribute. That’s how the container, called by the new() function knows that it has to inject something in it. Note that only by passing the service struct name to the container, it will identify the dependencies that have to be injected. In this case, the implementation of TUserRepo.

The new() function acts as a constructor, returning the struct itself. By using the container as mentioned before, the struct will be returned with the correct dependencies.

The service is also calling an injection container.

If we have different profiles in the future, a centralized container would be an interesting idea, for example, to return different dependencies according to an environment variable indicating the type of execution (such as dev, test etc).

The UserRepo implementation is the following:

To test this implementation, let’s create an instance of UserService and call the save() function.

main.rs file
result of command execution

And the “user saved!” (version of “Hello Repository!”) was printed successfully, what means that our implementation was correctly injected and our service doesn’t even dream about this implementation.

The complete code is below:

That’s all, I hope you enjoy it. See you next time!

Add a comment

Related posts:

The Hottest Things You Can Do During Sex For Maximum Orgasms

Being intimate during sex is the golden rule for maximum fun. Knowing how to touch your partner, where to touch, when to touch, and why to touch them. Sure, we can come during basic sex, you can ram…

payday loan phone number

payday loan phone number. “payday loan phone number” is published by Usimooukourx.

The Art of Being a Human

The fragments of evolution alongside the divine creation, consist the incarnation of the human nature. There are many characteristics that can be considered as part of the human cosmos as the main…