Differences I’ve seen between Rails and ASP.NET MVC

I’ve spent a long time with both Rails and ASP.NET MVC and thought I’d document some of the differences I saw for future reference. Both frameworks can produce nearly identical solutions so as far as the end product of either there’s not much to talk about. There are some subtle differences in how you get to that point.

Rails has a rapid release cycle
ASP.NET MVC releases slower and maintains compatibility

Rails is a full stack framework
ASP.NET MVC requires you to make your own choices

Rails tooling is mostly free
ASP.NET MVC has many great tools locked behind a purchase price

Rails works the same with most databases
ASP.NET MVC has a discrepancy in tooling and favors SQL Server

Rails is powered by Ruby which is a dynamic language
ASP.NET MVC is powered by C# which is a static language

Rails works great in Mac OS or Linux
ASP.NET MVC is meant for Windows

I’m sure there are a lot more but those are the ones that stood out for me. Neither one has reasons not to use them. Each one has ways to get around any perceived deficiencies. Both frameworks are being actively developed and continue to grow and get better. In my mind you can’t go wrong with either one but one might be more suited to a particular project. The best way to figure that out is to use them both and see which fits your current needs.

Rails versus ASP.NET MVC 4 Business Performance

ASP.NET MVC is based loosely off of the ideas found in Ruby on Rails. After using ASP.NET MVC 2 for many projects I decided to learn Rails 3 to get a deeper understanding of what Microsoft was basing its ideas upon. I spent a couple years working with Rails exclusively for web development. The last large project I produced was a student time tracking application. The application allows a student to sign into and out of the academic labs and tracks their total time spent over the course of a semester.

The application worked without problems for a single semester. The next semester saw a dramatic increase in the amount of students passing through the system. This increase caused some hardships and the weaknesses in my application were starting to show.

I spent several months researching solutions, digging through my code and solving problems I found. There was an N+1 problem in the reporting module that was solved using includes in ActiveRecord. That gave a major increase in performance and it seemed that the problems were solved. Then I started to receive gateway timeouts as the database grew each day. That’s when I decided to look at ASP.NET MVC and nHibernate to test its ability to process the data as an alternative to moving all processing to the background.

Initial testing was done on a final semester data set that included all student sessions for that semester. The code generates a detailed report that displays the total time spent by each student for each of their courses. The time required to run this report on both frameworks is below.

Rails 3.2 : 45.3 seconds
ASP.NET MVC 4 : 15.1 seconds

Based on these numbers I decided to completely rewrite the application using ASP.NET MVC 4. I spent several months of nonstop development to rewrite the project. Upon completion I was able to realize an increase of five times the performance of Rails for CPU intensive operations.

Both of these application servers run within the corporate VMware server using the same configurations. RHEL 6.3 was used for Rails while Windows 2008 R2 was used for ASP.NET MVC 4. I am comfortable in both environments and have successful applications running on both platforms. I embrace the Open Source movement even within ASP.NET MVC. For these tests I have tried to remain as unbiased as possible.

After spending considerable time with both frameworks I feel confident that either can provide the same solutions. The only difference comes when you run up against underlying problems with the technology. In this case MRI Ruby is known as being slower at CPU processing than C#. For web applications this is not the normal bottleneck but for business applications it can be depending on the amount of data that needs to be processed in a given unit of time.

Being a developer means knowing which tool to use for the problems you face. Neither of these frameworks have features to completely give up the other for. It is mostly a matter of choosing the right tool for the job. Having spent time writing the same application in both I have seen the good and bad qualities of each of them. I would recommend getting to know both and making your own decision as to which you like. For the work environment ASP.NET MVC 4 seems to be a better fit for me while in my off hours I still enjoy the simplicity of Rails.

DIY git remote repository

I have a secret to tell you.  There was a long period of time where I didn’t use a source revision control system.  *GASP*  Honestly, I mostly work alone so it never seemed like the effort was worth the payout.  I made copies of my code before I did major changes and always had backups.  I did have a brief run-in with SVN and while the idea of having your files watched and backed up as needed was nice, it turned into a hassle when I would move between machines or work offline.

Then I started rails development and git was thrown at me as a defacto standard.  At first I thought of looking for a GUI like I had with SVN but since I move between operating systems pretty regularly depending on where I am, I wanted something I could count on being where I was no matter what.  The command line utility was my choice.  It’s really not too difficult once you get used to it and even now I really don’t use it to it’s full potential as it’s mostly just myself.  But if I want to add a developer or a whole team to a project I can and we can all work seamlessly.

Once you decide to use git you need to have a place to upload your source.  A lot of people will use github and if you have an open source project that’s a great idea because it’s free.  For me, I already had a VPS and wanted to utilize that instead of paying another monthly fee.  Here’s how I got it working on my server.

Just a quick note about the server, my VPS is a Linux server.  I cannot say if this would work on a Windows server but as long as you can get a SSH server and git working there should be no reason it wouldn’t.  I suggest using Linux.

First you create a git user.  Doesn’t matter what you call it, but this will be the user you will use to log in to your server.  I suggest creating a separate user so you can store all your repositories in a fresh home directory.  Then log in to that user and do the following:

To create a new project repository on the server:

First create a directory to contain the code

mkdir myrepo.git

Then change into that directory and create a bare git repository

git --bare init

That’s it!  Now on your development machine we will setup git to push to this server.

I suggest setting up your name and email in git if you haven’t already done so.  You can do that using the following commands:

git config --global user.name "Your full name"

git config --global user.email "your@email.com"

In your project directory do the following:

Initialize the repository

git init

Add all the files to the repository

git add .

Commit the files to the repository

git commit -m "Initial commit"

Now setup the remote repository

git remote add origin gituser@yourserver.com:myrepo.git

[ Replace gituser with your user, yourserver.com with your domain and myrepo.git with your repository directory you create on your server ]

Now push the code to the server

git push origin master

It should ask you for the password for the user you used.  Once you enter it your code will be pushed into the repository.  If everything was setup correctly you should now be able to utilize your own server for housing your source code.

To pull down your code from your server you do the following in your projects folder:

Clone the repository

git clone gituser@yourserver.com:myrepo.git

This will prompt you for your password and then once entered will create the project directory and pull your code down from the server.  This is a new repository ready to go and you can push and pull on it as you need.

There are ways to get SSH to store your password on your development machine so you don’t have to keep typing in your password but I don’t use that mechanism.  For me, I just wanted to use my server to store my files between sites.  I’m sure there are better ways to do it, but this has worked for me for a while and I wanted to document it in case it was helpful to anyone else just looking for a quick and dirty DIY git repository server.

RSpec Scaffolding View Spec Errors in Rails 3.1

Rails 3.1 was released yesterday and I decided to update a large project I was working on to it.  I have that kind of leisurely time table to be able to do that.  I’m kidding, I’m already getting asked when it’ll be done but since 3.1 came out I wanted to take a day and see what I was up against for future projects.

I’ve really taken to the whole testing idea.  I love rspec and have been using it on my projects.  I don’t normally use scaffolding in my apps but I thought since it was a new version of rails that I’d check out what was generated with the scaffold to see if I needed to update my standard way of doing things.  I’m still fairly new to rails so it’s nice to see an “official” way of doing things.

After running four or five scaffold generations I decided to fire up rspec and see what happened.  That’s when I got this ugly failure popping up:

Failure/Error: assert_select "tr>td", :text => 1.to_s, :count => 2
MiniTest::Assertion:
Expected exactly 2 elements matching "tr > td", found 8.

This was in my index.html.erb file and was odd.  Digging into the spec file I noticed that I have four integer fields.  It’s trying to test these four integer fields by using 1.to_s and complaining because it’s seeing 8 instead of 2.  Well, it’s going to see 8 because it stubs two records both containing 4 integer fields all with the same integer.

So my solution to the problem was to edit the spec file and change the integer fields from 1 to sequential numbers.  So 1,2,3 and 4 in my case.  I did this both in the stub_model sections and below in the “renders a list of <model>” block.  Once those were updated the tests all pass and I can move on knowing it’s actually working.

Cross-Platform Rails Editor

I do own a Macbook, I’ll get that out up front.  I understand that the standard image of a rails developer is a person sitting behind a Macbook using Textmate as their editor.  I really wanted to be that person, I tried really hard to do that.  The biggest stumbling block I have is that my job allows me a nearly unlimited amount of PC hardware to play with and very little to no Apple hardware.

I have nothing against Apple or Macs in general.  I’ve owned several Macs through the years and while they’re nice and look great, there’s always some little thing that frustrates me and I walk away.  The last was trying to keep the Macbook running with the lid down.  Apple says that when you close the lid, you’re done.  That’s great but what if I’m not?

I am clearly not the target audience for the Mac and while it does just work, it also does so with a price.  You can’t do things your way.  Like I said, this is great for some people and I’m not knocking that at all.  To some, the price you pay to just be able to use a computer and have it handle everything for you is totally worth it.  For me, I’m a tweaking, gear turning, tech that likes to bend technology to my will.  So while I did find a nice utility to keep the Macbook running when you close the lid, it was just the straw that broke the camel’s back.

So with two PC laptops and various Dells at my disposal I set out to build an environment that I could use to develop in rails 3.  My previous post talked about setting up that environment but once it’s setup you will need an editor to do your work in.  That editor, after much looking, is Sublime Text 2.

Sublime Text 2 is cross-platform and will run on Mac OS X, Linux and Windows.  On top of this the editor is very customizable.  You can tweak it to be the editor you need.  This is great because at first I didn’t think it would work for me as it didn’t auto-pair rails tags in my views.  It’s such a simple thing but I really like that when I type <% I get the ending tag and I want my cursor to be placed next to the open tag.

After much digging and trial and error I was able to put together a fairly simple keymap for myself that might help others in their pursuit of the perfectly tweaked editor.  This is my user keymap:

[
{ "keys": ["alt+/"], “command”: “move_to”, “args”: {“to”: “eol”, “extend”: false} },
{ “keys”: ["alt+,"], “command”: “move”, “args”: {“by”: “characters”, “forward”: true} },
{ “keys”: ["alt+."], “command”: “move”, “args”: {“by”: “wordends”, “forward”: true} },
// Auto-pair Rails erb tags
{ “keys”: ["<", "%"], “command”: “insert_snippet”, “args”: { “contents”: “<%$0 %>” } }
]

Note: For Mac OS X use super instead of alt.

It’s simple and the auto-pairing is done for me, not as nicely as brackets or quotes but it works for me.  The alt key commands are what I came up with to allow me to easily move around my code without the need to remove my hands from the keyboard.  The one I use most is ALT+. as it allows me to just quickly move to the ends of words when typing commands such as link_to.

It’s a great editor and actually supports textmate snippets and has so much power under the hood that I’ve barely even touched.  It’s priced the same as Textmate and it’s definitely worth it.  You get cross-platform use, textmate snippets, the power to tweak the editor to your liking and so much more.

Install Rails 3 on Ubuntu

So the first thing you need when you decide to start rails development is a development environment.  I personally own a Macbook and while there seems to be this notion that rails developers all use Macs I still haven’t found a tactful way to run a purchase requisition through for one at work.  So being stuck with PC hardware the choice was Windows or Linux.

Windows actually can be used to develop rails applications and I did that for a week or two until I got the itch to put Linux to use.  Que about a week of lost productivity as I ran through all the distros looking for something I could use daily at work.  Surprisingly I was unable to find anything to replace Outlook and since it has become common for people to email me change requests or project ideas I really need a stable communication line with the Exchange server.  Yes, I could use the web client but when you’ve used Outlook for years you get used to being notified of new mail and having offline abilities available.

So without a replacement for Outlook easily available, I decided on the next best thing:  virtualization.  I have a license for VMware Workstation and it does have nice features like dual monitor support but all I need is a quick, light environment to work with.  Enter VirtualBox.  It’s open source and free to use so I can load it up on my computers at home or at work.  I picked XUbuntu for the distribution as it was light on resource usage and still has everything I need to feel at home.

With XUbuntu running on a fresh install the following steps will get you a rails development environment up and running in no time.  Since I compile ruby from source I need to install the development libraries and dependencies needed to do that.  The following is all one command:

apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libpq-dev

Once apt is done installing you can download the ruby source at http://www.ruby-lang.org/en/downloads/  You’ll want the latest 1.9 source which at this time is 1.9.2 p290.

Extract this to a folder and run the following command:

./configure && make && make install

Once ruby is installed you can check by running:

ruby -v

and it should display the version you downloaded.  Now we need to update the rubygems.

gem update --system

This will install the latest version of rubygems on the system.  Now we can begin to install the rails environment.  Run the following commands:

gem install rake

gem install rails

gem install pg

The last one will install the PostgreSQL library which is what I use, you can change it to whatever database you use.

After this I like to do a full update of all the gems.

gem update

This will update all the gems on the system but be aware that this WILL install new versions of rails or any other gems you have loaded if you run this command later.

You should now have a running rails environment ready for you to play with.