Inquiry regarding the implementation of DTO within a service layer parameter

I have a query regarding the choice of service layer to use.

// 1
export class SomeService{
  async create(dto:CreateSomeDto) {}
}

or

// 2 
export class SomeService{
  async create(title: string, content: string) {}
}

It appears that most individuals opt for option 1.

Nevertheless, I came across an article suggesting that this approach leads to increased interdependence between the controller and the service layer.

The argument is that the controller layer should rely on the service layer in a unidirectional manner.

There may not be a definitive answer, but could you help elucidate the advantages and disadvantages of each approach?

Answer №1

Using method 1 may save a small amount of time by not having to redefine DTO type definitions for service methods, but ultimately it is not worth the trade-off. It is recommended to use method 2 instead.

As your application grows, there may be instances where service methods are called from other services or controllers. This can lead to situations where callers of a service must provide unnecessary extra parameters in order to call the method.

For example, consider a scenario where you have a UsersService and UsersController for handling user registration:

class UsersController() {
  @Post('register')
  register(@Body() dto: RegisterUserDto) {
    const isVerified = this.verificationService.verify(dto.code);
    if (isVerified) {
      return this.usersService.create(dto);
    }
  }
}

class UsersService() {
  create(dto: RegisterUserDto) {
    // create user... 
  }
}

Everything works fine until a few months later when a client requests the ability to manually add users from their dashboard. Even though you already have an implemented create method in UsersService, you cannot directly use it in

AdminController</code because the method requires a <code>code
parameter. The compiler will not allow you to call the method without providing the required parameter, even if it is not actually used in the create method.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The name 'console' could not be located

I am currently working with Angular2-Meteor and TypeScript within the Meteor framework version 1.3.2.4. When I utilize console.log('test'); on the server side, it functions as expected. However, I encountered a warning in my terminal: Cannot ...

Facing Syntax Errors When Running Ng Serve with Ngrx

Currently, I am enrolled in an Angular course to gain proficiency in ngrx. In a couple of months, I will be responsible for teaching it, so I am refreshing my memory on the concept. Strangely, even after installing it and ensuring my code is error-free, er ...

What is the most effective method for obtaining the ViewContainerRef of a mat-row in Angular 4

I am currently working with a mat-table and I'm looking to retrieve the ViewContainerRef of a clicked row in order to add another component within that specific row. Can anyone suggest the most effective method to obtain the ViewContainerRef of a row? ...

Can an ejs template be displayed without the need to refresh the page?

I'm currently developing a game server that involves players entering a game room and getting paired with another player. One feature I would like to implement is displaying the game board without having to reload the page in order to avoid reinitiali ...

Continuously apply the template in a recursive manner in Angular 2 without reintroducing any duplicated components

Recently, I delved into the world of angular 2 and found it to be quite fascinating. However, I'm currently facing a roadblock and could really use some assistance. The scenario is as follows: I am working on creating a select box with checkboxes in ...

Error message: Issue with AWS Serverless Lambda and Angular - TypeError: express function not defined

I'm encountering an issue when trying to deploy my application from localhost:4200 to AWS serverless Lambda. The error in cloudwatch logs is causing a 500 {"message": "Internal server error"} response when I access the URL. My understanding of AWS is ...

Error message: "Unable to find a windows instance" encountered while conducting tests on Paho MQTT Client using mocha and typescript

After spending countless days searching online, I have yet to find any resources on testing the Paho MQTT Client. My approach so far has been somewhat naive, as shown below: import { suite, test, slow, timeout, skip, only } from 'mocha-typescript&apo ...

Issue: The variable 'HelloWorld' has been declared but not utilized

Why am I encountering an error when using TypeScript, Composition API, and Pug templating together in Vue 3? How do I resolve this issue when importing a component with the Composition API and using it in a Pug template? ...

Express Node Router with Targeted IP Address

I've run into an issue with nodejs express lately. It's functioning properly, but I have a specific idea that I'm not certain is achievable. Currently, I am hosting an express app on my localhost while also running hostapd on the local serv ...

Is it possible to place Angular Material components using code?

Currently, I'm in the process of creating a responsive Angular application. Is there any way to adjust the height and position of the <mat-sidenav-content></mat-sidenav-content> component in Angular Material programmatically without relyi ...

A guide on embedding an HTML file into a .ejs file

I need to implement a default header in a .ejs file. After seeking help from ChatGPT, I wasn't able to obtain the correct code. Attached is the index.ejs file that was sent via email: <!DOCTYPE html> <html> <head> <title&g ...

Creating Multiple Result Lists in MUI Autocomplete Based on Option Properties: A Step-by-Step Guide

I have a query about displaying results using the MUI Autocomplete component. I am looking to categorize the results into two separate lists placed side by side. Currently, I have a working searchbar with one list of results. However, I aim to segregate t ...

Issue with dynamically typed object properties in Typescript codebases

Check out this TypeScript code snippet: export type Mutation = (state: State, data: unknown) => void export type Mutations = { [key: string]: Mutation } private buildMutations(): Mutations { return { ['3']: (state, data) => ...

How can you retrieve user data using a hook in FeatherJS?

Recently, I started exploring FeatherJS and successfully registered a new user, obtained a token, and accessed protected data by including Authorization in the header. It is important to note that I am exclusively using HTTP Rest API. The documentation fr ...

Encountering "Undefined error" while using @ViewChildren in Angular Typescript

In my application, I am facing an issue with a mat-table that displays data related to Groups. The table fetches more than 50 rows of group information from a data source, but initially only loads the first 14 rows until the user starts scrolling down. Alo ...

How do I use TypeScript and protractor to retrieve the column index of a grid by matching the header text of that column?

I have been attempting to create a function that can determine the column index of a grid based on the header text for that particular column. Despite several attempts, as shown in the comments below, the function consistently returns -1 instead of the exp ...

Understanding the attribute types in Typescript React

While working on code using Typescript + React, I encountered an error. Whenever I try to set type/value in the attribute of <a> tag, I receive a compile error. <a value='Hello' type='button'>Search</a> This piece o ...

What is the best way to incorporate HTML code into a view?

Currently utilizing express.js along with EJS as the chosen template engine. I'm running into confusion when attempting to integrate partials into my project. The examples I've come across use the JADE template engine, which has left me uncertain ...

When integrating the @azure/msal-angular import into the Angular application, the screen unexpectedly goes blank,

Starting a new Angular app and everything is rendering as expected at localhost:4200 until the following change is made: @NgModule({ declarations: [ AppComponent, HeaderBannerComponent, MainContentComponent, FooterContentinfoComponent ...

Is a package.json file missing dependencies?

Curious about the meaning of peerDependencies, I examined the contents of this package.json file. It relates to a library project that is distributed elsewhere. { "name": "...", "version": "...", "description": "...", "author": "...", ...