How Ruby on Rails Taught me ASP.NET MVC

Before learning ASP.NET MVC, I had the opportunity to learn Ruby and Ruby on Rails. In this post I want to share how Ruby on Rails taught me ASP.NET MVC.

Managing Dependencies

To create a ruby on rails app, let’s type “rails new blog” in a terminal or command prompt. Now that we have a new app, we can see how rails manages dependencies. By running command “bundle install” inside the blog directory, Bundler will install all dependencies inside the Gemfile. It is that simple.

ASP.NET MVC has a similar process. We use nuget to install and manage dependencies. And our Gemfile is named packages.config. We also have a command line to install libraries (Install-Package jQuery).

App Structure

By convention, rails app creates the following directories inside the root app directory:  models, views, and controllers. ASP.NET also creates models, views, and controller but they are created at the root level. Rails gave me a solid understanding on the MVC architectural pattern. Before learning ASP.NET MVC, I knew that models were responsible for the domain’s entities. Views are only concerned with the presentation layer (UI). And controllers manage incoming requests and provide a link between models and views.

Routing

Before learning rails, I was using ASP.NET Forms. With this framework, we were not concern with routing because aspx files were served directly from the file system. There was no configuration done by us. The system just handle it. In the rails world, the route config is located in config/routes.rb. A typical route file might look like:

get "/posts" => "posts#index"
get "/posts/:id" => "posts#show"
get "/posts/new" => "posts#new"
post "/posts" => "posts#create"  # usually a submitted form
get "/posts/:id/edit" => "posts#edit"
put "/posts/:id" => "posts#update" # usually a submitted form
delete "/posts/:id" => "posts#destroy"

In ASP.NET MVC, you will see:


public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name 
            "{controller}/{action}/{id}",                           // URL with parameters 
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

They are basically mapping a request to a controller action. After learning rails, it was very easy to me to pickup this new routing thing.

Summary
I’m always recommending that you learn a new programming language, framework, or tools. In my case, I decided to learn a new web framework called Ruby on Rails. Not only I learned a new programming language but also a new web framework. I never anticipated that by learning rails I was also gaining a solid foundation to learn and use ASP.NET MVC.