Search This Blog

2023/04/20

Dependency Injection Design Pattern in Javascript

 Dependency Injection is a design pattern that helps manage the dependencies of

an object or a module by providing external dependencies to it. In other words,
instead of creating dependencies inside the object or module, the dependencies
are provided from the outside. This pattern promotes loose coupling between objects
or modules, making them more modular, testable, and maintainable.

Dependency Injection allows the programmer to define a set of dependencies required
by an object or module and provides those dependencies from the outside.
The dependencies can be provided via constructor injection, setter injection,
or method injection. Constructor injection is the most common form of Dependency Injection,
where the dependencies are provided via the constructor of the object or module.

By using Dependency Injection, the code becomes more flexible and adaptable to change.
It also makes the code more testable, as it is easier to isolate the dependencies and
mock them during testing. Additionally, Dependency Injection promotes code reuse and
modularity, as the dependencies can be shared across multiple objects or modules.


Connstructor Injection:

class EmailService {
sendEmail(to, subject, body) {
console.log(`Email sent to ${to}: ${subject} - ${body}`);
}
}

class UserService {
constructor(emailService) {
this.emailService = emailService;
}

createUser(name, email) {
console.log(`User created: ${name} - ${email}`);
this.emailService.sendEmail(email, 'Welcome to our app!', 'Thank you for joining our app!');
}
}

const emailService = new EmailService();
const userService = new UserService(emailService);

userService.createUser('John Doe', 'john.doe@example.com');

Using setter property injection
class EmailService {
sendEmail(to, subject, body) {
console.log(`Email sent to ${to}: ${subject} - ${body}`);
}
}

class UserService {
constructor() {
}

set EmailService(value) {
//check it is object of email service
if (value.constructor.name == "EmailService") {
this.emailService = value
}
}

createUser(name, email) {
console.log(`User created: ${name} - ${email}`);
this.emailService.sendEmail(email, 'Welcome to our app!', 'Thank you for joining our app!');
}
}

const emailService = new EmailService();
const userService = new UserService();
userService.EmailService = emailService

userService.createUser('John Doe', 'john.doe@example.com');


Function Based Dependency Injection

class EmailService {
sendEmail(to, subject, body) {
console.log(`Email sent to ${to}: ${subject} - ${body}`);
}
}

class UserService {
constructor() {
}

setEmailService(value) {
//check it is object of email service
if (value.constructor.name == "EmailService") {
this.emailService = value
}
}

createUser(name, email) {
console.log(`User created: ${name} - ${email}`);
this.emailService.sendEmail(email, 'Welcome to our app!', 'Thank you for joining our app!');
}
}

const emailService = new EmailService();
const userService = new UserService();
userService.setEmailService(emailService)

userService.createUser('John Doe', 'john.doe@example.com');

No comments:

Post a Comment