Archive for category Programming
MVC 3 Technologies
Posted by eric in Programming on January 26, 2011
I had a brown bag session with my colleagues at work the other day to discuss the new technologies introduced with MVC 3. Here’s a summary of my session and what I feel is the most useful new features. This is from the perspective of a developer who works on a mission critical website that gets millions of hits per month.
Razor View Engine
The new view engine is a great evolution for MVC. Not only does Razor minimize the amount of code we need to type (devs R lzy!), but it also allows for a much cleaner code file compared to the old ASPX files. Of course there will be some effort needed to learn the new syntax, but it shouldn’t be too difficult. I think the most difficult part of learning the new syntax is figuring out what syntax Razor is “smart” enough to interpret on its own vs. what I will need to explicitly delimit as code. I guess the general rule is to assume Razor is “smart” enough to interpret what that I mean, and when build/test fails, adjust the code as needed. If you ever doubt how “smart” Razor is, you’ll get this nice error message at runtime:

Another helpful feature is having the ability to declaratively create HTML helpers. I always thought it was awkward having to create/encapsulate HTML in C# code. Coming from an ASP.NET WebForms world with user controls encapsulating all the HTML code, having to code HTML in C# just seemed so wrong. The more natural @helper syntax just makes so much more sense! Unfortunately having to put the @helper code in App_Code instead of the original design of placing the code in Views\Helpers negates some of the benefits. We’ll have to review further to see if this could be leveraged on our website.
I feel the main disadvantage with the Razor syntax is figuring out how to integrate it into an existing project. Our website already has a substantial amount of code written using the ASPX syntax. Since rewriting the entire website at once is not feasible, we would have to do a hybrid solution with both ASPX code and Razor code. This will work for our team, but may cause headaches for other teams reading our code as well as getting new teammates up to speed with our codebase. Since there’s a pretty straight forward mapping between ASPX and Razor, I can easily see someone building a tool in the near future to automate this conversion!
Partial-Page Output Cache
Caching is key to creating a scalable and performant website. Our website uses a lot of partial views to encapsulate functionality so being able to output cache the partial views would be great! Currently we have our own helper class that encapsulates caching functionality. Having this functionality built into the framework would make the code a bit easier to manage. Too bad the documentation on this topic is lacking right now and I still haven’t been able to even test this feature out in test code. Once the documentation gets better, this is one feature I would like to revisit.
Unobtrusive Javascript
I get the idea of unobtrusive javascript: separate out the behavior from the content, but how does this impact a website outside of validation? Validation always seems to be the typical example, but I have yet to see how this works in other scenarios. We code our own jquery.validate on our website so using the built in unobtrusive validation isn’t very useful. Similarly with unobtrusive ajax calls, since we have our own framework for making ajax calls, this also isn’t very helpful. Seems like this is a good idea, but there needs to be more use cases before this becomes useful in an existing web application.
MVC 2 Upgrade to MVC 3
Wish there was a wizard to do this, but the manual steps outlined in the readme file actually isn’t too bad. Basically changing your version references and some GUIDs in your project and config files. My quick summary of the upgrade process from the release notes:
- Create empty MVC 3 project.
- Copy files form MVC3 project to MVC2 project: jquery.unobtrusive-ajax.js
-
- /Scripts/jquery.unobtrusive-ajax.js
- /Scripts/jquery.unobtrusive-ajax.min.js
- /Scripts/jquery.validate.unobtrusive.js
- /Scripts/jquery.validate.unobtrusive.min.js
- /Views/Web.config
- If using Areas, copy /Views/web.config to Views folder of each Area
- In web.config, replace System.Web.Mvc,Version=2.0.0.0 with System.Web.Mvc,Version=3.0.0.0
- Delete existing System.Web.Mvc 2.0 reference and add 3.0 assemblies:
-
- System.Web.Mvc
- System.WebPages.dll (%ProgramFiles%\ Microsoft ASP.NET\ASP.NET MVC 3\Assemblies)
- System.Web.Helpers.dll (%ProgramFiles%\ Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies)
- Edit the project file, replace {F85E285D-A4E0-4152-9332-AB1D724D3325} with {E53F8FEA-EAE0-44A6-8774-FFD645390401} in ProjectTypeGuids, save.
- In root web.config, add the necessary entries in the assemblies section and set bindingRedirect if needed. See release notes for exact code.
Other MVC3 Changes
The new ActionResult classes would have been useful a month ago! Other than being late, these are welcome new classes. Just as how there is a Response.RedirectPermanent() in ASP.NET 4.0, RedirectResult can be used to cause a redirect with 301 status code (permanent redirect) rather than the standard 302 redirect (temporary redirect).
JSON binding also falls under the category of “useful in the past, but not anymore”. I would imagine most teams out there using MVC already have their own framework to convert .NET objects into JSON code.
Software Releases
Finally with MVC 3, there is a bunch of new software releases. The NuGet Package Manager is interesting. If there is a way to setup a private repository server, then this feature would become infinitely more useful in an enterprise environment where multiple teams could easily share internal code. Since its currently based on a public repository (from what I read), then the useful of this is limited to just referencing public packages.
IIS Express and SQL Server CE is not very useful in a corporate environment since everyone would already have a copy of IIS7 from Windows and SQL Server.
Finally the release of the Web Farm Framework would make deployment so much simpler. Still looking into it but so far this looks to be very promising!
Thoughts and Resources
While looking for a cool image to include with this post, I realized that MVC doesn’t have a logo! Silverlight has one, ASP.NET has one (in this post). Since MVC is becoming more mainstream, Microsoft designers need to start designing a cool logo for MVC!
So that’s my quick summary and thoughts on the new features with MVC 3. Hopefully this will help out someone else out there too!
During my research, I reviewed these links to get up to speed.
MVC 3 Official Documentation
http://msdn.microsoft.com/en-us/library/gg416514(VS.98).aspx
MVC 3 Homepage
http://www.asp.net/mvc/mvc3
Scott Gu Posts
http://weblogs.asp.net/scottgu/archive/2011/01/13/announcing-release-of-asp-net-mvc-3-iis-express-sql-ce-4-web-farm-framework-orchard-webmatrix.aspx
http://weblogs.asp.net/scottgu/archive/2010/12/10/announcing-asp-net-mvc-3-release-candidate-2.aspx
http://weblogs.asp.net/scottgu/archive/2010/10/19/asp-net-mvc-3-new-model-directive-support-in-razor.aspx
http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx
Phil Haak Posts
http://haacked.com/archive/2010/12/10/asp-net-mvc-3-release-candidate-2.aspx
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
UITableView Background in a Standard UIView
Posted by eric in Programming on March 22, 2010
While exploring the CoreDataBooks sample iPhone app, I came upon the EditingViewController which I noticed had the same grey/white bar background as does a UITableView has when it’s in grouped style. After fiddling with the Background property for some time, I eventually figured out how to set the Background to the same background pattern.
1. Open the Inspector window for UIView and click Background.

2. Select “Color Palettes” (middle icon) and in the Palette drop down, select “iPhone SDK”.

3. In the options, select “groupTableViewBackgroundColor” and you’re done!

Your view should now have the same bar pattern as a grouped table view!

LINQ to SQL in SQL Server
Posted by eric in Programming on June 20, 2008
I’m writing some code that will be in a .NET 3.5 assembly running in SQL Server 2005. Since I’ll need to retrieve data from the database, I figured I might as well try out LINQ to SQL running inside SQL Server while I’m at it. The good news is that it will work, however you will have to add a couple more assemblies into SQL Server than just your assembly.
This post won’t go through the basics of SQLCLR integration. You can read about SQLCLR in this super long MSDN article or these 2 short and succinct articles instead (TIP: take the 2 short articles
).
So to get LINQ to SQL working inside SQL Server 2005, you’ll need to add the following assemblies first (shown in hierarchical dependence order):
- System.Core
- System.Data.Linq
-
- System.Runtime.Serialization
-
- SMDiagnostics
Run the following script to register these assemblies (assuming default installation locations):
CREATE ASSEMBLY [SMDiagnostics]
AUTHORIZATION dbo
FROM 'C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\SMDiagnostics.dll'
WITH permission_set = unsafe
CREATE ASSEMBLY [System.Runtime.Serialization]
AUTHORIZATION dbo
FROM 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\System.Runtime.Serialization.dll'
WITH permission_set = unsafe
CREATE ASSEMBLY [System.Data.Linq]
AUTHORIZATION dbo
FROM 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.Linq.dll'
WITH permission_set = unsafe
CREATE ASSEMBLY [System.Core]
AUTHORIZATION dbo
FROM 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\system.core.dll'
WITH permission_set = unsafe
Once these assemblies are in SQL Server, you’ll be able to add your LINQ-ified assembly into SQL Server.
With all those assembly registrations, is it worth it? I don’t think so. After I tested it out, I got rid of all those assemblies and replaced my LINQ code with a few simple calls to SqlCommand to get my small piece of data. This just goes to show you: the newest technology is not necessarily the best technology. Don’t just blindly follow whatever is the latest that Microsoft pitches to you!
Watching a random user use Vista
Posted by eric in Programming, Software on October 9, 2007
I was sitting in the library earlier today when I noticed someone sitting in front of me with her computer on. While laptops in the library are commonplace these days, what was especially interesting about her computer was that it was running Vista. So out of curiosity, I decided to watch Jane Doe for a few minutes to see what her experience with Vista was like (Yes I’m nosy, but this is for an educational experiment.
And if you’re the person who I was watching, I apologize. Don’t worry, I was too far to see anything meaningful!
).
Jane had a Sony laptop running Vista Premium. Let’s see what she was doing while I was watching:
1. Getting Connected
Shi Da (師大) has a wireless network across campus. The first thing I saw Jane do was try to get connected to the wireless network. She stayed on the wireless network dialog for a while (probably a good 2 minutes), which I’m guessing because the connection wasn’t connecting.
2. Media Player
After getting connected, she restored an already open Windows Media Player window which means she was listening to music. I noticed headphones on so that seemed like a valid assumption.
3. Messenger
Of course who doesn’t use IM these days? She popped open MSN messenger next to check on who’s online… well that was the original intention at least. The window stopped functioning during login and then the crashed dialog opened up showing the window had stopped functioning. I couldn’t see which option she clicked, but the progress bar started going afterwards.
4. Browser
Jane opened up an Internet Explorer window, but unfortunately this one also stopped functioning. She typed, clicked on the window without any response. Then the crashed dialog opened up showing the window stopped accepting input from the user. She clicked it and the progress bar started again.
5. Waiting
She went back to her studies while waiting for the close dialog to turn off. About 3 minutes later they finally closed. She opened up another browser window and this time no crashes. She typed in Facebook’s URL and started checking someone’s profile (couldn’t see from where I was sitting).
6. Torrents
She opened up another browser and this time opened up a torrent tracking site. She downloaded a torrent and started downloading some file (once again, couldn’t see exactly from where I was sitting).
7. Switching back and forth
For the next few minutes, Jane switched back from Facebook, to Media Player, torrent status, and back to Facebook.
8. Back to networking
About 5 minutes later, the browser stopped loading web pages. So she went back to the wireless network dialog to check on the network status. About 30 seconds later, the network was back up so she went back to Facebook.
At this point, I stopped watching as she was just browsing Facebook now.
One question comes into mind: did she use any of the “new” features of Vista? Absolutely not. Everything that I watched her do on her computer, XP could have handled perfectly if not better than the Vista equivalents. For example, the sidebar was left in the default state with the picture and clock gadget. The sidebar was left uncustomized and was just sitting there taking up screen real estate. Flip3D? Might as well not exist as she used the task bar to change windows.
So what did I learn from watching this random computer user using her computer running Vista for a couple of minutes? I learned that the flashy Vista or the new UI means nothing to the user. The user only wants to be able to accomplish a task: chat, read web sites, listen to music, …. While I learned this lesson previously through my past work experience, watching this random user today just reinforced this idea in my mind. I believe all software developers should watch a user use a computer before programming professionally. Of course I mean a “typical” user, not your project partner who’s hacking away at a bash script.
Watching a user gives a lot of insight into how to design software for the user and not for the programmer.