I'm not very familiar with the MVC pattern. Could you tell me which one of the following three controller actions is better? Thanks :)
(1) Have query in action:
public ActionResult List()
{
var query = repository.Query().Where(it => it.IsHandled).OrderBy(it => it.Id);
// ...
}
(2) Have query in service:
public ActionResult List()
{
var items = service.GetHandledItemsOrderById();
// ...
}
(3) Have order by in action:
public ActionResult List()
{
var items = service.GetHandledItems().OrderBy(it => it.Id);
// ...
}
If we choose (1), then we have too much business logic in controller?
If we choose (2), there might be lots of service methods like GetXXXByYYY()
.
If we choose (3), why we encapsulate Where(it => it.IsHandled)
but notOrderBy(it => it.Id
.
Any ideas?
LINQ syntax is typically less efficient than a foreach loop. It's good to be aware of any performance tradeoff that might occur when you use LINQ to improve the readability of your code. And if you'd like to measure the performance difference, you can use a tool like BenchmarkDotNet to do so.
LINQ in C# is used to work with data access from sources such as objects, data sets, SQL Server, and XML. LINQ stands for Language Integrated Query. LINQ is a data querying API with SQL like query syntaxes. LINQ provides functions to query cached data from all kinds of data sources.
LINQ offers the following advantages: LINQ offers a common syntax for querying any type of data sources. Secondly, it binds the gap between relational and object-oriented approachs. LINQ expedites development time by catching errors at compile time and includes IntelliSense & Debugging support.
I'm sure opinions may vary, but I've learned to try to keep as much business logic in the service as you can. 3 would be my choice. With 1, you've already spotted the issue. With 2, you are introducing display precedence in a service. With 3, you handle display preferences where necessary. If you were to introduce another interface to your business layer, you are requiring potentially, unnecessary code iterations by choosing 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With