In the last 2-3 years there were a lot of talking about agile and high-productivity development with an outstanding framework that is already in the legend, like happened to Java in the late 90s: Ruby on Rails.
Given its success, many other project in the Open Source community were born trying to reproduce the beauty of the Ruby on Rails rapid and agile development framework for other environments. Just to name a few: Turbogears for Python and Grails for Java.
The main features of these rapid development framework generally are:
- Convention over configuration Pattern
- MVC Pattern
- Inversion of Control Containers and the Dependency Injection Pattern
- An Object Relational Mapper (ORM) for the data layer management
- Code skeleton generators
I have seen many .NET developers migrating to Ruby on Rails or to others high productivity frameworks, not knowing that also for .NET (and Mono) there is already an outstanding framework for this purpose: the CastleProject.
Even if for this framework there is still not a first stable release (the main components are actually at Release Candidate 2), it offers already an excellent solution for agile and rapid development for the .NET world.
CastleProject is actually composed of 5 amazing Open Source projects (licensed under the Apache Software License) that can be used together or singularly:
- Monorail, an implementation of MVC Pattern (inspired by Ruby on Rails Action Pack)
- ActiveRecord, an enterprise data mapping pattern implemented using NHibernate
- MicroKernel, an inversion of control container implementation
- Windsor Container offers powerful configuration support for common enterprise application needs
- Aspect#, an AOP (Aspect Oriented Programming) framework
Unlike many other Open Source software, the CastleProject’s documentation is already at a very good point (even if there is still not an official Release, but they are very close to it, so it may have been changed when you are reading at this post). If you are in troubles and the documentation is not helping you, you can still post at the forum, a very active community will try to help you.
There are good resources on the web about CastleProject other than its site: the excellent blogs of Hamilton Verissimo, Ayende Rahien and a wiki just to name a few.
Now that I have introduced you at the CastleProject, let’s jump in the object of this post: a tutorial about MonoRail and ActiveRecord. You may have noticed that at the Castle site there are already quick tutorials for each component of the project, and a very nice one for starting with MonoRail and ActiveRecord.
When some weeks ago I started, for an incoming project at my job, learning how to develop with CastleProject, after I did the tutorial at the Castle site I was a bit lost. Before starting the development I wanted to have a more concrete sample application for ASP .NET developed with MonoRail and ActiveRecord, with some more domain classes and more complex relationships of the ones found at the Castle Tutorial.
So I started myself to develope a simple prototype application, for helping myself understanding the Castle stuff, in order to have a more advanced basic application to use as a base for myself and other developers here when starting real applications development.
So I dediced to develop this sample applications starting from the Domain Model of a classic Music Library Applications.
In this post I will describe you the Domain Model, in the next posts I will take you step by step in developing this application using MonoRail and ActiveRecord. Of course, before taking this tutorial, if you are new to the subject please first try the Castle tutorials I was writing about some lines before.
There are several approach with rapid developments frameworks like Castle. Here I will follow a rapid one, but you are free for using other approaches:
- write the domain classes
- autogenerate the database schema
- write the controllers
- write the views
In a real scenario you would probably unit test the domain classes before writing the controllers and the views.
The Music Library Applications is composed by the following domain objects:
- Artists (ex: Nirvana)
- Albums (ex: Nevermind)
- Genres (ex: Rock, Pop, Jazz…)
- Support (ex: CD, Tape, 33, 45…)
The relationships are:
- an artist may have more albums and an album can be created from many artists. This is a n-n relationship between albums and artists
- an album may be of one or more genres. If there are more genres there will be a main one. This is a n-n relationship between albums and genres with an attribute (main genre)
- an album may be on only a media type (support like CD, 33, 45…). This is a 1-n relationship between support and albums
So, here is the Domain Model of the Music Library Applications:
From these Domain Model the E/R Database Diagram will be translated in the following (note that for the Many-To-Many relationships we need the intermediate tables, and one is with an attribute)
Note: someone could argument that there is not need to use an attributive (attribute: main) many2many relationships for the table albumgenre. We could use a simple without attribute many2many and place a maingenreid field in the album table. But I did so for having in this tutorial a sample of a many2many relationship with attribute to have an almost full scenarios of relationships management with ActiveRecord.
In the next posts I will show you step by step how to create the Domain Classes, the Controllers and the Views. Until then you can download the full source and have a preview of what is coming.












Nice introduction to Castle Project.
Since you’re exploring ActiveRecord/NHibernate stack and you’re “thinking in GIS”, I think you’ll love this new project: http://codeplex.com/NHibernateSpatial
Hey Ricardo, looks very promising!
I were thinking about using NHibernate (and Hibernate) with spatial data, and I was wondering if there is such a project like yours, but never had the time to investigate (too much stuff in the stack).
Is NHibernateSpatial a translation of an analogos project for Java Hibernate?
I am definetely looking forward for the PostGIS implementation.
BTW, I know well Diego Guidi, the NetTopologySuite’s developer.
[...] is the second post of a serie started with this post about a MonoRail and ActiveRecord [...]
> Is NHibernateSpatial a translation of an analogos project for Java Hibernate?
No, it began as an experimental version for MsSqlSpatial only, inspired by new features in NHibernate/ActiveRecord, such as projections, and then generalized to support other spatial databases.
In fact, I didn’t know the Java project. I’m browsing the source code right now and there are many similarities, as those derived from (N)Hibernate extensibility framework and some naming conventions, but the approach is different in some key details (for example, how the generic geometry user type gets the current dialect). Anyway, I will give it a deeper review. I’m in particular interested in the Oracle Spatial dialect implementation, maybe I will borrow some code from there
> I am definetely looking forward for the PostGIS implementation.
Check out the source code
> BTW, I know well Diego Guidi, the NetTopologySuite’s developer.
The man deserves a special mention. Without his great work, NHibernate.Spatial (and MsSqlSpatial) could not be possible. Thanks again, Diego!
[...] Thinking in GIS : Castle MonoRail and ActiveRecord Tutorial (Part 1) (tags: MonoRail c#) [...]
[...] Castle MonoRail and ActiveRecord Tutorial (Part 1) [...]
[...] Castle MonoRail and ActiveRecord Tutorial (Part 1) [...]
[...] Castle MonoRail and ActiveRecord Tutorial (Part 1) [...]
>The man deserves a special mention. Without
>this great work, NHibernate.Spatial (and
>MsSqlSpatial) could not be possible. Thanks
>again, Diego!
Thanks a lot Paolo and Ricardo, we’re all trying to do our best to make the “ALT.NET world” a better place