Exploring the world of SPA: Implementing Data Transfer Objects

Considering implementing a Single Page Application (SPA) using a JavaScript framework like Angular JS. Currently, I have multiple existing Web APIs containing the necessary information for the app. I need to add another API that will handle new data and reference other DTOs. My question is, would it be better to make separate calls to each API and compile the DTO on the web app, or should I create a new API that merges all the data and provides a GET method with the complete object?

Approach 1 from the perspective of the website:

API Calls (Assuming userId = 1 for the logged-in user)

/get/order/{userId}
{
   UserId: 1
   Products:{1,2}
}

Based on the above call:

/get/user/1
{
   FirstName: test,
   LastName: test
}

/get/product/1
{
   Price: 10,
   Name: Test
}

/get/product/2
{
   Price: 100,
   Name: Test2
}

Data merging in Angular JS Website

Approach 2 from the website's perspective:

Merge all data within the Order Web API

API Call:

/get/ordermerged/{userId}
{
   FirstName: test,
   LastName: test
   Products:
   {
      {
         Price: 10,
         Name: Test
      },
      {
         Price: 100,
         Name: Test2
      }
   }
}

I have attached a screenshot for better visualization.

Answer №1

When deciding between reusability and specialization in coding, there are pros and cons to consider. Reusability can save time and effort by reducing the amount of work needed. Specialization allows for easier consumption of the API on the front end. Additionally, specializing can eliminate unnecessary data loading that may occur with separate API endpoints.

There is no one-size-fits-all answer to whether reusability or specialization is better; it depends on the specific situation at hand. It's wise to evaluate each case individually and choose the approach that makes the most sense. Some endpoints may benefit from a specialized version that consolidates all necessary information into one call, while others may be more suited for reuse with additional client-side processing.

In terms of performance, using multiple smaller API endpoints may have a slight advantage due to the efficiency of the HTTP/2 protocol. However, the choice between a single API call versus multiple calls ultimately depends on the context.

When considering Angular specifically, the debate between reusability and specialization becomes less relevant. Angular is agnostic to this decision and will function regardless of the chosen approach.

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

Ensuring that the $broadcast listener in Angular is properly loaded when a message is broadcast

In my Angular application, I have implemented two controllers. One controller triggers a $broadcast event when a specific data point is updated: $scope.updateTableInfo = function (table) { $rootScope.$broadcast ("updateTableInfo", table ...

Guide to displaying options in the Vue material select box even when the default value is blank

I have implemented the material design framework vuematerial with vue.js. In traditional HTML, a selection box looks like this: <select> <option value="">You should initially see this</option> <option value="1">One</o ...

Using ng-repeat in conjunction with ui-router conditions

Utilizing ui router ($routeprovider and $locationprovider) for angular js, I am looking to adjust the array being looped through in an ng-repeat based on the current url/route. Despite browsing similar threads on stack, I have not found a precise solution ...

Access user connections through Discord.js bot

Hi there, I'm currently working on creating a bot that can retrieve a user's connected battle.net account and display their game rank. I am utilizing the discord.js library and have been attempting to access the UserProfile through the bot. Unfor ...

Working with HTML5 Canvas to Clip Images

Is there a way to implement a tileset image in canvas like this one? I am trying to figure out how to make it so that clicking on the first tile returns 0, clicking on the tenth tile returns 9, and so on... Any suggestions on how to clip a tileset on an ...

Is there a way to determine if a browser's network activity is inactive?

Within an angularJS application, a noticeable delay can be experienced between the user and the server (potentially due to limited bandwidth), resulting in a wait time of approximately 2-500ms when loading a new page. I am considering implementing a metho ...

The module "jquery" in jspm, jQuery, TypeScript does not have a default export

Having some trouble setting up a web app with TypeScript and jspm & system.js for module loading. Progress is slow. After installing jspm and adding jQuery: jspm install jquery And the initial setup: <script src="jspm_packages/system.js"></scri ...

React component utilizes Material UI to detect scrolling within a table

Currently, my table's body size is set to fixed dimensions in accordance with Material UI guidelines. I am looking to implement a functionality that allows me to dynamically load more rows as the user scrolls through the table. Can you recommend the ...

Sorting of array in AngularJS ngRepeat may result in changing the length of the array

Link to JSFiddle: http://jsfiddle.net/WdVDh/101/. When updating the sorting option for an ng-repeat list, I noticed that the array length changes and in my local version, all items disappear. My code involves filtering and sorting, with the ability to fi ...

Attempting to comprehend the reason behind the presence of additional parentheses at the conclusion of a function invocation

Just a quick question I have while experimenting with an example involving OAuth. I want to make sure I fully understand what's going on before incorporating it into my own code. The example uses node, express, and passport-azure-ad. In the code sni ...

Vue not triggering computed property sets

As per the guidelines, I should be able to utilize computed properties as v-model in Vue by defining get/set methods. However, in my scenario, it isn't functioning as expected: export default{ template: ` <form class="add-upload&quo ...

The fullscreen API allows for the creation of a full-screen element containing internal elements, while also enabling the functionality

Is it possible to utilize the fullscreen API to make any element fullscreen, even if it contains multiple internal elements, while still maintaining the functionality of dropdowns and other custom elements that may be located in different areas of the page ...

Executing an automated process of adding items to the shopping cart using Python without the need to have a

Does anyone know of a way to automate the add-to-cart process using Python without the need for an open browser window? I've experimented with modules like mechanize, but they lack the ability to directly interact with web elements. Currently, I&apo ...

Some suggestions for updating two div elements using only one Ajax response

My website features a signin() button that I want to enhance with ajax functionality. When the button is clicked, I need it to update two divs: one displaying a personalized welcome message, and the other showcasing a statistics table in a different locati ...

How do I programmatically switch the Material-UI/DatePicker to year view in React?

I have a DatePicker component set up in my project, and it works perfectly fine. However, for my specific use case, I only need the year view to be displayed. Within the child component of the DatePicker (Calendar), there is a function called: yearSelect ...

Select the correct nested div with the same name by clicking on it

My problem involves nested div elements with the same class. For example, I have a Panel inside another Panel. However, when I click on the inner panel, it is actually the outer panel that triggers the $(".panel").click function. What I need is for the ...

Problem with uploading a CSV file in Angular JS

I am encountering an issue while trying to export data into Excel. When I click on the export button, the Excel file opens up empty. Can someone please assist me with this problem? Although I can view the data {a: 1, b:2}, {a:3, b:4} in HTML using knowled ...

Exploring the Differences Between Arrays in JavaScript

I am currently tackling the task of comparing arrays in JavaScript, specifically within node.js. Here are the two arrays I am working with: Array 1: [16,31,34,22,64,57,24,74,7,39,72,6,42,41,40,30,10,55,23,32,11,37,4,3,2,52,1,17,50,56,60,65,48,43,58,28,3 ...

Preventing clicks within an iframe

Within an iframe, I have HTML content that includes hyperlinks. I need to prevent clicks on these hyperlinks. I managed to accomplish this using jQuery as shown below. However, I prefer not to use jQuery for this task. How can I achieve the same result ...

Determining the installation duration of the React Native screen

Several questions have been asked about this topic, but none of them seem to have a definitive answer. What I am looking to do is calculate the time it takes to navigate to a screen. The timer will start here: navigation.navigate("SomePage") Essentially, ...