Return an IQueryable or Enumerated Object

I was interested to know about the difference in performance between the two scenarios and what could be the disadvantages from each other?

The first scenario:

public class Helper //returns IQueryable { public IQueryable<Customer> CurrentCustomer { get{return new DataContext().Where(t=>t.CustomerId == 1); } } public class SomeClass { public void Main() { Console.WriteLine(new Helper().CurrentCustomer.First().Name; } } 

Second scenario:

 public class Helper //returns Enumerated result { public Customer CurrentCustomer { get{return new DataContext().First(t=>t.CustomerId == 1); } } public class SomeClass { public void Main() { Console.WriteLine(new Helper().CurrentCustomer.Name; } } 

Thanks in advance.

+4
source share
3 answers

Well, the main difference that I see is when the request is executed and what else you can do with the request.

For example, suppose your Customer object has several large fields. Using the second approach, you will always receive them. Using the first approach, you can write:

 string name = helper.CurrentCustomer.Select(x => x.Name).First(); 

To do this, you would need to query only one field in the database. From the point of view of synchronization, the request will be executed only when you really request the data (since it can wait until you use Select to determine what to put in the request in the above case). It has pros and cons - it can complicate the reasoning, but it can also save. As for the β€œreasoning about” side, you know that as soon as you have a client, you have an object that you can simply work with. If you use the same query twice, you need to know if your LINQ query provider will cache the result ... if you write:

 IQueryable<Customer> currentCustomerQuery = helper.CurrentCustomer; Customer x = currentCustomerQuery.First(); Customer y = currentCustomerQuery.First(); 

will issue a request once or twice? I suspect that this is very dependent on the provider, but I would not want to make any guesses about specific ones.

Another thing to think about is how easy it is to use the API you are creating. Personally, it would usually be easier for me to use an API that gives me the data I want, rather than a query from which I can get this data. On the other hand, it is a little less flexible.

One option is to allow both methods to be used - GetCurrentCustomerQuery () and the GetCurrentCustomer () method. (I probably would not have made their properties on my own, but this is just a matter of personal preference.) That way, you can get the flexibility you want when you really need it, but just try to just get the current client as an object.

+4
source

In short, using IQueryable is much better and allows you to additionally filter the returned IQueryable along the way without having an object or collection actually loaded into memory. In this case, the return type is a simple Client class and the impact will be minimal, but in the case of collections you are strongly advised to use IQueryable. Chris Sells shows the problem in more detail here

0
source

The difference between the methods is that the first returns an expression that can return an object, while the second has already executed the expression and returns the object.

In this complex scenario, the difference is not very useful, and returning a single object as an expression is not very intuitive.

A scenario in which the difference is more useful is if you have a method that returns multiple objects. Delayed execution of an expression means that you only load the objects that you use. In the case when you need only the first few objects, the remaining objects will not be created.

0
source

Source: https://habr.com/ru/post/1306201/


All Articles