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:

Response.Write(ViewData["my-key"].ToString());

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!

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

3 comments