23

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?

gnat
  • 21,442
  • 29
  • 112
  • 288
John Strowsky
  • 347
  • 1
  • 2
  • 7
  • 7
    It's worth mentioning that ASP.NET MVC does not actually implement the MVC design pattern. MVC dictates that the model is observed by the view for changes which is clearly not the case in ASP.NET MVC. – Benjamin Gruenbaum Dec 29 '14 at 11:28
  • @BenjaminGruenbaum Are you suggesting here that there is a difference between original MVC pattern and the one Microsoft is using? – FaizanHussainRabbani Dec 29 '14 at 11:33
  • 2
    @FaizanRabbani no, I am suggesting that ASP.NET MVC does not implement the MVC design pattern at all - calling it that was a marketting decision. – Benjamin Gruenbaum Dec 29 '14 at 11:35
  • @BenjaminGruenbaum Is it documented somewhere? – FaizanHussainRabbani Dec 29 '14 at 11:56
  • 1
    @FaizanRabbani - there is always a difference between a design concept and what Microsoft does when it says it's applying that concept. – Pete Becker Dec 29 '14 at 13:28
  • 11
    It might also help if you changed the way you think about Views. Views *are not* client-side code. They are server-side templates that, when processed, generate client-side html. And as server-side code, it is perfectly acceptable that there is C# code there. – Eric King Dec 29 '14 at 15:12
  • 3
    You seem to think that the presentation layer should not involve any logic at all. I don't know where you got that idea, but it would make for very poor UIs. –  Dec 29 '14 at 16:56
  • 6
    @EricKing except for the part where templating systems that allow arbitrary code always lead, via path of least resistance, to bad design, horrible layering violation, and unmaintainability. Unfortunately, it seems to be a lesson that every community has to learn on its own. – hobbs Dec 29 '14 at 17:09
  • 12
    @hobbs Wow, ok. Not in my experience. Certainly not always, and (of course) there's some professional responsibility required on the part of the programmer. I don't blame the tool. – Eric King Dec 29 '14 at 17:32
  • @hobbs Even if you don't use a templating system, some part of the program has to generate HTML. There's nothing preventing arbitrary code from being written there, nor anything forcing the developer to modularize that code properly. Besides, if you allow a template system to execute code that isn't "arbitrary", what kind of code do you allow, exactly? – Doval Dec 29 '14 at 17:33
  • 2
    @Doval Twig/Swig is an example of a logic-lite templating system that's been popular enough to be cloned into many languages; ZPT/TAL is an older one with more XML crud but the same goals. Both allow user functions for filtering and advanced conditionals, but no code in the host language is ever found in the template itself; it's more like registering a limited set of callbacks by name. The templating language itself is deliberately far from Turing-complete. – hobbs Dec 29 '14 at 17:39
  • 7
    @BenjaminGruenbaum Isn't like every "MVC"-framework these days different in how they manage interdependencies? To the point where it's no longer productive to talk about The One And Only True MVC-Style, but where it would be more pragmatic to use the term for any system that reasonably partitions responsibility in _Models_, _Views_ and _Controllers_ however these are interdependent? – Alex Dec 30 '14 at 02:15
  • @BenjaminGruenbaum are you sure you really understand what MVC is? Your description of it sounds like a very specific subset, likely as implemented by a very specific implementation. – AviD Dec 30 '14 at 14:38
  • 2
    @AviD I'm quite sure, I've also talked about it with the Microsoft ASP.NET MVC team who acknowledged it and explained the rationale (marketting mostly). – Benjamin Gruenbaum Dec 30 '14 at 16:19
  • 2
    @Alex I'm not saying there is "one true MVC" - only that ASP.NET MVC is not an MVC framework (and thank god it's not since it would have been horrible if it were one). MVC actually means something pretty specific but people use it here for "separation of concerns between the presentation layer and business logic" which is a good thing to have but definitely not the same thing as MVC. All this is usually very uninteresting (because arguing about names of stuff is pointless bikeshedding) except when people ask a specific question about it (like this one) which makes the distinction necessary. – Benjamin Gruenbaum Dec 30 '14 at 16:22
  • 2
    @Alex blame rails :( – mefisto Dec 30 '14 at 19:18
  • @BenjaminGruenbaum But do you still claim that the specific dependencies between the _Model_ and the _View_ is a significant part of the MVC-pattern, thus disqualifying ASP.NET MVC in regards to its implementation of MVC as you do in your first comment? Isn't that exactly saying that there is "one true MVC"? (And of course when referring to "MVC-frameworks", we mean frameworks that utilizes the MVC pattern in its architecture, not frameworks that solely provides MVC infrastructure) – Alex Dec 31 '14 at 04:59
  • @BenjaminGruenbaum Or maybe what we need is a distinction between the originally invented MVC-pattern with all its quirks and implementation details, and the more general pattern of simply partitioning the logic of a system into Models, Views and Controllers, as we have discovered that this is the actual core value of MVC and that the specific details of the former just confuses people and brings little value to the table? – Alex Dec 31 '14 at 05:31
  • @Alex my point was you're calling "Separation of concerns between the presentation layer and the business logic layer" which is indeed a general pattern MVC which is the name of a specific pattern. Imagine if I called "A class with a reference to an instance of its own supertype" a decorator class, all decorators hold a reference they delegate to but then again linked list nodes typically also hold a reference to the next node but they're in no way decorators. This is like MVC here - MVC is a _specific_ partitioning which ASP.NET MVC __does not do__. They named it MVC for marketing reasons. – Benjamin Gruenbaum Jan 08 '15 at 23:31
  • @BenjaminGruenbaum I'm not calling "Separation of concerns between the presentation layer and the business logic layer" for MVC. I'm calling the partitioning of a system into the three parts: Model(domain model), View(presentational logic) and Controller(business logic) for MVC. And I'm suggesting that the interdependencies in "classical MVC" are so insignificant in the value in this pattern and how it is commonly used, as far as I know, that it motivates a change of vocabulary. Language is dynamic. E.g. _classical MVC_ vs _contemporary MVC_. – Alex Jan 09 '15 at 06:16

4 Answers4

66

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?

Oded
  • 53,326
  • 19
  • 166
  • 181
  • 1
    But C# is server-side language? – John Strowsky Dec 29 '14 at 10:02
  • 6
    @John - so? If you need to format dates for display (and display means client side, always), where would you format them? The model? The controller? Neither. You would do so in the view. – Oded Dec 29 '14 at 10:06
  • But for that we already have Javascript? @Oded – John Strowsky Dec 29 '14 at 10:19
  • 16
    @John - so, your date is stored in the DB, you pass it through the model/controller to your view. You need in there in the HTML, so you would output it somehow to JS to format, instead of directly formatting with C#? Why? Why is that better? Or rather, how is that approach more of a separation of concerns? – Oded Dec 29 '14 at 10:32
  • Ok, thank you for helping me out. I'll mark it as correct. =) – John Strowsky Dec 29 '14 at 10:34
  • 1
    @JohnStrowsky I'd just note that while C# is a server-side language, that does not in any way diminish the fact it is also a client-side language. If this doesn't make sense, open another question or do some reading. – NPSF3000 Dec 29 '14 at 11:12
  • 26
    @NPSF3000 - a language isn't "server side" or "client side". That's an architectural separation - and possibly one of language implementations (is JavaScript a server side or client side language - remember node.js). – Oded Dec 29 '14 at 12:19
  • 1
    Its not about the language or where the language is being used - its about facilitating efficient (or perhaps *effective*) server side rendering and razor is there to facilitate extending VS IDE support into server side templates. Separation of concerns is about what you actually do with the C# in a given template – Murph Dec 29 '14 at 13:01
  • What about formatting the date to a string to be displayed client side and storing it in a view model? Is that bad? Should the formatting logic always be in the view? – FreeAsInBeer Dec 29 '14 at 14:30
  • 14
    @FreeAsInBeer - this is the kind of logic that belongs on the client side - someone in France would want to see dates (and currency/numbers) formatted differently to someone in the US. The client would "know" best how these should be displayed. This is **presentation** logic, and as such belongs in the view. – Oded Dec 29 '14 at 14:37
  • 1
    @Oded, what makes you thing that all clients will have Javascript? Also given that dates need to be formatted in emails and reports, is it not better to use the same code in all of the cases? – Ian Dec 30 '14 at 09:49
  • @Ian - where did I say all clients have JavaScript? I said the client is best positioned to know how to format culturally sensitive things like dates, times, currency and numbers. I didn't say it has to do so via JavaScript. And the doesn't imply different code. As for emails and reports - that **entirely** depends on requirements. – Oded Dec 30 '14 at 09:52
35

Rather than directly answer the question, my response questions the assumption made in the question. That is, the assumption that Razor was built for MVC is incorrect. I work at Microsoft on the ASP.NET team and have first-hand knowledge of this.

Razor did not start out as a view engine for MVC. It was created for ASP.NET Web Pages, which is probably as far as you can go towards the least-separated-concerns side of the spectrum. It was created as a modern alternative to ASP.NET Web Forms / Classic ASP and of course many other similar server programming frameworks. The idea of Razor was to create nearly seamless transitions between HTML (markup) and C# (code).

Only later did the team (which includes myself) decide that the Razor syntax would make a lot of sense for the sake of a view engine for MVC, which was being written by the same team.

As to whether Razor enables, hinders, improves, or alters the concept of separation of concerns in ASP.NET MVC, Oded's answer is spot on.

Eilon
  • 459
  • 3
  • 5
  • 2
    Yay, downvote without a comment. I amended my answer to make it clear that I am questioning the assumption made in the original question. As I see it, the question is not directly answerable because it has an invalid premise. – Eilon Dec 29 '14 at 17:19
  • Out of curiosity, were any other templating engines considered for ASP MVC? – NWard Dec 29 '14 at 20:22
  • 2
    @NWard There were a number of 3rd party view engines for ASP.NET MVC at the time, but we didn't consider them too strongly. We felt that Razor was easier to understand (the HTML is HTML, the C# is C#) and also gelled better with the ASP.NET Web Pages project. – Eilon Dec 29 '14 at 21:13
  • 1
    @Alex oh I certainly can't take credit for all of Razor, but I appreciate your comment! – Eilon Dec 30 '14 at 03:59
  • We need some way to distinguish when an authoritative source posts an answer. +1 – asteri Dec 30 '14 at 18:11
  • 2
    @ateri After a short while it is the large number at the top left of the answer. – Mark Hurd Dec 31 '14 at 00:24
9

You are confusing "separation of technologies" with "separation of concerns". The basic idea behind the "View" portion of MVC is that code in the "View" is not performing any data access or heavy logic directly, rather that is left to the "Model" and "Controller" portions respectively. The "Controller" transforms the data, performs any necessary logic, and routes it to the correct "View". The view can also do data transformations, but I tend to keep them purely cosmetic, such as the date transform mentioned above.

whoisthemachine
  • 211
  • 1
  • 1
  • this doesn't seem to offer anything substantial over points made and explained in prior answers, particularly [this one](http://programmers.stackexchange.com/a/267601/31260) – gnat Dec 29 '14 at 21:02
  • 4
    +1 Formulated in a concise and clear way and with a different focus of explanation than previous answers. – Alex Dec 30 '14 at 02:26
  • @gnat I just wanted to make it clear where his confusion lay and then quickly explain how the separation of concerns principle applies to the MVC design pattern. Perhaps I should have spent more time on what "separation of concerns" means? – whoisthemachine Dec 31 '14 at 21:02
0

I can think of a perfect Don't do it example.

Lets say we have a ProductController:

public class ProductController()
{
    public ViewResult Discontinued()
    {
        var db = new ProductsDb();
        var products = db.Products.Where(x => x.Discontinued).ToList();
        return new ViewResult(products);
    }
}

With razor we have an alternative

public class ProductController()
{
    public ViewResult Discontinued()
    {
        var db = new ProductsDb();
        var products = db.Products.ToList();
        return new ViewResult(products);
    }
}

and in our view:

@model IEnumerable<Product> 

@foreach (var item in Model.Where(x => x.Discontinued)) {
    ....
}

I think it is pretty obvious that the second solution just feels so wrong. If you do something like this, don't blame razor - blame yourself.

And don't forget: Being able to use C# in views is not a razor feature, it was possible with ASP.NET views, too. With razor it's just a little bit simpler.

If you are searching for a template engine which is more rails like you should take a look at nancy.fx with the Super simple view engine.