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.
source share