October Quickie: A Little Bit of Everything

Well yeah, it started snowing today here in Moscow, but this post is not about snow. I remember I’ve done some quickie before, but couldn’t find it. Perhaps it was something I mentioned inside the blog post with a different title. But anyways, this is a short roundup of what I’ve been up to lately.

Let’s start with Python.. After reading a very popular book called Programming Collective Intelligence by Toby Segaran (review coming later), I had no choice but to peek at how Python is doing. I didn’t carry out much of the excercizes in the book while reading it, but I did run a few experiments afterwards. Some of the code examples were translated into PHP to immediately implement into my Twibots project, the juice was left for later.

This weekend I decided to gather some data from the Twitter Streaming API for some experimental purposes, and I decided to do this in Python. Of course I hadn’t had much time to read good books (still waiting from Amazon), but the plenty online tutorials, guides and articles out there are quite okay. So with a little trial and error I managed to write a script which gathers a little over 10,000 tweets in 30 minutes and dumps them into a database. That’s 20,000 tweets an hour, ~ half a million tweets a day. What am I going to do with those? I’ll give you an insight on this project a little bit later ;)

Twitter. I finally got #newtwitter. I thought it was a joke. It’s not a secret that I have multiple Twitter accounts, but I use them for development purposes, and guess what, those accounts were switched to the new Twitter three weeks before my account was. So yeah, I’m a little mad at Twitter, but their new look really rocks. They also need some updates to their mobile version.

.NET MVC. Some of you might be wondering (or not) what happened to that .NET MVC project of mine. Well it’s 99% finished, but we’re constantly coming up with changes to the content part, switching images around and other dirty work. Latest comment from the client was – okay so I open up Safari, click View and check the Zoom Text Only option. Then I zoom in and the whole design breaks! You guys promised me nice-looking XHTML in Opera, Safari, Chrome, IE and Firefox, what the heck?

PHP. I wrote an app in PHP that allows me to turn off my PC from my iPhone. I extended it to include volume up/down features a few days later. These are the kind of apps you write when you’re lazy ;)

And finally some goodies from all around the web for web designers and developers:

That’s it for today. Cheers!

How To Deploy & Publish a .NET MVC Application

This is the second post in the series called .NET MVC From a PHP Developer’s Perspective where I discuss the pros and (mostly) cons of jumping onto the .NET bandwagon. Keep in mind that I’m a php/unix guy. I’m now at week 5 and I recently deployed my .NET MVC application to a web hosting provider. This was done for a testing and debugging purpose, since I had to become familiar with the whole process.

We begin with picking the right hosting plan for your .NET MVC application, then proceed to deploying your application and the contents (images, javascript, etc). Finally I show you a little trick on how to deploy your database in a format (.bak) most hosting providers will (hopefully) accept.

Picking the Right Hosting Plan

I picked Parking.ru – a Russian Windows hosting provider, which were generous enough to provide me with a two weeks trial period. My choice was based on a few calls to several different hosting providers, their technical teams had no clue about if my .NET Framework 4, MVC 2.0 application with an SQL Express database would run on their hosting plans. The answers were all similar to:

Well we’re not sure, you may try this hosting plan, but we’re not sure. And you still have to pay in order to use it. It’s running Windows Server, but we don’t know what MVC is, so we can’t guarantee you anything, sorry.

Heh, there was always a problem with Russian hosting providers, but overall the service is getting better. Tech teams now at least know whether they’re offering Apache or nginx ;) But anyways, I was quite happy to get a free trial at Parking.ru, and since they had no FAQs on how to publish my MVC application, I ran off to Google.

Publishing Your Application and Contents

It’s not really a big deal after you get it the first time. Visual Studio can automatically publish your application and the contents to a remote FTP server, which in my case worked like a charm. The only prob I had is with the external contents such as images, stylesheets and some javascript, which all sumed up to around 500 files. I didn’t add them as resources to my project, thus they were never published. Some common sense was required here, so I fired up my favorite FTP client and flushed them all manually.

Publishing Your SQL Express Database

The major issue was of course with the database. As I wrote in my previous article, the MVC Music Store is the tutorial my application was based on. The tutorial creates the models from a database scheme, and all the data is stored in an SQL Express file with the MDF extension. My hosting provider supported backups (.bak) which I could upload to their servers, so I googled around for ways to generate such backups.

Most articles I came across mention SQL Server Management Studio, so I installed that, and after a few trial and error, I managed to get my SQL Express database inside the Studio. Now what’s weird is that the backup option didn’t work, yielding something about incorrect filenames. Of course my database was called [C:UserskovsheninDocumentsVisual Studio 2010…], but SQL queries worked quite okay, so did an SQL export which generated an SQL dump of all my data. Unfortunately that was not good enough for my hosting provider.

With a few more experimenting and searching, I came across the following query, which could be issued within Visual Studio itself (does not require the SQL Server Management Studio). It generates a .bak file which is exactly what I was looking for:

BACKUP DATABASE [C:PATHTOYOURDATABASE.MDF] TO DISK = N'C:my-database.bak' WITH NOFORMAT, NOINIT,  NAME = N'My Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10

Honestly, I have no idea what some of the options mean, but I’m sure Google can help. One thing you should pay attention to is the square brackets in the database name, especially if it contains the space character (which it will, if you saved your database in the Visual Studio directory by default). The command could not be parsed by Visual Studio, but the execution generates a .bak file on your hard drive. I instantly uploaded the file to my hosting provider, and voila!

I spent 3-4 hours in total on deploying my MVC application, but now that I got it right, takes me a couple of minutes. Then again, I really miss mysqldump, subversion and the rest. They seem to be a little more lightweight than .NET and IIS.

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!