Interesting use of XML Literals as a View Engine

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 View

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.

What others have said

Requesting Gravatar... Javier Lozano Dec 29, 2008 1:46 PM
# re: Interesting use of XML Literals as a View Engine
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
Requesting Gravatar... haacked Dec 29, 2008 1:53 PM
# re: Interesting use of XML Literals as a View Engine
@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.
Requesting Gravatar... Rod Dec 29, 2008 2:12 PM
# re: Interesting use of XML Literals as a View Engine
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)
Requesting Gravatar... haacked Dec 29, 2008 4:26 PM
# re: Interesting use of XML Literals as a View Engine
@Rod XML Literals are a fully integrated language feature of VB now. It's pretty cool actually.
Requesting Gravatar... Marwan Dec 29, 2008 4:50 PM
# re: Interesting use of XML Literals as a View Engine
this is really innovative.
Requesting Gravatar... Victor Kornov Dec 29, 2008 4:51 PM
# re: Interesting use of XML Literals as a View Engine
"Inherits SiteMaster" - that's somethings (i.e. specifying layout) I'd not want in my view.
Requesting Gravatar... Dustin Campbell Dec 29, 2008 6:36 PM
# re: Interesting use of XML Literals as a View Engine
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.
Requesting Gravatar... Sean Chambers Dec 29, 2008 7:33 PM
# re: Interesting use of XML Literals as a View Engine
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.

Requesting Gravatar... Barry Kelly Dec 29, 2008 8:52 PM
# re: Interesting use of XML Literals as a View Engine
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.
Requesting Gravatar... Jarrett Dec 29, 2008 10:46 PM
# re: Interesting use of XML Literals as a View Engine
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.
Requesting Gravatar... Henri Dec 30, 2008 4:57 AM
# re: Interesting use of XML Literals as a View Engine
<quote>
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.
</quote>

If you put the view classes in a separate project, surely they'll compile into their own assembly?
Requesting Gravatar... Henri Dec 30, 2008 5:12 AM
# re: Interesting use of XML Literals as a View Engine
Another advantage of this scheme is that strings are automatically escaped.
Requesting Gravatar... Andrew Davey Dec 30, 2008 7:14 AM
# re: Interesting use of XML Literals as a View Engine
Did you not see this on Scott Hanselman's blog?
www.hanselman.com/.../...ewEngineForASPNETMVC.aspx


Requesting Gravatar... [|] Dec 30, 2008 7:23 AM
# re: Interesting use of XML Literals as a View Engine
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 asp.net mvc.

If you're unfamiliar with Seaside, go look at www.seaside.st.
Requesting Gravatar... Rodrigo Dec 30, 2008 12:38 PM
# re: Interesting use of XML Literals as a View Engine
Phil,

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...

Product.Name

How will that be handled in this new scenario?
Requesting Gravatar... McLovin Dec 30, 2008 1:25 PM
# re: Interesting use of XML Literals as a View Engine
What is a "PUM"?
Requesting Gravatar... haacked Dec 30, 2008 1:39 PM
# re: Interesting use of XML Literals as a View Engine
You'd simply use the Inherits attribute in the @Page declaration like so:

&lt;%@ Page Inherits="ViewPage&lt;IEnumerable&lt;Product>>" ...
Requesting Gravatar... Oskar Austegard Jan 05, 2009 7:35 AM
# re: Interesting use of XML Literals as a View Engine
@McLovin: PUM = Product Unit Manager

What do you have to say?

(will show your gravatar)
Please add 3 and 2 and type the answer here: