LINQ-2-SQL ToListAsync async extension method

Are you “stuck” with LINQ2SQL as your ORM? Do you still want to take advantage of the new asynchronous programming model introduced in .NET 4.5? It can be done!

Here’s an example of how to do that. I’ve chosen ToList for my demonstration, but of course the concept is easily re-used.

        
public async static Task<IList<T>> ToListAsync<T>(
    this IQueryable<T> queryable, 
    CancellationToken token)
{
    var contextField = queryable.GetField("context");
    if (contextField == null)
        throw new InvalidOperationException("This extension method only works on IQueryables that have been created from a System.Data.Linq.DataContext");

    var context = (DataContext)contextField.GetValue(queryable);

    using (var command = (SqlCommand)context.GetCommand(queryable))
    {
        if (command.Connection.State != ConnectionState.Open)
            await command.Connection.OpenAsync(token).ConfigureAwait(false);
        var reader = await command.ExecuteReaderAsync(token).ConfigureAwait(false);
        return context.Translate<T>(reader).ToList();
    }
}

Points to note:

  1. Since I don’t know what environment you’ll be running this in, I use .ConfigureAwait(false). This ensures that there’s no risk of deadlocks if you consume this code in an ASP.NET, WinForms, or WPF environment. However, since the continuation doesn’t resume on the same thread, the thread’s culture information isn’t marshalled back. This means that the context.Translate call will always execute on the invariant culture. I haven’t reflected too deeply into the Translate call, but if you localize your application, you may need to confirm whether it exhibits culture-specific behaviour.
  2. The explicit cast to a SqlCommand protects against your custom DataContext implementation returning a DbCommand. DbCommand, although it provides some helpful-looking virtual methods, isn’t any good. For example, the ExecuteReaderAsync method of DbCommand is actually synchronous and blocking! Therefore you get none of the goodness of ADO.NET’s IO completion ports.

Enjoy!

.NET bitcoin wrapper

So I’ve been getting into the whole Bitcoin scene recently. It’s a fascinating currency that, at time of writing, has about $300m worth of value in circulation – no child’s plaything by any means.

I had a play with the JSON/RPC server that’s included in the default client, and decided to cook up a quick .NET wrapper for it as there didn’t appear to be much out there already.

As such, here’s Gretton.Bitcoin.

Mini-me

A little video of mini-me, who is nearly 3 weeks old.

It’s on Google Plus, which doesn’t seem to let me embed the video here?

 

Gumtree, how you hurt me.

I’ve been through the intensely painful experience of trying to post something on Gumtree. It’s seemingly the largest classified site in the UK, and I’m in the supposedly simply position of trying to sell a television. I wanted to avoid eBay so I didn’t need to worry about trying to send a very large TV somewhere – I just want someone to collect it locally.

However, Gumtree had other plans for me.

Here are the things that went wrong in my short time using it.

  • The SMS PIN that they send you to verify your ad, did not arrive. Twice. Third time lucky.
  • Want to preview your ad? Better not be using Chrome. It doesn’t work.
  • Want to use DOUBLE QUOTES in your ad? Bad idea – it’ll lose all the data as soon as you use them. This is, I’m afraid, simply incompetent on the part of their developers. I imagine since they’re using Perl, I’m falling afoul of some bad regex – but that’s only a guess.
  • Finally, I get through all the barriers, and post my ad. It gives me the URL. I click it. And instead, it redirects me to a random page with a querystring of “?expiredVipReferrer=true”. Where’s my ad gone? I have no idea.
  • Oh yeah – and endless javascript errors…

Seriously, I need to build an alternative – it’s shocking!

Image to HTML converter

I spotted this tweet while wandering around Brighton’s fantastic North Laine today. It described something that’s been done in so many ways before, and it inspired me to knock up my own version. So as soon as I nipped home at 17:48 today, I bought the domain I knew I wanted.

A bit of coding, a bit of dinner, and a bit of Netflix later: tada – I deployed the site at 22:15. It’s awesome being able to rapidly implement and deploy things with ease – although it does need lots of bugfixing and a designer’s loving touch.

Try converting an image into HTML yourself!

Now, time for some sleep before the day job begins!

IPv6 – it’s time; and it’s incredibly easy

Yes, it really is time for IPv6. The world is running out of IP addresses. NAT is a bad “solution”. IPv6 will happen, and you can play with it now without changing your ISP. Get ahead of the game, and give it a go today.

Linux users know what they’re doing and won’t be reading this.

Windows users, here’s how to do it incredibly easily.

  1. Sign up for an account at SixXS. It’s a free project, managed by a couple of super smart people who have rather distinguished day jobs. You’ll need to state the reason why you want to play with IPv6. Be honest.
  2. Wait for them to approve you (they let me in within 2 hours).
  3. Log in, and request an AYIYA tunnel
  4. Wait for them to approve you (this took less than an hour).
  5. Download the OpenVPN client. This one is current, right now.
  6. ONLY install the TAP driver when you go through the install wizard. Un-check everything else.
  7. Download both the the GUI, and console version of AICCU from here.
  8. Run the GUI version, and sign in once. Select your tunnel provider. No need to press “enable”, or “test”. Just click the top left part of the window, and choose Save Config. It will write a file to c:\windows.
  9. Close the GUI version, and run the console version instead.
  10. Try pinging facebook.com. It should resolve as something like 2a03:2880:2110:3f01:face:b00c::. Congratulations, you’re now running on IPv6.
Now go and write to your broadband provider, asking them when they’re introducing support for it. Many Asian markets are already well ahead of the game on this one, and your IPv4-only skills are going to look remarkably outdated really soon…

 

Right click / view source

Being an old-timer, banking at Nationwide, I get to participate in their AGM voting. A yearly (duh) ritual, I am lucky enough to be able to vote online.

They appear to be outsourcing their service to “votebyinternet.com”, whose HTML source is curated by someone who doesn’t expect it to be read all that much.

For example, a lovely stock photo of some supposed Nationwide customers is named as follows:

<img src="images/nbs12/dudeAndWoman.jpg" width="264" height="168" />

And some “works on my machine” code:

 function wopen(url, name, w, h) {
       // Fudge factors for window decoration space.
        // In my tests these work well on all platforms & browsers.
        w += 32;
        h += 96;
        var win = window.open(url,name,...

And there’s also the always amusing “didn’t make it in time” features:

<!--<li><a href="#">Accessible PDFs</a></li>-->

Plus of course a nice chunk of CSS that shouldn’t exist:

/*
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
TEMP STUFF - TO BE REMOVED
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
*/

And some downright confusing axiomatic statements:

<!-- These need to point to the right place -->
<a href="#"><img src="../images/nbs12/ers-logo-2012.gif" alt="Electoral Reform Services" width="103" height="32" /></a>

I’m sure my own work has similar amusements (and all the above is completely harmless, of course). It’s just fun to pass comment on how an otherwise anodyne page can be quite charismatic underneath.

 

 

“No one can write correct programs”

…we did not (and still do not) believe in the standard multithreading model, which is preemptive concurrency with shared memory: we still think that no one can write correct programs in a language where “a=a+1″ is not deterministic

Roberto Ierusalimschy, Luiz Henrique de Figueiredo and Waldemar Celes: The Evolution of Lua.

That’s a pretty harsh accusation, but a very good point nonetheless. Let’s see if we can write a correct multithreaded program that can add up numbers.

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static int a = 0;
        static int b = 0;

        static void Main(string[] args)
        {
            // technique 1
            for (var i = 0; i < 100000; i++)
                Task.Factory.StartNew(() => IncrementA());

            // technique 2
            Parallel.For(0, 100000, (i) => IncrementB());

            // more than enough time for your machine to finish
            Thread.Sleep(TimeSpan.FromSeconds(10));
            Console.WriteLine("A is: {0}", a);
            Console.WriteLine("B is: {0}", b);
            Console.ReadLine();
        }

        static void IncrementA()
        {
            a = a + 1;
        }

        static void IncrementB()
        {
            b = b + 1;
        }
    }
}

The best thing for you to do, would be to run that on your machine and marvel at the results. However, if you’re not willing or able to, here’s the spoiler. A typical output would look something like this:

A is: 99805
B is: 45903

What on earth is going on? I’m using integers, and integers are meant to be atomically readable and writable, aren’t they? Yes, as per the spec, that is indeed the case. But it’s not what we’re doing here – we’re reading, then modifying, then writing. And as such, the nature of our multithreaded approach means we lose out. Imagine one thread reading the value of a as 0, then adding 1 to it, as another thread does the same. At the next step, they both write back the value 1 to memory, even though we’d expect the value to now be 2.

Interestingly, it looks like there’s a big difference in implementation between using the TPL, and using the Parallel class, with the Parallel approach running much faster – and therefore being much more prone to overwriting data.

The answer, if you’re not yet familiar with it, is to use the Interlocked class., which allows you to safely and atomically modify variables when many threads are potentially using them. I have trivially fixed my code sample – see below.

With the age of multicore machines truly upon us, perhaps it’s time to learn a language designed from the ground up for concurrency?

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static int a = 0;
        static int b = 0;

        static void Main(string[] args)
        {
            // technique 1
            for (var i = 0; i < 100000; i++)
                Task.Factory.StartNew(() => IncrementA());

            // technique 2
            Parallel.For(0, 100000, (i) => IncrementB());

            // more than enough time for your machine to finish
            Thread.Sleep(TimeSpan.FromSeconds(10));
            Console.WriteLine("A is: {0}", a);
            Console.WriteLine("B is: {0}", b);
            Console.ReadLine();
        }

        static void IncrementA()
        {
            Interlocked.Increment(ref a);
        }

        static void IncrementB()
        {
            Interlocked.Increment(ref b);
        }
    }
}

 

A (small) dose of programming awesome

What really got you into “computers”? For me, it was seeing a friend’s 386 running this (at the time) astonishing piece of code. At home, I was still soldiering on with a monochrome 286 computer, running at (yes) 0.0125ghz. It even had a button on the front to slow it down to 0.004ghz in case it was going too fast. I had a few games, and of course I played some games on friends’ consoles – but there was something about this demo that turned my head. I had to get involved.

Since then, I’ve been amazed by too many programming feats to enumerate or even remember – but here’s a few awesome things that you may have missed out on.

“When I grow up”, I can only hope to be as smart as the people responsible for the feats above. In the meantime, it’s back to the day job.

 

 

I am an idiot. I am a genius.

I was pleased to get an invitation to Stack Overflow Careers today. Pretty much everything they touch turns to gold at the moment, and I really wanted a play to see what they could offer. In particular, where did it stand on the spectrum – leaning more towards LinkedIn, or more towards a standard social network profile?

They’re really keen for you to fill out some free text entries (I haven’t written a “personal statement” since I applied to university!) and it raised the usual crisis of confidence – am I actually any good?

On the one hand, I look at some of the Big Names in the .NET world, and wallow in my inadequacy. This is reinforced on a daily basis when I pick up my RSS reader and see what Ayende or one of the many Scotts has to say. How do they absorb and understand so much information, dedicate themselves to the community, and still manage to productively deliver in a full time job?

On the other hand, I can take a simple glance at Stackoverflow and realize that I can place myself above quite a lot of others – for example, there’s too many reasons why this question has received no answers!

I think I’ll browse the unanswered section if I ever need an ego boost!