jeudi 30 mars 2017

Advice on how to go about returning an entity with many other related entities

I am building an application with ASP.NET Web Api. This application has a MessageController, which has methods that concern everything related to, you guessed it, the Message entity. This Message entity has many related entities.

My question is, when I request a certain Message, should I return a Message and all its related entities, even though they might not be needed?

Reading similar questions on this site, I know that the answer is 'it depends', but usually in favor for returning a graph instead of sending a request for each dependend entity associated with the main entity, because of possible network latency.

My choice could be that I make adding in the extra entities optional (I got this from a pluralsight course on WEB API by Shawn Wildermuth), but the problem here is that if I have a lot of related entities, the URL will become something like ?includeThis=true,includeThat=false,includeSmtElse=false,... which seems to me a bit overboard. Not to mention the parameterlist of my ActionMethod. Also, this would mean that in my repository, I would have a jungle of methods with names like GetMessageWithThisIncluded, GetMessageWithThatIncluded, GetMessageWithThisAndThatIncluded, etc.

Now, if this is a good idea, what could be some patterns I could use to make this manageable? I know this goes a bit more into technicalities, but how to solve having a general method in my repository from which I can include or exclude entitities with EF?

Another idea is to just always load the graph, and whatever I use I use, and whatever I don't use I don't. There's one caveat though; I want to make use of Angular2. I have not used Angular2 before, but from what I read and watched, is that you break up the application (or page) into modules, which in turn make use of services. But don't these services need to make use of the smallest json possible? I.e., if I pull in a graph, how would this work with the services? Can I distribute from the graph to the services?

Or,...? What could be some other idea that I could make us of? Maybe have the graph and just add the id's of the related entities, so that if the consumer needs a related entity, it could request it by sending the id to the appropriate endpoint?

Aucun commentaire:

Enregistrer un commentaire