.net and other musings

Ben Lovell, an agile developer living in the UK.

Category: Ruby/Rails

Growl notifications with autotest made easy

Setting up a new project to use growl notification via autotest can be a bit tricky. There is a fantastic gem called growl-glue that can simplify the whole procedure. Simply:

[sudo] gem install growl-glue

Then add the following lines to your .autotest file:

require 'growl_glue'
GrowlGlue::Autotest.initialize

initialize will accept a block containing a stack of different configuration options. Take a look at what is possible here.

MonoRail – rendering partials

You can achieve the equivalent of RoR’s partial support in MonoRail via the following:

  • Place your partial template (.vm) in the views/shared folder
  • Call #parse(“shared/yourpartial.vm”) wherever you want to render the partial content

Note: this above example only applies to the NVelocity view engine.

Simple really!

Ninja EditAyende has pointed out the Brail alternative:

  • OutputSubView(”/shared/yourpartial”)

RoR vs. Web Forms vs. MonoRail Pt2

Ok so in the last post I talked about some of the more obvious issues I have with ASP.NET and web forms development. Thankfully, the Castle Project’s MonoRail framework goes some way to alleviating that pain.

The Positives

MVC, MVP, ABC easy as 123…

Under the covers, MonoRail is a full MVC compliant framework essentially comprising a front controller sitting over the ASP.NET infrastructure intercepting specially formed URI’s. The HTML ends up in the browser via your choice of view engine, with NVelocity (a fork of the Jakarta Velocity port) the default and most accessible, Brail being the Boo dynamic language and most performant option and of course a cruddy web forms engine too which nobody should use. πŸ™‚ I’ve been comfortably using NVelocity for a while now, and provide testament that its not difficult to pick up.

The controller code orchestrates the flow of logic through the application and rails URI’s map nicely to actions on the controller which are ultimately public methods on the controller class itself. All nice and easy. Actions exposed on controllers derived from the magical SmartDispatcherController can translate posted form values into domain model objects for you by simply adding the DataBind attribute to your action’s argument list:

public void Save([DataBind("publisher")] Publisher publisher)

In the above example the publisher argument is passed into the Save action fully constructed via the form or query string values posted! This saves on the usual binding and scraping stuff you normally do in web forms and which almost always ends up sitting in the code behind untested. Note: the databinding stuff is of course very flexible and my example above just touches on the possibilities.

ActiveRecord

The model can be supplied with an ActiveRecord enabled domain model. This of course abstracts away NHibernate behind the scenes. Unfortunately generic collections support doesn’t work right now but I believe that work is firmly underway.

Edit: Generic collections are supported, as long as you’re running from the trunk.

Domain entities are adorned with the necessary attributes defining how their properties are mapped to your data source. This is a departure from the usual hbm XML mapping files but just as expressive.

ActiveRecord also supplies simple validation in the form of special attributes including email validation, regular expression validation and the like.

UI Components

Reusability of UI components is achieved via ViewComponent. These can support nested sections, parameters etc.

AJAX and client scripting support

Built into MonoRail are a number of helpers which allow you to perform the usual AJAX stuff such as calling remote server side methods and callback support, Scriptaculous and Effects2 client side goodness. In fact the helper extensibility is a nice way of adding extensions and wrapping commonly performed functionality in the view.

Convention over configuration

Like RoR, MonoRail prefers convention over configuration. The project structure skeleton is the same throughout all MonoRail enabled solutions. The MonoRail installer also includes a handy templated solution provider for both VS.NET 2003 and 2005 to create the project skeleton and testing is included. Accustomed MonoRail developers can open any solution and just know where things are going to be.

Testability

One of the sore points of ASP.NET development was the unit testing difficulty. MonoRail overcomes this by enforcing the MVC pattern. Controllers are testable classes sitting outside of the usual ASP.NET cruft. There are a number of framework features within MonoRail which specifically aid testing.

Scaffolding

Like RoR, controllers adorned with the necessary scaffolding attributes can automatically produce the basic markup and logic for CRUD operations on your domain model. Handy for prototyping or creating rough-and-ready data entry capabilities in your applications.

Container support

MonoRail supports IoC via the Windsor container. Controllers and their dependencies/parameters can be injected by the container if necessary.

The negatives

MonoRail does have some negatives, which I will mull over:

No 3rd party or ASP.NET web forms controls support

I don’t really see this as an issue but can see that some ASP.NET developers will. Fans of the big UI library packages will complain, but there is this old concept of HTML and it is surprising how flexible it can be πŸ˜‰ Most use of control libraries I have seen has been misguided, by this I mean they’re usually added to a screen just because they can.

The web forms control library isn’t such an issue as things like calendars, grids and the like are not to difficult to duplicate the MonoRail way.

Learning a whole bunch of new concepts/patterns/practices

Most ASP.NET developers are what can only be described as morts. They’re not familiar with design patterns and good practices such as clean separation since they came from either ASP or VB (procedural backgrounds) so this stuff doesn’t come naturally. If you’ve used NHibernate you’ll pick up the ActiveRecord stuff in no time. Learning the template syntax for your chosen view engine is of course another consideration.

Summary

In summary, the benefits of using MonoRail over web forms are clear. Clean separation of concerns, testability, reduced complexity and none of the web form cruft and artifacts. MonoRail won’t suit everybody, but even if you’re happy with web forms it’s definitely worth a look. Personally I think its the only way worth programming in the enterprise with ASP.NET today.

Catching the DLR

DLR announcements abound… Wow, the DLR or Dynamic Language Runtime, is causing quite a buzz across the tech-blogs and with good reason! Expect lots of Ruby goodness to come.

Nice to see that this is an open-source initiative, being licensed under the Microsoft Permissive License. What is it with the big vendors and their individual open-source licenses? If you don’t have your own open-source license you’re officially a nobody nowadays it seems πŸ™‚

Ruby Reference

I’ve been falling back on a simple PDF Ruby language reference lately to help me with that quirky syntax as-and-when I need to. The book is backed up by available sourcecode too. All provided by those nice folks at SapphireInSteel, take a look at the Little Book of Ruby here…

RadRails

I’ve been spending much of my cycles in the RadRails IDE and its a pleasure to use. It’s missing the refactorings and extensibility of full-blown Eclipse but I like the fact that its lightweight. I’ve used Eclipse and it just feels rather sluggish, RadRails is a snappier experience. As with the best things in life, RadRails is of course entirely free.

So too have I been using Ubuntu 7.04 or Feisty as its also known and i’m impressed with the environment, especially when compared to my usual development environment which would usually consist of a VM’d Windows 2003 server and VS.NET in one of its several incarnations.

What’s happening…

Wow has it really been this long since I last blogged…

Anyway, what am I doing? Well i’ve been working on a super-secret and ultra-cool project using the awesome Ruby on Rails framework. I can’t really say much about the project at the moment but rest assured that all will be revealed in the fullness of time πŸ˜‰

Have I mentioned before that I absolutely love Ruby and the Rails framework? Everything just feels right… I feel the measure of a good framework is when things kind of work just how you expect them to and RoR fits this description perfectly. So given my recent deprecation of .NET (only temporary I might add, expect a few Rails related posts… If I ever get round to posting πŸ™‚

RoR quick reference

While we're on the subject of RoR – I've found this Ruby on Rails Reference to be a great help…

Ruby on Rails: Wow, just wow!

I've been digging into the Ruby on Rails framework lately. All I can say is "wow!". The greatest barrier for entry for me so far has been the oddities of Ruby syntax. However, the Rails framework is extremely simple to understand and the benefits are huge, I can really see lots of potential here, particularly for super-rapid prototyping.

Watch this webcast to see a blog engine created in 15 minutes (and a fairly decent workout of the Rails internals) and see what all the fuss is about!