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

Transform input string containing newline characters into separate paragraphs

I utilize Contentful CMS for content management and fetch the content through their API. When the content is fetched, it comes in as a JSON object. One of the keys within this object pertains to the main text block for the entry I am retrieving. This stri ...

Different placeholder text rendered in two text styles

Can an input box placeholder have two different styles? Here's the design I have in mind: https://i.stack.imgur.com/7OH9A.png ...

Unable to successfully link filter outcomes with input in AngularJS

Here is the code snippet I am working with: <body ng-app=""> <div ng-init="friends = [{name:'John', phone:'555-1276'}, {name:'Mary', phone:'800-BIG-MARY'}, {nam ...

Unable to establish a local dependency link with npm

Attempting to connect my local project testabc123 to myproject using the usual method: cd testabc123 npm link cd ../myproject npm link testabc123 However, encountering an error message: npm ERR! code E404 npm ERR! 404 Not Found - GET http://registry.npmjs ...

Filling a HTML div with data through PageMethods and AJAX

My issue is quite unique. Despite checking numerous examples before reaching out here, I am facing a peculiar problem. I have a div element in my ASPX file and I am using AJAX to send a POST request to populate it. <script> function send(input ...

Guide to creating an asynchronous function

Starting my first react website and needing assistance with writing an asynchronous JavaScript function. I am currently working on uploading user input files to firebase storage and then making a post request to the API in order to store the data in the da ...

Is it better to have Angular and Laravel as separate applications or should Laravel serve the Angular app?

I have a new project in the works, aiming to create a large SAAS application. Although I come from a CakePHP background, I've decided to use Laravel and Angular for this venture. As I navigate through unfamiliar territory with these technologies, I a ...

Issue with JavaScript ScrollTop

Simply put, I am trying to determine the total scroll size for a text area in a unit that scrollTop can align with. However, I am at a loss on how to do so. I've tried scrollHeight and various other methods without success. Any advice or suggestions ...

Enhance the speed of webview on Android devices

I have been working on an application that utilizes a webview to load both HTML and JavaScript files. Unfortunately, I have noticed a significant decrease in performance when displaying the content. Despite trying various solutions such as: webVi ...

Tips for composing a hyperlink within a property

I'm a newbie to Vue.js and facing a challenge with assigning a property to a link. I'm unsure of how to write the "counter" variable from "data" in order for it to properly function as intended. export default { name: 'app', data ( ...

Activate dark mode automatically in material-ui

According to the official documentation: The documentation mentions that a dark mode theme will be automatically generated and reflected in the UI, but I am encountering issues with it. Dependencies: "@emotion/styled": "^11.0.0", ...

Refresh the HTML content within a specified div element

In my index.html, there is a graph (created using d3.js) along with some code that displays a stepper with a number of steps equal to the child nodes of the clicked node: <div ng-include="ctrl.numlab==2 && 'views/stepper-two-labs.htm ...

Is there a way to use jQuery to adjust the text color once it has been clicked on?

I have been struggling to change the text color upon clicking on it without any success. Within my code, there are seven labels - one for the question, four for the answer options, one for the correct answer, and the last one for the explanation. The desi ...

Steps for converting Json to object in C# for Windows phone 8

Is there a more efficient way to solve my issue? Here is the JSON data I have: [ { "name":{ "IsEmpty":false, "X":-10.5, "Y":2.47 }, "password":"pas" }, { "name":{ "IsEmpty":fa ...

The issue I am facing is that when I click on a checkbox, only one of them seems to respond

When I click the button, only the first checkbox event is being checked while the rest of them are not. Can someone please provide some guidance on how to fix this issue? $("#cascadeChange").click(function() { //alert("Clicked"); ...

Tips for keeping JavaScript created checkboxes saved and accessible

Utilizing the "ajax" function within the script enables communication with the server by sending post or delete messages. The javascript code that includes ajax is responsible for dynamically adding checkboxes to the page. How can we ensure that the create ...

Incorporate an attachment from a test run while generating a Bug ticket within Azure DevOps

My goal is to automatically generate a Bug ticket in DevOps when a test run fails, including a screenshot in the attachments. However, when I try to create the Bug ticket, only error messages, stack trace, and other information appear in the Repro Steps se ...

Is it possible to track traffic using Alexa or SimilarWeb on a single-page application?

We are currently grappling with the challenge of how to effectively track traffic and user engagement within our classified sites on a single-page application built in angularJS. While we have successfully managed SEO and tracking with Google Analytics, we ...

The jQuery message validator is currently experiencing some issues with its functionality

I am working on implementing a basic login system using jQuery and it appears to be functioning correctly, but I keep getting an error message in my jQuery code here: alert('Error in system');. What's puzzling is that when I use alert(answe ...

Using AngularJS ng-repeat to loop through an object that consists of both nested objects and

I have a unique situation where I am working with an object that contains nested objects and arrays. Allow me to present the structure: var keywords = { "Animals" : { "Pets" : [ "Guppy", "Parrot", "Goldfish", "Dog", "Cat" ], ...