Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Sergio Pereira

There are no half-solutions because there isn't half a problem

October 2008 - Posts

  • ViewEngines at Chicago ALT.NET

    One of the reasons I've been mucking with NHaml is the upcoming meeting of the Chicago ALT.NET group.

    The plan is to talk about NHaml and Spark (and maybe Brail), comparing equivalent implementations of a simple website written in ASPX, NHaml and Spark. The meeting is on November 12th, see registration link below for details.

    The Different Views of ASP.NET MVC

    6:00 pm
    Pizza and networking time

    6:30 pm
    The default ASPX view engine in ASP.NET MVC is fine and comfortable but as with just about anything in ASP.NET MVC, you can replace it with alternative engines — and there are a few of those available.

    We will be giving an overview of some of the alternative view engines, showing how to install and use them, what brought them about, and why would you use them.

    7:45 pm
    You may want to stick around after the presentation portion of the meeting and take part in our monthly open discussion. The topic is never arranged in advance but it's common that it reflects the content of the presentation.

  • ASP.NET MVC and NHaml

    I started playing with NHaml lately for sheer curiosity. It comes with MVCContrib and that's what I've been using to explore it.

    I'm not completely on board yet that you'll want to write all your views, for all kinds of application scenarios using NHaml. I know someone will probably jump out and say that their entire site was built with NHaml and it's wonderful. I'm sure it can be done, I'm just still wondering if it's more trouble than necessary. On the other hand, if you are starting a new application from scratch (i.e. you are not inheriting any existing template or that sort of thing,) then NHaml can guide you through a different way of thinking about your views and simplify them a lot.

    I might have already said this before, but I'm generally in favor of working as close to the platform as possible and viable. That's why I'm not a big fan of things like Script#, RJS, and Volta. NHaml is almost in the same category but it does bring some nice things to the table and that's why I'm not ready to dismiss it just yet. Just take a look at what our HTML looked like circa 1998 and what it is now with richer CSS and JavaScript.

    As much as I can, I try to separate my (X)HTML from my CSS and my JavaScript. Sometimes it gets to the point that I wonder why I am using HTML to begin with — it's all data and structure. I wonder how long until we have a WikiText or MarkDown view engines.

    NHaml fits well for these kinds of well-separated HTML views. It does away (kind of) with the HTML tags and focus on the structure and meaning of each piece of information. Because it defines structure in a way that works well with CSS, it also works great with jQuery for our JavaScripts. It's unobtrusive JavaScript heaven!

    I think the key to learning NHaml is forgetting about HTML and its tags (or at least don't focus on them). Forget that the page ultimately rendered will be in HTML. For a moment just visualize the areas of the page as meaningful pieces of data: sidebar, lists, headings, article title, article body, author name, navigation tabs, etc, not divs, tables, spans, fieldsets, etc.

    Less Noise More Content

    The most obvious impression you get when looking at a NHaml template for the first time is how skinny it is compared to any tag-based template. Your eyes are used to look for angle brackets to help you understand the structure of the document, but in NHaml the indentation serves that purpose.

    Take a look at a common template to create a grid of products in ASPX.

    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    	<head>
    		<title><%= ViewData["pagetitle"] %></title>
    	</head>
    	<body>
    		<h1>All Products</h1>
    		<table class="grid" id="products">
    			<tr>
    				<th>ID</th>
    				<th>Name</th>
    			</tr>
    			<% foreach(var prod in (IEnumerable<Product>)ViewData["products"])  { %>
    				<tr>
    					<td><%= prod.ID %></td>
    					<td><%=  prod.Name %></td>
    				</tr>
    			<% }  %>
    		</table>
    		<input type="button" value="Say Hi" onclick="alert('Hi');" />
    	</body>
    </html>
    

    Now take compare that to the NHaml version.

    !!! XML
    !!!
    %html
      %head
        %title= ViewData["pagetitle"] 
        %body
          %h1 All Products
            %table.grid#products
              %tr
                %th ID
                %th Name
              - foreach(var prod in (IEnumerable<Product>)ViewData["products"])
              %tr
                %td= prod.ID
                %td= prod.Name
            %input{ type="button", value="Say Hi", onclick="alert('Hi');"}/
    

    Never Forget a Closing Tag Again

    Having meaningful indentation brings the common advantage of needing to explicitly mark the end of a block, making that automatic. In other words, the closing tags are added at the right places for you. That's something I value a lot.

    Identifiers and Selectors, Css-Friendly

    By now, after looking at the above NHaml sample a few times, you probably noticed that the class and id attributes are set using a dot and a # sign, respectively.

    %table.grid#products

    Not by coincidence, that's the same type of syntax you'll find in CSS selector rules. Another plus for NHaml here, one less syntax to be memorized — or eventually mismatched.

    Server Side Code/Tokens Syntax

    Server side statements are simply prefixed with a dash and a space. The code blocks are also delimited using indentation (notice the lack of curlies in the foreach loop.)

     - foreach(var prod in (IEnumerable<Product>)ViewData["products"])
     %tr
       %td= prod.ID
       %td= prod.Name

    I'm a little bit uneasy with this code formatting but, to be fair with NHaml, I haven't played with that aspect of it enough yet.

    Let's Throw jQuery in This Mixture

    Oh, about those CSS selectors I mentioned above. That goes very well with jQuery as well since jQuery uses CSS selection rules as well. That's one more reason NHaml can help you keep the HTML/CSS/JavaScript triad separated.

    More to come (hopefully)

    I plan to explore it a little more, especially with MVC in mind, and I'll try to get to some conclusion in a later post.

  • Performing Code Katas

    I just came back from the first meeting of the Software Craftsmanship Group. Tonight Micah Martin talked about Code Kata but added an extra facet to it.

    Micah proposes that, although practicing the Katas by yourself, in your spare time, is valuable, presenting your routine to an audience can deliver even more results. For the presenter there is the opportunity to gain feedback from the audience (peers, masters, even pupils.) For someone watching there's the chance of seeing another peer (or even a master) in action and learn how other developers approach problems and construct their software. By the way, it takes a non trivial amount of courage to sit in front of an audience and write code, even if you have practiced it several times beforehand.

    To demonstrate this concept, Micah created a Ruby program that implemented Langton's Ant. I'll try to illustrate the results of this experience.

    The audience - me (and many others)

    Once Micah explained what the problem was, he asked that we watched him code and be prepared to evaluate his performance (quality, smoothness, clarity, etc.)

    Although I know a little bit of Ruby, I'm by no means a proficient Ruby developer yet. Seeing someone that works with the language all the time in action would be interesting no matter what. But there was more in it for me.

    Micah, as a true BDD pratictioner, started by creating the specs with RSpec and proceeded with the red/green/refactor iterations until he achieved a complete successful specification execution.

    There's nothing like seeing a technology at work to understand it better. Tonight's performance contributed a lot for my BDD understanding.

    The presenter

    After the presentation we had to rate it (0 to 10) and give some feedback. There's where the other Ruby and BDD ninjas in the room could make educated comments about Micah's performance and average joes like myself could comment on less sophisticated issues like font size and keystrokes.

    Judging by the constructive feedback, I'd imagine the presenter's future performances will be fine tuned and get better. On that note, the suggestion is that the presenter moves on to a different Kata for each performance.

    Buncha' Links

  • New Software Craftsmanship Group

    It's so rare to find interesting group meetings up here in Suburbia that I can't pass the chance to attend new ones.

    As Micah Martin announced, the Software Craftsmanship Group was created and the first meeting happens soon.

    One of the topics for the evening will be Ruby Kata. Should be fun.

  • Chicago ALT.NET - The aspects of AOP

    The October 2008 meeting of out user group will be held next week, Wednesday the 8th. The chosen topic for this month was AOP.

    Core: An Aspect Oriented Business Objects Framework

    6:00 pm
    Pizza and networking time

    6:30 pm
    Learn about aspect-oriented design patterns and how they can be used to quickly add common functionality to your business objects. Josh Heyse explains how Aspect-Oriented Programming allows for the separation of true business logic and the code written allowing interaction with user interfaces. The Core framework is a generation model that dynamically adds common services, such as logging, auditing, persistence, and security to business objects. Aspects, or behaviors, are requested using attributes or configuration files which allows services to be included only where necessary eliminating overly bloated objects and tailored for the environment into which the object is loaded.

    7:45 pm
    You may want to stick around after the presentation portion of the meeting and take part in our monthly open discussion. The topic is never arranged in advance but it's common that it reflects the content of the presentation.

More Posts

Our Sponsors

Red-Gate!

Proudly Partnered With