Posts

Showing posts with the label Typescript

Node.js Microservice with NestJS: Part 3 - NestJS Dependency Injection

An Introduction to Dependency Injection in NestJS NestJS is a powerful framework for building efficient, reliable, and scalable server-side applications. One of the core concepts in NestJS is Dependency Injection (DI) , which is built on the Inversion of Control (IoC) design pattern. In this third installment on building a microservice using  NestJS , we'll explore what dependency injection is, how it's implemented in NestJS , and why it's crucial for building maintainable applications. We'll also provide practical examples  of how we use it in our tour-service application to illustrate these concepts and break down important topics like decorators, constructors, modules, application context, and services. For more details about the service we are building and what we have done so far, check out the following posts: Node.js Microservice with NestJS: Part 1 - Service Overview  - An overview of the microservice we will be building Node.js Microservice with NestJS: Part ...

Constructor-Based vs Property-Based Dependency Injection in NestJS - Which is Better?

Image
Constructor-Based vs Property-Based Dependency Injection in NestJS - Which is Better? Using constructor-based injection is generally considered better than property-based injection in NestJS (and in many other frameworks) for several key reasons.  Lets look at a few reasons why and some scenarios where  property-based injection  may be more appropriate. Immutability and Readability Constructor-based injection ensures that dependencies are provided at the time of object creation, making them immutable . This means that the dependencies cannot be reassigned after the object is instantiated. With property-based injection , dependencies are injected later, making them mutable. This can lead to side effects if the dependencies are modified during the object's lifetime. Constructor-based injection makes it clear what dependencies the class needs at the time of instantiation, improving code readability. Example: private readonly landmarkService : LandmarkService ; constructo...

Node.js Microservice with NestJS: Part 2 - Using the NestJS CLI

Image
Introducing the Nest CLI The NestJS framework provides a Command Line Interface (CLI) that can be used to generate NestJS projects and components of various types, such as services and controllers. It is build on top of the angular schematics ( @schematics/angular ) code generation framework.  In this second installment on building a NestJS microservice, we will use the Nest CLI to bootstrap our tour-service microservice into the autotoor-app repo. For more details about the service we are building and what we have done so far, check out the following posts: An Overview of the App  - An overiew of the entire application we will be building, both frontend and backend. Node.js Microservice with NestJS: Part 1 - Service Overview  - An overview of the NestJS microservice we will be building. Installing the CLI Since our demo project is using pnpm we will use it to install the Nest CLI as follows: pnpm add -g @nestjs/cli Instructions for installing the Nest CLI in other ways c...