1

Logic isn't allowed in the view, but i wonder if it is accepted to use a LINQ in your view.

For example: If I have a view which renders the players of a team. In the sidebar of the same view I have a widget which renders the topscorers or something. Can I use the same player collection and use a LINQ to get the top 5 players with the most goals?

I'm not looking for a solution. I know it is possible with LINQ, but does it follow coding guidelines(best practices)?

Update: My application was simple at a first glance. So I thought i could use my DomainModels(Entities) as models for my view. I now completely separated my DomainModels and use a builder to create my view models. All linq logic is now moved from view to builder, much cleaner.

5
  • maybe this helps you: stackoverflow.com/questions/8413249/… Commented Oct 16, 2013 at 12:06
  • i would have this logic in my controller and definitely not on the views Commented Oct 16, 2013 at 12:07
  • But a solution would be to create a separate Topscorersmodel and fill this in the controller. Then add it to a greater model which also has the Players as a model? Commented Oct 16, 2013 at 12:11
  • 1
    I would have this logic in my ViewModel and definitely not in my controllers. Commented Oct 16, 2013 at 12:11
  • Never thought of using my view model. Commented Oct 16, 2013 at 13:16

2 Answers 2

3

As a rule of thumb - imagine that you allow your webapp to function as webservice, returning XML(or JSON or whatever) instead of rendering HTML. Any piece of logic that would still be needed when the app acts as webservice is part of the business logic, and should be in the controller. Any piece of logic that turns redundant in webservice mode is presentation logic and should be in the view.

Does the webservice need to fetch the top 5 players? Yes! So this is business logic, and should not be in the view.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, nice explanation. Another question: can I define the LINQ in the view Model? Cause a team already has it's players. So if i define a function on team that just iterates the current player collection and returns the top 5 players?
I'm not very familiar with ASP.NET MCV methodology, so you'll want to get an extra opinion. In RoR, you would create a scope(=method for affection queries) in the model, so that anyone that uses the model can use it. The controller prepares a query for the view, so the view can apply the scope to the query. You can't do that in MVC.NET because the view can only access the model via the view-model. You also shouldn't put that method in the view-model, since it's just a container that shouldn't do any logic. Maybe put the method in the model and write it to a field in the view-model?
My application was simple at a first glance. So I thought i could use my DomainModels(Entities) as models for my view. I now completely separated my DomainModels and use a builder to create my view models. All linq logic is now moved from view to builder, much cleaner. Thank you for your response/opinion.
"LINQ Logic" is not a thing. LINQ is just a new(well... not that new anymore...) way to handle data. The question where to put logic is not about how that logic handles data - it's about what that logic does with that data.
I know LINQ logic isn't a thing, but it hands you the possibility to iterate your collections in a much cleaner way. My views still look clean when using LINQ there like a simple count, but I had to change my mindset, because its obviously way cleaner/testable to use a builder and viewmodel.
3

There is nothing wrong with using logic in views, as long as it is presentational logic, not business logic. Hence, you may use linq in views.

In your case, calculating top scoring players, I think would be considered business logic, not presentational logic, and should be done in the controller, not the view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.