Lemiffe Music, Software, Stories & AI

My initial experience with ASP.NET MVC3 and Razor

A couple of weeks ago I was asked to develop a mobile application with data access in ASP.NET.

Naturally, I did a bit of research, tried out a few things, and finally came to the conclusion that .NET framework 4.0 was the best choice.

So I got down straight to development, using Jquery Mobile Framework, ASP.NET with .NET 4.0,  MVC3, ADO.NET Entity Framework 4.1, and ASPX (web forms).

It was easy enough to set up, but when it came down to implementing user controls, I found it a nightmare. Passing parameters to the user control whilst using a MultiView was giving me a headache.

The issue here is that web forms are not naturally set up to work via AJAX calls, and Jquery Mobile Framework relies entirely on AJAX calls. So I decided to try out the Razor layout engine instead of web forms.

I had no experience using the Razor layout engine, but after a couple of hours, I found it delicious to use. I could code up pages much faster than in web forms. And the integration with controllers and models in the MVC3 framework was just, natural, it all fit into place beautifully.

Naturally, I found a few issues learning the basics of the MVC3 framework, forms authentication, the Razor layout engine, and accessing data from a database. Tutorials seemed to be all over the place, but I came across a few good ones which I will share with you.

If you are getting started with MVC3 + Razor + Data access (using DBContext in MVC3), follow these tutorials, and you’ll be a ‘pro’ in a couple of days (providing you have enough experience).

Razor Layout Engine:

Learn the basics of the Razor layout syntax:

http://msdn.microsoft.com/en-us/gg618477

Learn how “sections” work in Razor:

http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

Learn how to implement partial views in Razor **(which can be used as ‘user controls’):**

http://rachelappel.com/razor/partial-views-in-asp-net-mvc-3-w-the-razor-view-engine/

MVC3:

Learn how a basic MVC3 application works in .NET:

http://www.asp.net/mvc/tutorials/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript

Learn how to integrate data access (database first) using MVC3 and EF 4.1:

http://msdn.microsoft.com/en-us/data/gg685489

(If you want to create models, but use a MSSQL database that is not on your machine, and you don’t want to have it in the App_Data folder, just follow the steps, then delete the database from the app_data folder, and change the data source in the web.config to point to the correct location).

Writing your own queries to access the database instead of relying solely on those from the model:

http://blogs.msdn.com/b/adonet/archive/2011/02/04/using-dbcontext-in-ef-feature-ctp5-part-10-raw-sql-queries.aspx

So that’s it for now, I thought it would be good to have a central location for these tutorials as they served me well whilst I learned the basics of MVC3 + EF4.1 + Razor, so I hope they are useful to you as well.

If you have any queries, please let me know in the comment section below.

The Edinburgh Trams Nightmare

Over four years ago I started making plans to come to Edinburgh, UK, to study a master degree. Naturally, I did some research into the city beforehand and found out that one of the major local issues was the construction of a tram network that would run from the airport all the way to the city centre.

Edinburgh trams from the 1900s

I remember reading that a large percentage of the locals opposed the tram system, partly because the bus system is already an excellent travel option, but primarily because of the chaos the construction would cause. By the time I read that, I believe it had already been under construction for quite a while.

I moved to Edinburgh over two years ago, only to find Princes Street (one of the main streets in Edinburgh) still closed due to the construction. Buses were taking an alternative route, but the people still weren’t happy. This seems quite logical, as it had already been under construction for years with no end in sight.

About a year ago I remember quite clearly that the works on Princes street were finished. The end seemed near. Then shortly before the Pope’s visit not so long ago they even placed a tram on the rails on Princes St. But, was it functional? Well, the thing itself seemed pretty functional… but the rails lead nowhere! Yes, Princes St. had rails installed, but at each end they just terminated abruptly. Construction was still not finished, and there was no evidence to why the works were halted.

Shortly after the scandal grew as the council got into endless debates with the construction committees and the mediators. There was a lot of frustration over the cost of the whole project, and the fact that it was coming out of the public’s pockets. Then they started a debate over whether to scrap the whole project and remove the tram lines, or finish the project off. The issue was costs of course: Is it cheaper to go on or to back off? This was followed by more discussions…

At the end of August, 2011, there were several interesting events reported in the news. I believe the first one is that the council decided to back out and scrap the project. Alarmingly quite a lot of people complained, including citizens who initially opposed the project. Afterwards, the tram chiefs decided it might be best to terminate the line at Haymarket. Finally, according to the Metro newspaper, a consortium (Train manufacturer Bombardier and French company RATP) declared that they would like to take over the construction of the trams and offered a good price. They tried the same previously but were rejected.

Now for my personal view: This is an absolutely ridiculous matter. Come on, years to build a tram system? How hard can it be? So much debate about the costs, so much time wasted, so much money spent on ridiculous meetings in posh hotels that reached no conclusion, so much chaos in Princes St. for years. I wonder how stupid a group of people must be not to be able to develop a proper system, a clear view of the costs based on materials and labour, a precise estimate of time, and a funding effort to obtain the money from various sources instead of relying primarily on the council.

Half of the tram line is already there! It’s just a matter of being intelligent, adding some numbers, deciding on the best strategy, and going ahead with it. You can’t back out now. That would be like NASA saying “oh, sorry astronauts, we’ll have to leave you up there on the ISS because we’ve disbanded all our shuttles and it would cost us too much money to get someone else to bring you down.” (I seriously hope that doesn’t happen, that will be the moment I lose faith in humanity).

Anyway, that is just my 2p on this issue. Think first, then act, but don’t spend too long thinking, and don’t back out when you’ve already started to act.

How Police could effectively pre-empt riots

Over the past few days we have seen violence escalate dramatically in different cities across England, with London being the primary location. Acts of violence, rioting, looting, and arson have taken place at night for several days.

I have been attached to the BBC News coverage through their website and I saw an interesting tweet posted there:

Tweeted by Hannah Robertson in GloucesterAt first I thought. Pre-empt where the next bout would take place? How on earth would they manage that?

But it actually isn’t that hard. They have a mountain of CCTV data, they have a good amount of servers, all they lack is a bit of artificial intelligence.

The best way I can think of to determine where possible bouts of violence could sprout in what seems like an apparent random non-deterministic method of choice is as follows:

The IT team of the police should annotate the direction of each CCTV camera, including which streets are in view and the coordinates.

Afterwards, the CCTV cameras should be plotted on a 2D map, as vectors (pointing in the direction they are facing, with the length of the vector being the distance the camera covers).

Map Plot of LondonAdditionally, all points of interest should be mapped, such as shopping centres, residential areas, commercial areas, shops with high-value items, and shops with low-value items. Most of this data can be readily obtained from Google Maps, amongst other online maps. This data can be extracted and annotated with the values we require (such as the value of the goods sold per area, etc.)

If you think this is a very hard task, it is not. A very simple way of doing this would be to go to the website (or ask by phone) of each major retailer and chain for a list of addresses of their shops. Google Maps can plot them on a map using a spreadsheet as input. There you go, simple as that!

A Machine Learning program could be developed using WEKA (for instance), in which an SVN is programmed to take data from the map-plot, where priority spots include places where high-value items are sold. Additionally, it would take data from each CCTV camera.

Now, how do we represent the data from CCTV cameras? One way would be to take a selection of pixels from each camera, measure the amount of change for a second, and wherever there is a large enough change in different areas of the same camera, we might have a lot of movement going on. So we automatically annotate the data per CCTV camera as having “movement” or “no movement”. Additionally, a range from 0-1 would produce better results (hopefully).

So now we run an SVN machine on the data and hopefully come up with some interesting results.

What could essentially be obtained from this is a vector describing the movement of as mass of people from camera to camera, this vector would be projected on the 2d map. Multiple vectors could be plotted at the same time if there were multiple riots taking place at the same time in the city. A confidence level can be given to each vector (assuming we have built in a few mechanisms to differentiate people from cars, etc). The places of interest could be mapped as hotspots, and furthermore we can predict paths to possible places of interest, derived from the speed and direction of each vector.

So there you go. If you belong to the MET, please share this blog post with your boss (or the IT team) and get working on a system to perform such actions, it would certainly help prevent further stupidity in the future. 6,000 police officers should be able to deal with outbreaks of crime provided they knew where the rioters would be gathering and where they are heading.

Credit is NOT bad

Credit is not bad - But the way we use it IS!

Edilberto Salazar (name changed), 35, married, with one daughter, is a man that I met over two years ago while working for an IT firm in Mexico. This man owed over $200,000 (£10,000+) pesos to the bank, more than $100,000 (£5,000+) pesos in taxes, plus his mortgage and personal credits at department stores. He is one of those guys that saturates one credit card, then applies for another to which he transfers all his overdue credit from his other card, and so on.

He is just one of many people I have met in several years that has the same problem. Endless calls and letters, harassment, and bad looks from friends and family. The truth is that he is just one in millions caught in the same situation.

Credit was created as a way of obtaining something we want now and paying for it later, at an interest, of course. Interest rates and other surcharges are the building blocks of banks, lending companies and other financial institutions. In fewer words: Banks charge for the “benefit” of lending you the money you need right now.

But it’s not just money nowadays, is it? Back in the old days it was mostly about a means to purchasing the home you’ve always wanted, or paying for a costly operation, or buying something you really need urgently. Nowadays the amount of things you can do by credit is enormous: Clothes, home appliances, cars, luxury items, handbags, mobile phones, phone contract payments, payments for other debt you may have in other financial institutions, education, even a haircut for goodness sake. Just about anything can be “purchased” on credit, and if the store doesn’t have its own credit line, you can probably still pay with your Amex, Visa, Mastercard, Maestro, etc.

But the concept of “free money” is what has started the massive wave of debt. We saw it recently with the credit crunch, but we’ve seen it previously countless times. And it’s not about individual people any more, it’s about entire countries indebted to others.

Why did this last credit crunch occur? Because of the sub-prime mortgages, the ratings of CDO. MRS, ABS and other deals, the lending, selling and packaging of these deals amongst banks and world-wide financial institutions, etc. They thought the gold mine would never end, and when it looked like it was depleting they found another way of marketing and selling the same deals as new products to investors, creating new indexes in the stock market, and eventually tying everything up in a way that a credit crunch became unavoidable.

But it’s not the banks fault, nor the investors, they are just looking for ways to make more money. The fault lies within the general public, as described by Tetsuya Ishikawa in his book “How I caused the credit crunch”. Credit allows you to have a house before you can pay for it, clothes without any money on you, and more importantly, operations and medical care when you do not own enough money to pay for them.

We all need money for certain things in life, but we also ought to have some decency (or put more straightly: intelligence) and analyse all options into consideration. Credit is a tool, an aid through difficult times, it is not free money, and it often results in a much higher cost than what you could have paid originally for it. Interest is a killer.

So if this opened a wound in you, try to sort out your problems, and lay off credit for life unless REALLY needed.

My Social Network Map

Three days shy of a year ago I published a blog post about my social networks in which I explained a bit about the interconnectivity provided by sites such as Ping.fm which help you keep your status updates synchronised across social networks.

Last year I was on about 10 social networks, this year the number has gone up to 17 (which is  really19 as I excluded Google Reader and Wordpress.com for visual purposes). But it seems to me that interconnectivity has still not matured enough, there is no “home” for your social networks, and I doubt this will change in the short term.

Click on the image to see a full-sized version.

image