简体   繁体   English

在webApi中加载链接/子数据的正确方法

[英]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. 我对如何正确地在RESTful Web API中为具有1对1、1对1关系的实体正确加载数据感到困惑,并想知道人们是否可以给出建议哪种方法正确,以及为什么不应该以某些方式进行建议。 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. 我认为这让我更加困惑,因为我也试图创建一个Web应用程序来显示数据,并且我不想围绕该Web应用程序构建Web api,我敢肯定,很多人会犯错。

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 现在,如果我要通过webApi加载联系人,则当前将显示与此类似的模型

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. 这对我来说似乎是正确的,如果我需要公司的详细信息,则只需从公司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? 但是,在使用webApi的Web应用程序上,我想将此记录显示在页面上,在我的Web应用程序中进行两次单独的调用以加载所有数据对我来说似乎不正确? Maybe this is because i am used to ASP.NET and loading the data all at once? 也许这是因为我习惯于ASP.NET并一次加载所有数据?

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). 现在这很有意义,因为现在我将公司作为模型,并且我可以向用户显示联系人也属于的公司名称,再加上我的公司编号,以便在我要将其传递给用户时使用公司屏幕(使用公司ID作为参数)。 Plus now i can use 1 web api call in my web application, instead of two. 另外,现在我可以在Web应用程序中使用1个Web api调用,而不是两个。

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. 是的,我可以使用上面的方法,但是如果所有联系人都是同一公司的,那么为每个联系人加载相同的CompanyModel似乎毫无意义。 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? 但这是否完全违反了通过RESTful Web API加载数据的规则?

Any pointers on webApi design for relationships would be much appreciated. 有关关系的webApi设计的任何指针将不胜感激。

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. 我的CompanyModel和ContactModel匹配您的第二个实现,尽管第一个/第三个实现在内存方面是最高效的。 I use the second implementation only because I share my models on both MVC pages as well as WebAPI Controllers. 我之所以使用第二种实现,是因为我在MVC页面和WebAPI控制器上共享我的模型。 Having CompanyModel right there allows me to access Company info while writing Razor. 有了CompanyModel,我可以在编写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. 但是,如果您不写Razor(或者随着时间的流逝我意识到您更喜欢使用AngularJS或JSViews之类的Javascript MVC / MVVM框架),那么在第一个/第三个实现中编写它就很有意义。 For me, the second implementation allows me to load CompanyModel when I want to and keep it null when it isn't needed. 对我来说,第二种实现方式允许我在需要时加载CompanyModel,并在不需要时将其保留为null。 (It is my little justification to not refactoring code just because my style of coding has changed.) (我的一点理由是不仅仅因为我的编码样式已更改而就不重构代码。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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