简体   繁体   中英

Correct way to load link/child data in webApi

I am a bit confused on how to load data correctly for entities with a 1 to 1, 1 to many relationship in a RESTful web api, and wondered if people could give advice which way is correct and maybe why it shouldnt be done certain ways. I think this is confusing me more because i am also trying to create a web application to show the data as well and i dont want to build the web api around the web application which i am sure many people make the mistake of doing.

Our code has many 1 to 1 relationships and also 1 to many relationships.

For example something like Company and Contact.

Now if i were to load a contact via a webApi, currently it would show a model similar to this

public class ContactModel() {
    public long Id { get; set; }

    public string Name { get; set; }

    public long CompanyId { get; set; }
}

This seems right to me, and if i want details of the company, i would just load the data from the company api.

However, on our web application, which uses our webApi, i would like to show this record on a page and it doesnt seem right to me to make 2 separate calls in our web application to load all the data? Maybe this is because i am used to ASP.NET and loading the data all at once?

So do i want a model like this instead?

public class ContactModel() {
    public long Id { get; set; }

    public string Name { get; set; }

    public CompanyModel Company { get; set; }
}

public class CompanyModel() {
    public long Id { get; set; }

    public string Name { get; set; }
}

Now this makes a lot of sense, because now i have the Company as a model and i can show the users the company name that the contact belongs too, plus i have the companyId so i can use that if i want to pass them to the company screen (using the company id as a parameter). Plus now i can use 1 web api call in my web application, instead of two.

But.... what about when i show many contacts on a page? Yes i could use the above, but if all the contacts are for the same company, then it seems kinda pointless to load the same CompanyModel for each and every contact. Maybe something like this would be better.

public class GetModel() {
    public ICollection<ContactModel> Contacts { get; set; }

    public ICollection<CompanyModel> Companies { get; set; }
}

public class ContactModel() {
    public long Id { get; set; }

    public string Name { get; set; }

    public long CompanyId { get; set; }
}

public class CompanyModel() {
    public long Id { get; set; }

    public string Name { get; set; }
}

Now i could load all the contacts into the Contacts collection and any related companies into the Companies collection. But does this completely now go against the rules of loading data via a RESTful web api?

Any pointers on webApi design for relationships would be much appreciated.

I was debating whether I should post this as a comment or as an answer. Debating primarily because design is subjective and very much dependent on the problem you are trying to solve.

My CompanyModel and ContactModel matches your second implementation though the first/third one is the most efficient in terms of memory. I use the second implementation only because I share my models on both MVC pages as well as WebAPI Controllers. Having CompanyModel right there allows me to access Company info while writing Razor. However if you don't write Razor (or like me realized over time that you would prefer using Javascript MVC/MVVM frameworks like AngularJS or JSViews), then writing it in the first/third implementation makes most sense. For me, the second implementation allows me to load CompanyModel when I want to and keep it null when it isn't needed. (It is my little justification to not refactoring code just because my style of coding has changed.)

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