Month: November 2012

EntityFramework 6 and async database queries

Posted on

Microsoft is going all-out on .NET 4.5 and C#5’s async feature. Windows RT (the .NET libraries for interfacing with the windows runtime) makes heavy use of task-based asynchrony in the API, and Microsoft’s rule of thumb to all .NET developers is to make any method that can reasonably be expected to take more than 50ms should be asynchronous. This includes web service requests, file read and save operations, and more.

Given the examples in the previous paragraph, most people will instantly think “Well, what about databases?” (as I did). Indeed, so did Microsoft, and they’ve got us covered. Entity Framework 6.0 (currently in alpha state at time of writing) has been updated to support asynchronous queries. What does this look like? Currently there are five methods that make use of asyncrony – SaveChangesAsync() and ExecuteSqlCommandAsync() for non-queries, and FindAsync(), SingleAsync() and ToListAsync() extension methods for IEnumerable.

I’m looking forward to testing this code out to see how it runs. So far I haven’t used async in production code, but from the work I’ve done on my own personal projects, I can say that I’m very impressed with how well .NET async works as well as how easy it is to use.

Keeping your Powershell profiles and functions synchronised between computers

Posted on Updated on

If you’re like me and you use Powershell on a regular basis, you probably have a load of functions set up in your profile.ps1 file so that they get loaded when you start a new session. The moment you start doing this, there’s the constant problem in the background of how to keep this in sync between different computers. I will often be fooling around with some new technology at home and will come up with a super-useful Powershell command, and will then need it at work as well.

I solved this problem with use of Dropbox (you could use any other file syncing app you like, such as Skydrive (or even a network share), the only prerequisite is that it supports symbolic links).

  1. If you haven’t already, separate your Powershell functions into distinct files, and have them all be loaded by your profile.ps1, which acts as a bootstrapper. Your profile will look something like this:
    $scriptFiles = gci "$home\Documents\WindowsPowerShell\*.ps1" -Exclude "profile.ps1"
    
    foreach ($scriptFile in $scriptFiles)
    {
    . "$($scriptFile.PSPath)" | out-null
    }

    Once you’ve accomplished this, you need to create the symbolic link. Open the command prompt and cd into your My Documents folder. If you already have a directory called WindowsPowershell in there, you’ll need to delete it, copying all the files within into your new directory in Dropbox. The command you need to run is this:

    mklink /D WindowsPowershell <dropboxPowershellDir>

    where <dropboxPowershellDir> is the full path to the directory of scripts in your Dropbox folder. Et voilà, you now have a Powershell profile that synchronises between computers!