简体   繁体   中英

How should i return data in Web API Controller Methods?

I have a UI that needs lots of information. Most of the information has been identified as separate entities in database. For example, displaying the details of an Employee. The UI page might be Employee details with various sections regarding Employee Information such as Personal Details, Address, Contact details, Project Details etc.

So if we derive the entities for the same requirement, we will have following tables

Employee

Address

Contact

Project

We will be implementing repository pattern in DA layer, and ASP.Net Web API as our restful service layer.

So, now if i need to display Employee details in the UI, what should be the best approach. What i was thinking is mentioned below.

Approach

There will be a single call to Web Api to get the Employee details. Internally it will connect to database multiple times to get Employee details, Address information, Contact information, Projects information etc and populate into one EmployeeDetailsModel (a kind of viewmodel ) and return this as a JSON.

Is this a good approach ? If there is any other better approach please suggest

Going with your approach might prove as a poor user experience as users will wait for all the data to be assembled. I suggest you divide your web page into sections. First call employee data, and then on a different client event (say click the employee's project cell) call the employee's project detail (supposing it is another table)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM