Answered by:
If MVC is “Separation of Concerns” then why was Razor Syntax introduced?

Question
-
My question is related to MVC design pattern and Razor Syntax introduced by Microsoft.
While learning MVC design pattern I was told that the idea is based upon a principle known as Separation of Concerns.
But Razor Syntax allows us to use C# in Views directly.
Isn't this intersection of concerns?
Monday, January 5, 2015 1:50 AM
Answers
-
You are conflating the Razor syntax with separation of concerns.
Separation of concerns has to do with how you structure your code.
Being able to use C# in views doesn't prevent that. It has nothing to do with separation of concerns as such.
Sure, you can structure the code in your view to not comply with separation of concerns, but what about C# code that is used for display purposes only? Where would that live?
- Proposed as answer by 1-----------1 Monday, January 5, 2015 6:29 AM
- Marked as answer by HackerMayCry Monday, January 5, 2015 11:23 AM
Monday, January 5, 2015 5:52 AM
All replies
-
Hi,
Good point indeed.
But we have to understand that Razor is just a view engine. It does not restrict you to code in c# just like the Asp.Net Webform engine. so whether or not you've placed c# code in it, it is your programming discipline.
MVC on the other hand is still what it is as historically discussed many times; most ASP.NET MVC developers clearly separates UI and business layer codes; so if you code your logic in the razor itself then it is still your prerogative.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Proposed as answer by 1-----------1 Monday, January 5, 2015 6:27 AM
- Unproposed as answer by 1-----------1 Monday, January 5, 2015 6:29 AM
Monday, January 5, 2015 3:43 AM -
Sometimes you do want to use programming structs like looping or conditionals when constructing thing like grids. Even if in most case these can be converted to templates, there are cases that it could be not effective enough.
Say, a nested grid containing 2 enum value that has 5 possible values, that depending on these values, the detail row should show/hide different fields, and postback to different controller methods. You can choose to construct 25 templates to do it in declarative way, or use razor syntax to do it by some inline logic.
IMO, some web developers from the old days of ASP also would prefer to do it this way.
Monday, January 5, 2015 3:50 AMAnswerer -
You are conflating the Razor syntax with separation of concerns.
Separation of concerns has to do with how you structure your code.
Being able to use C# in views doesn't prevent that. It has nothing to do with separation of concerns as such.
Sure, you can structure the code in your view to not comply with separation of concerns, but what about C# code that is used for display purposes only? Where would that live?
- Proposed as answer by 1-----------1 Monday, January 5, 2015 6:29 AM
- Marked as answer by HackerMayCry Monday, January 5, 2015 11:23 AM
Monday, January 5, 2015 5:52 AM -
Refer to this discussion as well.
- Edited by Youen Zen Monday, January 5, 2015 6:57 AM
Monday, January 5, 2015 6:56 AM -
EngSoonCheah is right. Razor is not a part of the SoC discussion as it is a client side code convertor allowing you to embed C# into an HTML (CSHTML) web page view. Now how you embed that code is a discussion of SoC. In particular, just sticking to binding and format concerns. Don't put business logic of any type in the View. Also do not access the data layer directly from the view.
JP Cowboy Coders Unite!
Monday, January 5, 2015 7:15 AM