Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Is ASP.NET MVC A Gateway to Ruby on Rails?

DZone's Guide to

Is ASP.NET MVC A Gateway to Ruby on Rails?

·
Free Resource

I have been working primarily with ASP.NET MVC at work but have been doing some small Ruby on Rails stuff in the off hours and the similarities between the two are not hard to see.  There are several MVC implementations out there for multiple languages and each have their own quirks, but I think it is pretty obvious to anyone who has worked with both ASP.NET MVC and Ruby on Rails that ASP.NET MVC is definitely modeled after it.  Which leads me to beg the question, Is ASP.NET MVC a gateway to Ruby on Rails?

It certainly was for me.  I had taken a couple of looks at Ruby on Rails several years ago and it was completely different that anything else I had been doing.  Mind you, I had been doing primarily ASP.NET Webforms development at the time and maybe a little PHP here and there.  I've always been one of those developers who really likes to know what is going on under the covers so I'm not a super huge fan of code generation in most cases.  I'm not fundamentally against code generation, I just think it's really helpful to know what is getting generated and why.  The scaffolding in Rails threw me off at first and without really knowing what I was getting myself in to, I passed on learning it years ago.

I have to admit also, that I didn't take the time I probably should have with Rails back when I first looked at it.  I am always working on learning some new technology, but it's easy to pass on something that doesn't quickly "click" and move on to something else, which is what I did.  While there was already a decent growing community, I hadn't really caught on to what was going on with it.    

I started out web development primarily with PHP so I was used to that style of coding and then did a good bit of Coldfusion and eventually moved to ASP.NET.  I liked the more application-ish feel of ASP.NET, but I never really liked the idea of code behind.  A lot of my first ASP.NET apps actually had all of the code inline like I would do in PHP (Shhh, don't tell anyone.)  As I progressed as a developer I learned important concepts like building layers in applications to provide separation and such so code behind was starting to wear on me.

Enter ASP.NET MVC. It gave me what I was looking for.  I got things like clean separation of logic and presentation, the ability to easily do bare metal HTML and much more.  Once I jumped on the ASP.NET MVC bandwagon, it was hard to go back and do regular ASP.NET Webforms apps.  With all the buzz on twitter about Ruby on Rails, I decided I should take a look.  Several .NET developers I knew and respected were working with it so I figured it was time to give it another look.  To my surprise, Ruby on Rails now made perfect sense, all thanks to ASP.NET MVC.  

While they both are implementations of the Model View Controller pattern, they are similar in structure.  For a project right out of the box, the structure for a Rails app looks like this:



While a new ASP.NET MVC project looks like this:


Both at the root of the application have folders for controllers, models and views.  Which really isn't a big deal in of itself, but since it's the first thing you see coming into a new project, it makes for familiar ground when coming from ASP.NET MVC to Ruby on Rails or vice-versa.

Both make use of the concept of Convention over Configuration, which in a nutshell means that they are using the convention of the class names and files for locating resources rather than a bunch of configuration files.  It's a good thing too, I have a few decent ASP.NET MVC projects that if there had to be configuration files to map it all, it would have been a nightmare.   If I want to have a "Home" controller for the home sections of the site, I create a HomeController class in the controllers folder in my ASP.NET MVC project.  I normally do this right with in Visual Studio since there you can right click in the Solution Explorer and click "Add Controller".  

There is then a folder under Views that you would use called simply "Home" and the framework knows that the views there belong to the HomeController class and any action methods will correspond to those views.  To continue to look at the similarities between the two here is a basic HomeController class for both ASP.NET MVC and Ruby on Rails:

ASP.NET MVC with C#:

public class HomeController : BaseController
{
public ActionResult Index()
{
return View();
}

public ActionResult About()
{
return View();
}
}

Ruby on Rails:

class HomeController < ApplicationController

def index

end

def about

end

end

As you can see, the similarity here is striking.  Another big thing that is rather similar is routing.  In ASP.NET you use can manage routes within the global.asax.cs file and it Rails there is a routes.rb file for route configuration.  The concepts are relatively the same:

ASP.NET MVC Global.asax.cs routing:

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

Ruby on Rails routing:

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'

Without going into the language features here, these are just a few small examples of how ASP.NET MVC and Ruby on Rails are similar and it is that similarity that can make it easier for a developer working with one to look at the other with relative ease.  Obviously there is a great deal of difference between C# and Ruby, but the language difference aside, the MVC framework similarity between the two make the process much easier.  

All that being said, I don't see myself whole heartedly switching from ASP.NET MVC to Ruby on Rails for my normal work.  As a developer, I love learning new things and I think it's an integral part of a good developers life and as such I think my work with Ruby has made me a better programmer.  I would highly recommend looking at languages and frameworks that are similar to what you currently use even if you don't intend to switch to that new language or framework.  Sometimes just seeing how other people do things can have a big impact on how you think and solve problems as a programmer.  I know it has for me. 

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}