Article Image
read

I was incredibly skeptical when I heard about WebMatrix. I was dismissive and snotty about the WebMatrix data access story. I called the WebMatrix IDE a "MySpace Code Editor". I was wrong. They got it right, and I'm really impressed.

No, Really. It's Impressive

As opposed to "the usual suspects" blogging about how awesome WebMatrix is without doing much more than "Hello World" - I, along with

Scott Hanselman decided to

write and deploy a working website with WebMatrix. It's small, it's simple. But I went through the entire process and learned a lot (and got some bruises along the way). The summary form: I was able to get my work done with 90% of the "noise" removed. I felt myself pulled along happily - learning as I went - and I finished what I needed to do in short order. I would expect something like this from Sinatra - hardly from Microsoft.

To be honest, I'm a bit stunned by it. WebMatrix is full 180 from the highly abstracted cathedral that is ASP.NET. It's a nod to PHP and classic ASP with a clean syntax and a budding package system that I think will eclipse ASP.NET in the next few years. Unless they screw it up and the fiefs inside DevDiv go to war over it. Which they surely will - but ... well let's stay positive :).

No, it's not a "look - NO CODE!" solution. WebMatrix is focused on simplicity and the "Get It Done" developer. Which, to me, is a massive undersell as we're all "Get it Done" developers. I think by focusing on "Get it Done" the Web Platform team has somewhat accidentally backed themselves into where the web is going these days: simplicity, elegance, and power.

Sinatra is a fine example of this - it's incredibly stripped down, yet it's also incredibly powerful. WebMatrix made me feel the same way. Enough of the preamble - let's get to the code.

Data Access - Compelling and Simple

One thing you'll notice right away is that you have to use raw SQL to query the database. This is going to turn off the Ivory Tower crowd who prefer to wade through XML Soup and tedious designers - but that's to their detriment. As I've always said - the best DSL I've ever seen for working with data is SQL.

However, I also really hate supporting it and debugging it. More on that below - and it's my guess we'll see an ActiveRecord package in the next 6 months anyway.

To run up a query you do something like this (from our TDL website):

var db = Database.Open("TDL");
var selectQueryString = "SELECT * FROM Articles";
shows = db.Query(selectQueryString, slug);

Hooray for SQL! OK stay with me - what this returns is IEnumerable . Which means that it's typed on the fly - DUCK FRICKIN TYPING. Well sort of. I can now loop this comme ca:

@foreach(var show in shows){ The title is @show.title }

That's Razor in action - a terse, easy to love and understand markup for working in WebMatrix. The point here is that "show.Title" shouldn't exist - "Show" isn't a class anywhere in my project. This is the

Groovy Dynamic Freakshow that's built into C# 4.0.

I get to work with data in a semi-typed way, without a massive crufty designer, repositories, XML, modelbinders, LINQ knuckle-buster code, and Fear of Codebetter Guys? Yeah - I'll do that. In fact, gimme seconds.

To query a single record, you do this:

var db = Database.Open("TDL");
var selectQueryString = "SELECT * FROM Articles WHERE slug = @0";
show = db.QuerySingle(selectQueryString, slug);

Very "SubSonic-y" if I do say so myself. Which makes me rather happy. There's a parameter in use there so we avoid injection problems, and I've put this bit of code into an object called Article - so I don't write SQL all over my app. The method here is "Article.Find(string slug)" - boom.

You start off with SQL CE - a pretty capable DB for development. It's a file-based DB just like SQLite but it has tooling built right into WebMatrix

Let's talk about this for a second. You're probably a really smart person, sitting there thinking "that's crap". You might suggest that people who don't know any better will litter their app with SQL strings that are hard to debug and ripe for injection attacks.

Just like you did. And just like I did. We can wave our arms all day and talk about how Microsoft is teaching kids to play on the freeway, or we can spin this around: this tool is actually better suited to people who know what they're doing. More on that below...

If you work with a DBA

say... someone like Rob Sullivan, you might find WebMatrix to be a breath of fresh air. You can work with their StoredProcedures dynamically, without generating a designed bit of code - just access the returned data as properties. It makes this whole ORM battle with DBA thing rather pointless - which is a win for those of you who do battle daily.

90% of the .NET developer landscape wants their SQL abstracted. A large percent of these people DON'T want their HTML abstracted. Perhaps if you think of SQL as "data HTML" you'll feel a bit better about being given a WHOLE LOT MORE CONTROL over the way queries are executed. No more mangled SQL created by EF. No more Googling for "How do I do an IN query with embedded ORs". Just write it and move on.

Razor, Helpers, Layouts and Sections

It's hard not to love Razor once you get the hang of it. It's an incredibly light code-footstep in the HTML, and the fact that there's no designer makes me really happy.

One thing I particularly like are the helpers. You just create a .cshtml file (a Razor file) in App_Code and create yourself some helpers. I call this one "Sugar" - cause it's sweet and sexxy:

@helper MusicLink(string artist, string title, string link){

  @artist 
  - @title

}

That signature is so... wonderfully terse and accommodating. It really does remind me of the simplicity of Sinatra. To use this in my code:

@Sugar.MusicLink("Puscifer", "Momma Sed", "http://puscifer.com")

No wonky gator tags, no references to set in Web.Config. No massive set of overloads to wade through on built-in helpers. It just works.

Not only that - but you're actually PUSHED into tersing up your views by the Razor engine. I have a feeling this isn't intentional, but sometimes when a side-effect is the developer doing the right thing, it means that the tool is focused properly.

For instance - we need to embed MP3 links in our site, and we use a bit of convention when it comes to naming. This means we can take the slug of the show (usually) and stick on ".mp3". Try doing this in Razor:

Download Here

And it fails. It doesn't know what the ".mp3" method is on a string - which is funny. This kind of thing happened to me routinely and every time I was quite happy to go and work up a helper to centralize this link builder. Imagine that.

Each page has a built-in object that you can use to do all kinds of things - such as setting layouts and titles, etc. Page is, once again, dynamic so you can do some funky gymnastics with it.

Layouts (think of them as Master pages) can contain layouts, which can contain more layouts. This might sound weird, but it's an interesting way to solve some problems. On our site, we have a Site layout that has our CSS declarations, Body tag, header tag and so on.

We also have a Layout for our individual shows. Normally this would all be data-driven on a single page, but we wanted the ability to have each show have it's own page - so we needed a second layout.

This works perfectly and highlights another interesting thing about Razor: the Page object. This is another bit of dynamic fun that, once set, is usable by all constructs (including the Layout pages themselves). To be more specific - if I need to pass some information "up" to the Layout for a given Show page, all I need to do is think up a property on Page and set it:

@ { Layout = "_ShowLayout.cshtml" Page.Show = Shows.Find(slug) }

"Page.Show" doesn't exist - well it didn't until I made it exist by declaring it. Such is the dynamic object. Now my _ShowLayout page can access it and use it happily.

Partials are supported just fine, as are things called "Sections" - reserved areas in a layout that can be set by the pages that contain them. Dynamic gymnastics.

That last part is important, and really defines where WebMatrix is going (and why I like it so much): WebMatrix (and Razor) are embracing fully the new dynamic features of C# 4.0 - and dammit I'm impressed.

Package Manager

This, to me, is one killer feature of WebMatrix and feels incredibly like Ruby gems. Kick up a demo site and then enter "admin" into the URL, right after localhost (it will look something like "http://localhost:3321/admin").

Up pops the package manager. You'll have to go through and set a password, etc, but this once that's done:

You get to see which packages you have installed. If you want to add more - just change the drop-down to say "online" and you'll have access to a some pretty interesting stuff:

Some of these things are pretty interesting. My favorite is

QuickEdit - an in-place content editor.

URLs and Semanticness

Geeks gotta love the URL - and at first I thought this was a major drawback with WebMatrix. The good news is that it's not at all - and the team decided to keep the abstractions to a minimum (thank DIETY!). When you install WebMatrix you also install UrlRewriter 2.0 - which is also plugged right into IIS.

This means that you can tweak your URLs right in your Web.Config:

The rule there just matches our old Tumblr post format and redirects off to the /post/ directory, sending in the second match (which is the slug of the post). If you don't know how UrlRewrite works, it's pretty damn simple - you just kick up some Regex (yay... fun) and pass the matches off. You can redirect (as we're doing here) or simple "mask" the return - reforming the URL internally into a parameterized querystring.

Here's a warning: if you define your URLs as we've done here, you'll "supercede" the machine.config settings on your server and the Extensionless URLs will break. To fix that, make sure you pop in the "This puts it on par with ASP.NET MVC in terms of lovely URLs - but there is no Routing story. Which is a good thing in some ways, it's less abstraction that you need to know. It's bad in other ways - change the rule, break your links. Unless you use a helper... see how you can solve problems easily?

Why and When Would I Use This?

Let's get down to it: when should you use WebMatrix? Would you use it as opposed to ASP.NET MVC or Rails? What about Sinatra?

WebMatrix vs. ASP.NET

Always and forever. ASP.NET WebForms needs to be put down (hmmm - strong opinion anyone?)

WebMatrix vs. ASP.NET MVC

This one's more difficult. WebMatrix is to ASP.NET MVC as Sinatra is to Rails. MVC has 3+ years of development behind it and is revving with some impressive features. If you need a full-blown framework under you then you might want to use MVC. If you don't know MVC or what it means - use WebMatrix. Honestly - the package support and easy data access make this a really close call.

WebMatrix vs. Rails

This one's a little easier. If you know Ruby/Rails already - well it's got a 7 year lead on WebMatrix. The gem system is just too compelling. If you don't know Ruby/Rails and you're a Microsoft person in a Microsoft world... well there isn't really a choice is there?

Would I use WebMatrix to build Tekpub? Right now - no. I love Rails precisely because it's 7 years old and the gem system is blowing up with just about everything I could want. If I had to move to a Windows server - unequivocally YES.

I should add that if the community can get behind the NuGet package system built into WebMatrix - look out. This choice won't be so easy in the coming years. That's a big IF, however, given the climate of .NET OSS.

WebMatrix vs. Sinatra

Again, if you're a Ruby person... well you'll have a hard time with this (if all else is equal... server choice, etc). The deployment story is about the same... sort of as Capistrano. The URL management is easier with Sinatra and again you have access to a ton of gems, and Rack itself. Sinatra is more "baked" and uses Ruby - so that might put it right over the top for you if you like Ruby. If you're an MS person in an MS world - well there's no decision is there?

Greenfield Projects

If you get to choose what you use for a new project - WebMatrix is actually a really good choice. I've been thinking and thinking about exactly when I'd jump to something like MVC - and truth is I can't think of a reason. You can build out your domain in the same way you always have, suck in a DLL, and use it in WebMatrix.

Which is kind of compelling - if things get complicated just push them to a new assembly. You can still use NHibernate if you like (which is like dragging your Grandmother to a Tool show with you) - but hey if you like NHibernate, why not?

To expand on this point - I was just talking to a client of mine the other day about a new project and I was describing WebMatrix and why it would be fun to use it. He said yes rather quickly...

The Not-so Fun Stuff

There are problems, of course. Thankfully no show-stoppers but... well you have to scratch your head a bit about some of it. Here's the not-so good...

Deployment

This is .... well I said I would keep it positive, so it's more complicated than it needs to be if you're deploying to your own IIS box, using the WebDeployment tool and not FTP. And I say that with love. The good news is that the team was very receptive to hear about my travails and have some workarounds.

Once you get it set up - it's butter. But I should add this very important point:

it's not really built up for use with your own personal IIS just yet.

It's built with deployment to hosters in mind and currently a bunch of hosters are already set and ready to roll with Deployment - so that's a win if you host your site with DiscountASP, OrcsWeb, etc.

My workaround is "just use FTP" - which makes it rather simple. But - if you have an IIS box and you want to use WebDeployment, I'll write a post on how to set it up tomorrow.

Bubble Gum and Rainbows

The UI of WebMatrix is ... sort of cutesy. It uses some neato animation effects from WPF and I hate to say it but I find it distracting:

The toolbar is uncustomizable and a bit too large for what it does, minimizing the code space. There's also a big gap to the right hand side that's empty. Understanding that the target audience is "beginners" who like buttons for Copy/Paste - I spose this makes sense. It wasn't made for me, so I can move on...

Git. If you use the publish feature of WebMatrix you'll be excited to see that there's no "ignore" settings. Such as "ignore .git". Which means you either get to send your entire repo to your server or... you don't use Git. This is unfortunate.

Summary

WebMatrix is good. It's actually quite stunning that Microsoft would take such a bold step - in the face of the Ivory Tower geeks ... and yes I'll take some responsibility for being a small turd in a big Ivory Tower toilet bowl.

I asked Microsoft to innovate, grow a pair and amaze me. And they did just that.

Blog Logo

Rob Conery

I am the Co-founder of Tekpub.com, Creator of This Developer's Life, an Author, Speaker, and sometimes a little bit opinionated.


Published