Interesting use of XML Literals as a View Engine, mvc 0 comments suggest edit

Dmitry, who’s the PUM for ASP.NET, recently wrote a blog post about an interesting approach he took using VB.NET XML Literals as a view engine for ASP.NET MVC.

Now before you VB haters dismiss this blog post and leave, bear with me for just a second. Dmitry and I had a conversation one day and he noted that there are a lot of similarities between our view engine hierarchy and and normal class hierarchies.

For example, a master page is not unlike a base class. Content placeholders within a master page are similar to abstract methods. Content placeholders with default content are like virtual methods. And so on…

So he thought it would be interesting to have a class be a view rather than a template file, and put together this VB demo. One thing he left out is what the view class actually looks like in Visual Studio, which I think is kinda cool (click on image for larger view).

VB.NET XML Literal

Notice that this looks pretty similar to what you get in the default Index.aspx view. One advantage of this approach is that you’re always using VB rather than switching over to writing markup. So if you forget to close a tag, for example, you get immediate compilation errors.

Another advantage is that your “view” is now compiled into the same assembly as the rest of your code. Of course, this could also be a disadvantage depending how you look at it.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.



18 responses

  1. Avatar for Javier Lozano
    Javier Lozano December 29th, 2008

    Cool experiment, but seems like a honey pot for the developer that wants "more out of a view", thus breaking the SoC you get from MVC.
    In other words, I can see a lot DbConnections being created here.
    Just my $0.02

  2. Avatar for haacked
    haacked December 29th, 2008

    @Javier I can see DbConnections being created in a .aspx view too. But just because it can doesn't mean it will.
    As an added discipline, you could create these view classes in a separate project that doesn't reference any of the data classes.

  3. Avatar for Rod
    Rod December 29th, 2008

    First thing that caught my eye was not the hack itself, but this thing on VB that allows you to enter XML elements straight into the code, without quotes around them or anything. Weird, man.
    ..but then again, looks like alternative View Engines are the place for people go crazy now :o)

  4. Avatar for haacked
    haacked December 29th, 2008

    @Rod XML Literals are a fully integrated language feature of VB now. It's pretty cool actually.

  5. Avatar for Marwan
    Marwan December 29th, 2008

    this is really innovative.

  6. Avatar for Victor Kornov
    Victor Kornov December 29th, 2008

    "Inherits SiteMaster" - that's somethings (i.e. specifying layout) I'd not want in my view.

  7. Avatar for Dustin Campbell
    Dustin Campbell December 29th, 2008

    I was fortunate to be given a demo by Dmitry of this a few weeks back, and his approach using VB is fresh and exciting. Very, very cool.

  8. Avatar for Sean Chambers
    Sean Chambers December 29th, 2008

    Although this is an interesting trick with VB, I disagree that this should be used in the wild.
    A couple of reasons:
    1. Views should be "dumb" templates that simple display and read input. Now, if that's all this is used for then it's a great trick, but we all know that this will be abused to no end. We can believe that in a perfect world it would be great but there is always a developer somewhere looking to cut corners and do a "quick and dirty" app which morphs into an 8 headed hydra that breathes fire.
    2. This opens up the surface area that people have to work with the view and tempts them with adding "a little code" in there. So on and so forth. Again, BAD! =)
    At the same time it is an interesting approach but should be approached with caution as all techniques but more so because this has the potential to hurt peopl =)
    All the same, thanks for the info Phil definitely a different approach.

  9. Avatar for Barry Kelly
    Barry Kelly December 29th, 2008

    Raw HTML lacks simple abstraction tools like constants, functions and loops, and templating languages like ASPX never quite make working with them feel neat and clean. The "best" approaches use some combination of binding and abstractions encapsulated in components instantiated via a different namespace, but that is still very clunky compared to almost all programming languages.
    I personally think the approach described herein is a solid way to go, and preferable to most templating languages. Keeping business logic etc. out of your view is a discipline you need to have no matter what language you are using; if one were programming a rich GUI, you'd still need it.
    IMO, the templating language used should be a pure functional one - that should keep out most of those DB connections - but with loops and functions and nested scoping. Whether or not it uses XML/HTML literals would ideally be a layering choice.

  10. Avatar for Jarrett
    Jarrett December 29th, 2008

    This is very interesting and I personally like the purity of it. I think it may help those who code the rest of the application in C# to know that when they are coding the actual view in VB, that they shouldn't be doing anything too complex (just view logic). A nice context switch to act as a reminder.

  11. Avatar for Henri
    Henri December 29th, 2008

    Another advantage is that your “view” is now compiled into the same assembly as the rest of your code. Of course, this could also be a disadvantage depending how you look at it.
    If you put the view classes in a separate project, surely they'll compile into their own assembly?

  12. Avatar for Henri
    Henri December 29th, 2008

    Another advantage of this scheme is that strings are automatically escaped.

  13. Avatar for Andrew Davey
    Andrew Davey December 29th, 2008

    Did you not see this on Scott Hanselman's blog?

  14. Avatar for [|]
    [|] December 29th, 2008

    Very much like the Smalltalk framework Seaside works. Pushes rendering code into Smalltalk code which can then be refactored etc. (It uses no templates at all) It's a dream. I would love to see something similar and as flexible for mvc.
    If you're unfamiliar with Seaside, go look at

  15. Avatar for Rodrigo
    Rodrigo December 30th, 2008

    I've seen that in the next release there will be no code-behind, which is great, but how to handle strongly typed view data?
    Following Scott Gu, I could use ViewData<T> in the code-behind and then have something like this in my views...
    How will that be handled in this new scenario?

  16. Avatar for McLovin
    McLovin December 30th, 2008

    What is a "PUM"?

  17. Avatar for haacked
    haacked December 30th, 2008

    You'd simply use the Inherits attribute in the @Page declaration like so:
    &lt;%@ Page Inherits="ViewPage&lt;IEnumerable&lt;Product>>" ...

  18. Avatar for Oskar Austegard
    Oskar Austegard January 4th, 2009

    @McLovin: PUM = Product Unit Manager