If I get IQueryable out of my service level, will there be less database calls if I need to grab information from multiple services?

If I expose IQueryable from my service level, will there be less database calls if I need to grab information from multiple services?

For example, I would like to display 2 separate lists on the Posts and Users page. I have 2 separate services that provide a list of them. If both options provide IQueryable, will they be combined into one database call? Each repository creates a context for itself.

+2
source share
6 answers

It is best to think of IQueryable<T> as the only query waiting to be launched. Therefore, if you return 2 IQueryable<T> instances and run them in the controller, this will not be different from how they will be executed separately in their own maintenance methods. Each time you execute an IQuerable<T> to get the results, it runs the query on its own, regardless of other IQuerable<T> objects.

The only time (as far as I know) this will affect if there is enough time between two service calls that can close the database connection, but you need a considerable amount of time between service calls for this to be so.

Returning the IQuerable<T> to the controller still has some usefulness, for example, simplifies the processing of paging and sorting (therefore, sorting is performed on the controller and is not performed at the service level, which does not necessarily concern data sorting or paged). However, this does not concern performance, and people will not agree if this is best done in the controller or not (I saw that reputable developers do this and give well-thought-out reasons).

+4
source

Not. The best IQueryable can do is reduce the number of calls in the context of a unique database. IQueryable will not cross contexts.

Personally, I do not use IQueryables past repositories for several reasons:

1) I do not use the same domain objects as database objects, and seeing that "there is no translation into SQL" infuriates me;)

2) I do not like the necessary structure for IQueryables in the views: foreach (var element in the collection) {var tempItem = item; tempItem code}

3) I came up with a method for passing common filters to the data layer (LinqKit and PredicateBuilder - gods)

If these reasons are not applicable to you, you can of course freely use IQueryables depending on which layer you need.

+2
source

Not with two different contexts.

+1
source

Definitely NO. This is a fuzzy abstraction.

He admits such abominations:

 q.Where(x=>{Console.WriteLine("fail");return true;}); 

The thing is - when exposing IQueryable, you say that your data layer fully supports linq for objects.

+1
source

If you make two method calls, you will make two requests.

You can combine methods into one method, which immediately receives all the data. If you implement a repository template, it will be easier for you if you create an instance of one database context for each query.

0
source

The service level is exactly the layer that serves what you need. Often my service layers are called things like SearchService , which have methods for returning every packaged collection I will ever need (presentation models themselves). And if I ever need a new search, my service level will get a new method. Support for your service level can then contain any backup or data storage model that you would like, be it a repository or provider of the Entity Framework, etc.

To answer your question, the line should be drawn at the service level, all requests should be contained within it and only data should be returned.

0
source

All Articles