Saturday, May 21, 2011

Checking for anonymous types

Because I blogged about anonymous types last month, I thought following method would also make an interesting post.

private static bool IsAnonymousType(Type type) {
    Debug.Assert(type != null, "Type should not be null");
    // HACK: The only way to detect anonymous types right now.
    return Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false)
               && type.IsGenericType && type.Name.Contains("AnonymousType")
               && (type.Name.StartsWith("<>", StringComparison.OrdinalIgnoreCase) ||
                   type.Name.StartsWith("VB$", StringComparison.OrdinalIgnoreCase))
               && (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic;

For a type to be anonymous:
  • It should be marked with the CompilerGenerated attribute
  • It should be a generic type
  • Its name should contain "AnonymousType"
  • Its name should start with "<>" or "VB$"
  • It shouldn't be publicly accessible
A little fun fact is that the VB and C# compiler generate different type names. The C# compiler makes the type name start with "<>" and the VB compiler uses "VB$". Both smart safeguards, because the compiler doesn't allow us to use "<>" or "$" while defining type names. I find the C# way a tad more elegant though.

I stumbled upon this beauty while browsing the ASP.NET MVC source (System.Web.Helpers.ObjectVisitor). Because there is no direct way to detect anonymous types yet, I'm pretty sure this is the best implementation out there.

Saturday, May 14, 2011

My first jQuery plugin: jRoll

With transitioning to ASP.NET MVC, I see our JavaScript codebase increasing exponentially. And this is a big win, unless we fail to keep that codebase maintainable. We mainly rely on jQuery to do our DOM manipulations, so it's only logical for us to abstract those manipulations into reusable jQuery plugins.

So today, I wrote my first jQuery plugin and named it jRoll.


jRoll is a plugin that finds all the (external) links in a jQuery object, and replaces the value of the href attribute with a value you specified. If you don't specify that value, the default value is used, hence the Roll.

It can be used like this.


But it can and should be used like this.

$('body').jRoll({ 'url' : '' }); 

I'm also not breaking the chain.

$('body').jRoll().click(function() { alert('Never gonna give you up') });

Anyhow, here is the source of my first not-so-serious jQuery plugin.

// Pass jQuery to a closure that maps to $ so it can't be overwritten
// by another library in the scope of its execution
(function($) {
    // Add a new function to the jQuery.fn object (your plugin name)
    $.fn.jRoll = function(options) {    
        // Default settings
        var settings = {
            'url' : ''
        // Merge defaults with options argument
        if (options) { 
            $.extend(settings, options);
        // Modify elements and return them to maintain chainability
        // this refers to the jQuery object the plugin was invoked on
        return this.each(function () {                    
            $(this).find('a[href^="http://"]').attr('href', settings.url);

To build your first plugin, I recommend you use the excellent jQuery documentation.

Download the full jRoll source here.

Monday, May 9, 2011

My thoughts on WebMatrix

After building, I felt like I had to do a follow-up post sharing my experiences with WebMatrix. While doing some research, I came across this post by Rob Connery. And frankly, I think it's almost impossible for me to add something to his findings.

In a nutshell

For those who are like "tl;dr", here are my thoughts on WebMatrix..

WebMatrix is the perfect framework to start with ASP.NET development (wish I had). For people who are already familiar with ASP.NET, WebMatrix will hardly have any learning curve. They should also have a look at it though, because it's a lot of fun ánd it is a great framework to get things done. Every developer has been asked - at least once - to slap together a site for a distant acquaintance, where the site consists of mostly static pages, with a dynamic List<Something> pulled out of a database. I see WebMatrix as a perfect gluing framework to swiftly ship these types of projects. While you could probably also build something more enterprisey, I don't think you should.

See for yourself

I have uploaded the source of Download WebMatrix using the WebPlatform Installer and see for yourself.

You might notice that the project is called arealprogrammer. That was the name of the project before I bought the wrong domain name.

Monday, May 2, 2011 mentioned on Channel9

While nosing around in the analytics, I saw a bunch of traffic coming from Channel9. Clicking through, I found out A Real Developer was mentioned in this video (02:01). That's a Channel9 front-page video!

That's so awesome, I think it's great people find it a fun project.

I have been listening to your feedback, and this Sunday I implemented some of your requests:
  • Easier linking
  • Sharing
  • A feed
  • Voting
  • Comments

I try to keep everything as simple as possible, that's why I implemented most of these features using two popular free plug-ins: AddThis and Disqus.

For those interested, I will do a few posts on the making of this month.

Oh, and by the way, for Americans, they did pretty well pronouncing my last name ;)