Home

Castle Stronghold

ActiveRecord Scaffold

This support still on its early stages and has been refactored twice. Nevertheless it's quite good to create prototype applications in no time. Its implementation allow you to override the views if you want, so the scaffolding will handle only the CRUD.

Using it

We are now assuming that you are familiar with ActiveRecord to some extend. Ising the Scaffolding in a web project that happens to be using ActiveRecord already is absolutely easy.

The required assemblies are:

And the ActiveRecord assemblies:

There is no additional configuration required. The configuration below if for those that jumped into this page and have never ever heard of ActiveRecord:


<configuration>

    <configSections>
        <section name="activerecord"
                 type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
        <section name="monorail"
                 type="Castle.MonoRail.Framework.Configuration.MonoRailSectionHandler, Castle.MonoRail.Framework" />
    </configSections>

    <monorail>
        <controllers>
            <assembly>TestScaffolding</assembly>
        </controllers>
        <viewEngine 
            viewPathRoot="views" 
            customEngine="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity" />
    </monorail>

    <system.web>
        <compilation defaultLanguage="C#" />

        <httpHandlers>
            <add verb="*" path="*.rails" 
              type="Castle.MonoRail.Framework.MonoRailHttpHandlerFactory, Castle.MonoRail.Framework" />
        </httpHandlers>
        
        <httpModules>
            <add name="monorail" type="Castle.MonoRail.Framework.EngineContextModule, Castle.MonoRail.Framework" />
            
            <add name="ar" type="Castle.ActiveRecord.Framework.SessionScopeWebModule, Castle.ActiveRecord" />
        </httpModules>
    </system.web>

    <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="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" />
      </config>
      
    </activerecord>

</configuration>

ScaffoldingAttribute

Now you can create a controller (or use an existing one) and add the ScaffoldingAttribute pointing to the an ActiveRecord class. Suppose you have the following an ActiveRecord class:


using Castle.Components.Validator;

[ActiveRecord("Blogs")]
public class Blog : ActiveRecordValidationBase
{
    private int _id;
    private String _name;
    private String _author;

    [PrimaryKey(PrimaryKeyType.Native)]
    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }

    [Property, ValidateNonEmpty]
    public String Name
    {
        get { return _name; }
        set { _name = value; }
    }

    [Property, ValidateNonEmpty]
    public String Author
    {
        get { return _author; }
        set { _author = value; }
    }

    public static void DeleteAll()
    {
        ActiveRecordBase.DeleteAll( typeof(Blog) );
    }

    public static Blog[] FindAll()
    {
        return (Blog[]) ActiveRecordBase.FindAll( typeof(Blog) );
    }

    public static Blog Find(int id)
    {
        return (Blog) ActiveRecordBase.FindByPrimaryKey( typeof(Blog), id );
    }
}

You can then create a BlogsController like this:


[Scaffolding( typeof(Blog) )]
public class BlogsController : Controller
{
}

Now point your browser to your controller, and to an action called list.rails like

http://localhost/blogs/list.rails

How it works?

The scaffolding support basically relies on Dynamic Actions to do its magic. It adds dynamic actions to your controller based on the name of the target ActiveRecord class. Some of the virtual actions might use a view, and if so you are allowed to provide your own views instead of letting it generate the html for you.

Here is a list of the actions added dynamically:

If more than one ScaffoldingAttribute is associate with a controller, then the actions will have the entity name:

Listing

The image below is the default list rendered if you have used the stylesheet and layout mentioned before:

If you want to supply your own view, add a file name list.vm on the view folder for the controller. The following data will be available to your view on the PropertyBag:

Adding

The image below is the default new entry page rendered if you have used the stylesheet and layout mentioned before:

If you want to supply your own view, add a file name new.vm on the view folder for the controller. The following data will be available to your view on the PropertyBag:

Editing

The image below is the default edit entry page rendered if you have used the stylesheet and layout mentioned before:

If you want to supply your own view, add a file name edit.vm on the view folder for the controller. The following data will be available to your view on the PropertyBag:

Removing

The default rendered page will ask for confirmation on the removal. If you want to supply your own view, add a file name confirm.vm on the view folder for the controller. The following data will be available to your view on the PropertyBag:

Complex models

Complex models should be supported. For example, the following is a page rendered for a Person type that uses inheritance and nested types (what NHIbernate calls Components)

Google
Search WWW Search castleproject.org