Castle Demo AppMonoRail At A Glance

time to read 20 min | 3958 words

Okay, so far we saw that MonoRail can be used to serve static files just as well as a 1993 Gopher Server. Considerring that Gopher is dead, we might want to add some stuff to our application. But allow me to give you a very short overview of the way MonoRail works.

MonoRail has Controllers and Views. A controller is a normal class that inherit from the Castle.MonoRail.Framework.Controller class. When a request comes in, MonoRail analyze the URL and dispatch the request to the proper controller method. This is important, unlike in ASP.Net web forms, where the smallest piece you handle was the Page, here the smallest piece is a controller's method. This has various implications on the way you structure you application, but we will touch those a little later.

Controllers are usually named [DescriptiveName]Controller, and any public method without parameters is an action. In the case of our previous example, the HomeController.Default action matched the /home/default.rails URL. Later we'll see how to use parameterized method and databinding to

Views are used to show information to the user, there is usually one to one match between a view and an action, although several actions can use the same view (or a single action can use several views). The file path of a view is significant, since this is used to match the action to the view. So the view for the HomeController.Default action is: "Views/Home/Default.boo".

Okay, that is enough theory for now, I think. Let's see what we can do with this information. Our goal now is to show the user a list of projects. Because currently the database is empty, we will manually populate it with dummy data, so we can see the results of our actions. Here is the script to create the data:

--Cleaning things that may have remained from our tests earlier

DELETE FROM UsersProjects

DELETE FROM Bugs

DELETE FROM Projects

DELETE FROM Users

 

DECLARE @UserID int

 

INSERT INTO Users

SELECT 'Dummy','Dummy@Foo.com','Unhashed password'

 

SELECT @UserId = SCOPE_IDENTITY()

 

INSERT INTO Projects

SELECT 'Default Project',@UserID

 

INSERT INTO Projects

SELECT 'Second Project',@UserID

Now we can have something real.

We'll start by adding the following references:

  • MythicalBugTracker.Model
  • Castle.ActiveRecord.dll
  • NHibernate.dll

Add the following the HomeController's Default method:

    public void Default()

    {

        Project[] projects = Project.FindAll();

        PropertyBag.Add("Projects", projects);

    }

Edit the Views\Home\Default.boo file so it will look like this:

<p>Projects:</p>

<ul>

<%

     for project in Projects:

           output "<li>${project.Name}</li>"

%>

</il>

We need to add the Active Record configuration to the web.config file. Add the following line to the <configSections> node:

<section name="activerecord"

     type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />

Add the Active Record and Connection String information:

<connectionStrings>

     <add name="MythicalBugsTracker" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\MythicalBugTracker\Data\MythicalBugTracker.mdf;Integrated Security=True;"/>

</connectionStrings>

 

<activerecord isWeb="true">

     <config>

           <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />

           <add key="hibernate.dialect"                 value="NHibernate.Dialect.MsSql2000Dialect" />

           <add key="hibernate.connection.provider"     value="NHibernate.Connection.DriverConnectionProvider" />

           <add key="hibernate.connection.connection_string" value="ConnectionString = ${MythicalBugsTracker}" />

     </config>

</activerecord>

Important: Notice that we set the isWeb attribute to true. This is needed so Active Record would store its state properly. Failing to do these could cause random failures, especially under load.

We are almost set to go, now we just need to initalize Active Record, we do this by adding global.asax file and putting the initalization in the Application_Start event:

<%@ Application Language="C#" %>

 

<script runat="server">

   

    void Application_Start(object sender, EventArgs e)

    {

         Castle.ActiveRecord.Framework.IConfigurationSource source =

            System.Configuration.ConfigurationManager.GetSection("activerecord") as
                    Castle.ActiveRecord.Framework.IConfigurationSource;

            Castle.ActiveRecord.ActiveRecordStarter.Initialize(
                typeof(MythicalBugTracker.Model.User).Assembly, 
                source);

    }

</script>

Now we can go to http://localhost:8080/Home/default.rails again, and see what kind of monster we created. Here is the result:

Projects:

  • Default Project
  • Second Project

It's still not an enterprise ready web site, but we made some steps away from gopher fame.

More posts in "Castle Demo App" series:

  1. (03 Mar 2006) ViewComponents, Security, Filters and Friends
  2. (01 Mar 2006) Code Update
  3. (01 Mar 2006) Queries and Foreign Keys
  4. (28 Feb 2006) Complex Interactions
  5. (25 Feb 2006) CRUD Operations on Projects
  6. (22 Feb 2006) Let There Be A New User
  7. (22 Feb 2006) Updating our Users
  8. (20 Feb 2006) Getting serious, the first real page
  9. (20 Feb 2006) MonoRail At A Glance
  10. (20 Feb 2006) The First MonoRail Page
  11. (19 Feb 2006) Many To Many Relations
  12. (19 Feb 2006) Lazy Loading and Scopes
  13. (17 Feb 2006) Active Record Relations
  14. (17 Feb 2006) Getting Started With Active Record