Failure is inevitable


Book Review: Instant Razor View Engine How-to

I consider myself an ASP.NET MVC expert.  I’m very comfortable with the platform, I’ve been through a large portion of the source, and I’ve built countless apps on it.  While I did find “Instant Razor View Engine How-to”  to be an adequate  reference for the basics of the Razor view engine, I would have a hard time recommending it. 

First, I must disclose that Packt Publishing asked me to review this book.  My policy is that any review I do will be published, whether the result is good or bad.  This is one of those cases that falls into the later half.

This is the first of their “Instant” books I’ve ever read, but I believe the general idea is that the “Instant” line of books gives you a crash-course on the subject.  If that’s the intent, then “Instant Razor View Engine How-to” is arguably successful.  It does cover the high points of the view engine and its syntax.  The book is broken down into these sections:

  • Creating the project (Should know)
  • Fundamental Razor syntaxes (Must know)
  • Razor layout pages (Become an expert)
  • Models in Razor (Must know)
  • Razor helpers (Must Know)
  • Partial views (Should know)

Each section is mostly what you’d expect.  The distinction between “Should know,” “Must know,” and “Become an expert” seems arbitrary and rather pointless.  To effectively use Razor, you must know about things like partial views and layouts.  Without that, you cannot possibly use Razor effectively. 

My main gripe with the book is that it is lacking in substance.  Virtually everything in this book is easily and readily accessible online without any digging. 

My other big gripe with the book is that it is an awkward read.  It refers to code blocks within a Razor view as “Razor syntaxes,” and there are several other poor wording choices that distracted me from the content.  I also hated how many of the examples were presented.  Here’s a simple example:

    ViewBag.Title = "Index";


        string day = DateTime.Now.DayOfWeek.ToString();
        if (day == "Sunday") 
            @:I enjoy @day with my family.


This. Is. Wrong.  And the majority of the samples in the book are like this.  This is only slightly better than writing pure C# with magic strings mixed in.  The power of Razor is that I can write my markup with bits of C# injected into it.  The mix should be more markup than C#, not the other way around.  Here’s a better way to do the same thing:

    ViewBag.Title = "Index";
    var day = DateTime.Now.DayOfWeek.ToString();

<p>@(day == "Sunday" ? "I enjoy Sunday with my family." : "")</p>

Or even better:

    ViewBag.Title = "Index";

@ if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday) {
    <p>I enjoy Sunday with my family.</p>

In the end, this book might serve as a good reference to the Razor view syntax for someone, but the information you’ll find here is easily obtainable from the site (or via Google.)  Even at the low price it’s currently listed for, I would not recommend it.   If you are looking for more of a deep-dive into Razor, move along, this book is not for you.

My advice to the author: if you want to make this book useful, you must go further than the content that’s readily available online.  You need to dig deeper and provide something that’s worth the reader’s investment.  Also, understand that the power of Razor is in cleanly mixing C# and markup.  Throwing big blocks of C# with embedded strings on the view fails to capitalize on that power.

About Matt Honeycutt...

Matt Honeycutt is a software architect specializing in ASP.NET web applications, particularly ASP.NET MVC. He has over a decade of experience in building (and testing!) web applications. He’s an avid practitioner of Test-Driven Development, creating both the SpecsFor and SpecsFor.Mvc frameworks.

He's also an author for Pluralsight, where he publishes courses on everything from web applications to testing!

blog comments powered by Disqus