A 60-Day Trip from PHP to .NET MVC

This is quite an unusual post I guess, something that came in to my mind a few days ago. If you’re familiar with my biography you know I’m in love with php, unix and the open source world. The reason I’m writing this post (or maybe set of posts) is that I found quite a good client, who’s very strict about technology, so the very basic website that they’re looking for should be written in .NET in a little less than two months.

I haven’t finished the whole project yet, but I’d like to give you a short overview of .NET MVC from a PHP developer’s perspective, as well as share some coding thoughts in general. Don’t treat me as a Microsoft hater though, I love what they’re doing, and there are a bunch of very rich web applications driven by Microsoft’s technology! But since I’m used to what I’m used to, I feel a little bit weird and disappointed.

Why did I pick MVC? Well, perhaps because it’s quite new compared to the rest, and a bit of a challange since I don’t have any friends familiar with this monster. So the only places to find help are Google and Stackoverflow. MSDN is pretty much useless, especially the localized versions.

There are several reasons why I called .NET MVC a monster, and here’s a short list based on the first few weeks of my experience:

  • Too many abstractions (week 1)
  • No SQL code at all (week 2)
  • Models are built using drag ‘n drop (week 3)
  • Name conventions are silly (week 3)
  • Confusing (week 4)

Getting used to LINQ

I started my trip by exploring the MVC Music Store Tutorial on the ASP website, which gives quite a clear overview of how to setup a basic website structure, handle some user input and work with the database using LINQ. Wow, I never thought SQL was so complicated that they had to reinvent it. I’ve seen some abstractions over SQL (PDO, Zend_Db) but LINQ is something different:

var shop = (from s in db.Shops where s.Slug == id select s).First();
var workingHours = (from m in shop.ShopMetas where m.Key == "working-hours" select m.Value).ToList();
var comments = (from c in db.Comments orderby c.PublishDate descending select c).Skip(page * 5).Take(5).ToList();

Third line shows my implementation of pagination on a page full of comments. Groups and joins are even more “fun”. I came across a handy resource for studying LINQ called Hooked on LINQ with an awesome section called 5 minute overviews.

But anyways, quoted from Wikipedia: Some benchmark on simple use cases tend to show that LINQ to Objects performance has a large overhead compared to normal operation.

Haven’t tested, haven’t benchmarked, maybe it’s different. But from what I’ve seen, same old SQL is under the hood. Now, a few words about models.

Building Models and Relations

We all know MVC doesn’t go around without models, yet, I never thought models could be designed using only your mouse! I was quite surprised when I saw the models being generated from a database schema in the Music Store tutorial, yet, that’s not as deep as it goes. More interesting is inheritence, mapping, foreign keys and relations, all this using only your mouse! I felt kinda dumb, really. For a second I thought this was programming for kids, but hey, it really makes life simpler! Kudos to Microsoft ;)

Visual Studio Data Model

Worth mentioning is that there’s a flag called Singularize model names when building models from a database schema, which converts the Shops table to a Shop model, a Comments table to a Comment model, (quite cool this one) a News table to a News model, and a (surprise) Movies table to a Movy model.. Whaa? ;)

Next is something that took me quite some time to sort out, so to save you that time, I’ll give you a quick tip.

The Master View

Master views are cool, but according to Google, one of the most frequently asked questions by .NET MVC newbies is “how on earth do I pass data into my master view”? Yup, I asked that question too, but didn’t get too much responses. So here’s a newbie response (how I see it): Master views are very similar to normal news, so they need data passed by a controller. You can go ahead and pass the same data for the master view with each and every controller that you create, but it would be difficult to manage, you need something more centralized.

So the answer would be to create an abstract class (OOP, remember?) which would derive from the standard Controller class, then change your controllers to derive from your newly created class. Easy as that:

public abstract class ApplicationController : Controller
    public ApplicationController()
        // This is how you pass some data to your master view
        ViewData["my-key"] = "my value";

Then make sure you derive your controllers from the new ApplicationController. Here’s my Home controller:

public class HomeController : ApplicationController
    public ActionResult Index()
        return View(viewModel);

Note that I dropped the namespace for both code pieces of code to make them shorter. The namespace used is MyApplication.Controllers. Let’s switch over to the actual Master view and here’s how you capture the data:


That wasn’t very difficult, was it? You can pass all the data/objects you want in ViewData as well as run LINQ queries inside the Application Controller for your sidebar widgets, menus and other stuff that comes from the database.

Well folks! That’s about it. I’ll keep you posted on my experience in .NET MVC but honestly, I’m getting tired of it already, can’t wait to ride home and get my hands back onto php ;) Thanks for reading and sharing!

Foller.me Got Coverage On Mashable!

The first thing I do when I wake up on an ordinary day is check my e-mail and Twitter, and today was an ordinary day until I met some strange messages regarding Foller.me in my inbox. Why is everybody suddenly so interested? Turns out that this day is not ordinary at all. Yes, Foller.me gets covered by Adam Ostrow at Mashable! The post became part of the Mashable’s Spark of Genius series via the Microsoft BizSpark programme. I came accross Microsoft BizSpark a few months ago but I weren’t too excited as all they had to offer was based on Microsoft technology while Foller.me is based on open source software.

But anyways, thank you Mashable and thank you Microsoft. We’re having a big party tonight!

Update: Due to the heavy traffic from Mashable and Twitter today, some profiles on Foller.me today turned out as “Not found”. I’m not quite sure about the technical reasons but I’m investigating certain profiles and prepairing to write a fix to make sure it doesn’t happen again. Sorry for all the inconvenience caused. After all this is my first ever Twitter app! It’ll get better, I promise!

Followup: Foller.me: The 404 Issue

Benchmarking: Your Web Hosting is Not That Perfect

Today I realized that the VPS I’m renting for $20/mo is not as good as it seemed at first. Ever thought about high loads? Okay, this may sound like some DDoS hacking tools, but no! 100 requests with 10 simultaneous made my virtual private server think for ~ 1,5 minutes. Jeez!

It took me quite some time to find good software for running some load tests on my webserver, linux has some good utilities (linux.com/feature/143896), but I suggest you start from ApacheBench which is a command line utility bundled with the Apache distribution. It’s cross-platform, therefore you can use it on Windows (I did). Anyways, here’s how you launch a test:

ab -n 100 -c 10 http://www.microsoft.com/

Why did I pick Microsoft? Well, if I get like 10,000 views tomorrow and everybody tries that command, that’d be a DDoS attack on Microsoft servers and I think they’re good enough to handle it. My server would just explode :)

Anyways, take a look at what the results may be like:

Benchmarking www.kovshenin.com (be patient).....done

Server Software:        Apache/2.2.8
Server Hostname:        www.kovshenin.com
Server Port:            80

Document Path:          /
Document Length:        84 bytes

Concurrency Level:      10
Time taken for tests:   90.984 seconds
Complete requests:      100
Failed requests:        1
   (Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Write errors:           0
Non-2xx responses:      100
Total transferred:      36564 bytes
HTML transferred:       8674 bytes
Requests per second:    1.10 [#/sec] (mean)
Time per request:       9098.438 [ms] (mean)
Time per request:       909.844 [ms] (mean, across all concurrent requests)
Transfer rate:          0.39 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   15   3.4     16      16
Processing:  2203 8866 8879.2   6188   48797
Waiting:     1969 8532 8664.9   5891   48750
Total:       2219 8880 8879.6   6203   48813

Percentage of the requests served within a certain time (ms)
  50%   6203
  66%   7281
  75%   8141
  80%   8313
  90%  17078
  95%  32266
  98%  43813
  99%  48813
 100%  48813 (longest request)

Ah.. And a failed request there, how sad… You might also want to check out your load on the server while benchmarking. Use the ‘top’ command, it should produce similar output:

Yup, although the super cache plugin is working, wordpress consumes a lot of memory… I also ran this with a 500/100 requests, that made my server go down for like 6 minutes, I had over 200 failed requests and my blog kept saying database connection error until the test had finished. Free memory dropped down to 0! Scary? For more information about how ab works, read Apache HTTP server benchmarking tool documentation at apache.org.

Windows Live Writer

The Pro Web 2.0 Mashups book I wrote about earlier is going great and I finally got to the blogging chapters. Standalone blogging, yeah that was a news for me. I mean it is obvious that offline/standalone blogging is applicable to WordPress and other blogging systems, and that’s one of the main reasons why the XML-RPC protocol is supported by them, right? Well I just didn’t see the benefits of offline posting.

Actually, this is my very first offline post, so I’m not so sure that it’ll come out safe and sound, but I really do trust Microsoft in some ways, although I’m in love with Fedora Linux. Anyways, Microsoft Windows Live Writer seems great and has lots of cool features. It’s not just a plain-text editor that’ll post through XML-RPC, so I suggest you check it out: http://get.live.com/writer/overview. It supports most of the blogging systems that we use today.

I’m currently experiencing problems linking it to my russian weblog, maybe it’s because I self-customized the theme (losing some meta tags), or just no wordpress 2.6.2 support. I’ll be sure to upgrade and check that out next week.

UPD 5 minutes later: Yup yup! ;)