Monorail #1 : Reasons, Setup and First Output

UPDATE: I am using the Castle MonoRail project trunk code, NOT RC2

REASONS:

I’ve had a few friends ask me what is MonoRail and how do I set it up?  I decided to go from the ground up here.  There is nothing here that you can’t get from the Castle website documentation.  I just want to grok this stuff and like Mr. Torvalds says, “Talk is cheap, show me the code“.  I also want to learn how to unit test in this environment.  I’ve heard the great things about Ruby on Rails (RoR from now on), but want to make that migration slowly.

Please don’t laugh if this is too beginning for you.  I will eventually get to some advanced topics like:

image

and much more after the scar tissue starts to gather (one mentioned below).Please be patient with me and if you are using MonoRail in production, please make sure I stay on the correct path (JoeyDotNet, Scott Bellware and the Dovetail team, and Billy McCafferty are a few that I know of).

Now we begin.

SETUP:

My current main folder structure in my source control (Subversion) is like so:

image

I also use Ankh for my Subversion plug-in in Visual Studio 2005 (hence the green checkmarks in the image above and future images.)

I keep all my dependent libraries in the lib folder (yes, Jason, we’re not morons…I know)

Once the default structure is setup, you need to setup your folders so that MonoRail will work properly:

image

Notice the folder structure:

Project Root
+Content
—–css
—–images
+Controllers
+Models
+ViewComponents (future post)
+Views
—–components
—–(matching controller named folders containing vm files)
—–layouts (next post)
—–rescues (next post)

This is all provided on the Castle website documentation.

I am currently using shared hosting (WebHost4Life) and it has served it’s purpose.  I recently posted about how they added the .rails extension to the server so that I can feel more legitimate about using rails than having to use a different extension, explained here and in my previous post.  The installation instructions on the Castle website documentation are very user friendly.  I personally took the sample solution and started from there.  It worked great.  Just build it locally and upload your web project to your web host.  I personally check into a subversion repository and use CruiseControl.NET for my automated continuous integration and during that process my NAnt script handles deployment.

I’M NOT A DESIGNER:

No matter how hard I try to design sites, I can’t make them appealing to the human eye.  I LOVE Open Source Web Designs.  It has over 2000 designs and gives me a place to start.  The site I’m using, JasonMeridth.com, for this example is create by Node Thirty Three (I firmly believe in giving the designers their dues) and know it will do it’s job.

NOTES TO PAY ATTENTION TO:

If, sorry, when you setup your Test projects make sure to reference the correct assemblies needed for unit testing with MonoRail.  They are Castle.MonoRail.Framework, Castle.MonoRail.TestSupport, and my unit testing framework of choice is NUnit, so I reference nunit.Framework.  Also don’t forget to reference the project you are unit testing.  In this case it is my JasonMeridth project.

I’m still trying out the abstract base test class that the Castle group has created, AbstractMRTestCase. 

TIP: Currently I’ve learned that if you do the following (This is only a serious rough draft, please no comments on test names or formatting):

   1: using System;
   2: using Castle.MonoRail.TestSupport;
   3: using JasonMeridth.Controllers;
   4: using NUnit.Framework;
   5:  
   6: namespace JasonMeridth.Tests.Controllers
   7: {
   8:     [TestFixture]
   9:     public class ExampleControllerTests : AbstractMRTestCase
  10:     {
  11:         private ExampleController exampleController;
  12:  
  13:         [Test]
  14:         public void Setup_context()
  15:         {
  16:             exampleController = new ExampleController();
  17:         }
  18:  
  19:         [Test]
  20:         public void Controller_should_not_be_null()
  21:         {
  22:             Assert.IsNotNull(exampleController);
  23:         }
  24:  
  25:         [Test]
  26:         public void HelloWorldAction()
  27:         {
  28:             DoGet("example/helloworld.rails");
  29:  
  30:             AssertSuccess();
  31:  
  32:             AssertReplyContains("Hello World");
  33:             AssertReplyContains(DateTime.Now.ToLongDateString());
  34:         }
  35:     }
  36: }

you will get a NullReferenceException:

image

All I had to do was change the SetUp attribute to a test attribute.  I will pursue this further, and I imagine it has something to do with the fact that the controller is instantiated before the view, but that is just a guess. [Stupid moment being recorded in my blog, future update coming here when someone comments and educates me :)]

FIRST ATTEMPT AT OUTPUT:

The helloworld.vm file:

   1: <p>
   2:  
   3: <div class="post">
   4:     <div class="header">
   5:         <h3>JasonMeridth.com<sup>1.0</sup></h3>
   6:         <div class="date">$today.ToLongDateString()</div>
   7:     </div>
   8:     <div class="content">
   9:         $message
  10:     </div>            
  11: </div>
  12: </p>
The ExampleController.cs:
   1: using System;
   2: using Castle.MonoRail.Framework;
   3:  
   4: namespace JasonMeridth.Controllers
   5: {
   6:     [Layout("default"), Rescue("generalerror")]
   7:     public class ExampleController : SmartDispatcherController
   8:     {
   9:         public void HelloWorld()
  10:         {
  11:             PropertyBag["message"] = "Hello World";
  12:             RenderView("helloworld");
  13:         }
  14:     }
  15: }

http://www.jasonmeridth.com/example/HelloWorld.rails

image

The header of my site is a picture of my 2 year old son standing in the underwater view of the Dolphins at the Texas State Aquarium in Corpus Christi, Texas.
The side photo is me beginning Meridth 2.0 training. He loves Tux Racer. :)

Next Post is on Layouts & Rescues (Exception handling pages).

 

kick it on DotNetKicks.com

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jason Meridth

Continuously learning software developer trying to not let best be the enemy of better
This entry was posted in castle, monorail. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

6 Responses to Monorail #1 : Reasons, Setup and First Output

  1. Good stuff. This should definitely help more folks to start using MonoRail. I’m currently on my 3rd “professional” MonoRail project and I can’t ever imagine having to go back to “classic” Web Forms.

    Also, regarding unit testing, you’ll definitely want to check out the BaseControllerTest class (assuming you’re working from the trunk, which I *do* recommend). BaseControllerTest is a much better way to drive out your controllers via TDD and I’ve used it successfully on my last 2 projects.

    Funny you mention RoR, because I’m actually in the process of learning Ruby (and Rails) on the side and I’ve just been blown away by it. I love the simplicity of MonoRail, but it truly is held down by being tied to the static languages of .NET. From what I’ve seen so far, that beauty in simplicity in RoR is 10-fold of what we can get with MonoRail on .NET.

    Anyways, btw, thx for the tip/link to Open Source Web Design. Hadn’t seen them before. Looks like a great place for us creatively-challenged folks to get the design ideas bumping around in our heads.

  2. Awesome!! Thanks Joey, I will check that BaseControllerTest class. I’m using the trunk. :)

    I will head down the RoR path soon, but baby steps for me.

    I look forward to any RoR posts from you in the future (hint, hint) :)

    OSWD is awesome. Glad I could get that out there.

  3. I have to agree with Joey here. I have done about three projects with Windosor/MonoRail as well and I couldn’t imagine going back to WebForms. Someone would have to drag me kicking and screaming =)

  4. I’m starting to feel the same way with just the little I’ve done so far. I may be tapping into your knowledge later Sean.

    Thanks for the info.

  5. Joe says:

    Hey man, looks like a good post. You beat me to the punch, I’ve been working on a Monorail introduction myself. As for unit testing, that is actually no longer the recommended way to test. Check out this page http://using.castleproject.org/display/MR/TDDingControllers that shows the new recommendations for TDD with MR. The old way (inheriting from AbstractMRTestCase) is meant to be used internally.

    Keep up the great posts!

    Joe

  6. @Joe

    Thank you for that link.

    I appreciate the support. Post about MonoRail anyway bro, another perspective is always good.