Thursday, 18 February 2010

Using XmlPeek and XmlPoke in PowerShell

<xmlpeek /> and <xmlpoke /> commands in NAnt are really useful when it comes to changing configuration files during your automated build process. But do we do when we need that functionality in PowerShell? I did not find any native implementation but it's really easy to implement your own. Here it goes:

function xmlPeek($filePath, $xpath) {
    [xml] $fileXml = Get-Content $filePath
    return $fileXml.SelectSingleNode($xpath).Value

function xmlPoke($file, $xpath, $value) {
    $filePath = $file.FullName

    [xml] $fileXml = Get-Content $filePath
    $node = $fileXml.SelectSingleNode($xpath)
    if ($node) {
        $node.Value = $value


It accepts FileInfos so it can be easily used together with Get-ChildItem:

Get-ChildItem P:\MyProject -Include *.config -Recurse | %{ xmlPoke($_, "/configuration/connectionStrings/add[@name='MainConnectionString']/@connectionString", "DataSource=MyDB") }

Enjoy! :)

Tuesday, 16 February 2010

What is Windows Phone 7 Series for developers?

Internet is buzzing all around about new Windows Mobile OS - Windows Phone 7 Series. Presentations are awesome, UI is nice and usable. At last Windows fans have something mobile to be proud of :)

But what is this event for us, developers? As an iPhone user and .NET developer I was rather anxious to people who could write programs for their phones and sell them at market. But looks like things have changed. We'll have a nice .NET environment at new phones - that's a fact. However, base technology still remains uncovered. According to Scott Guthrie:

re: Windows Phone 7 development: I can't say more right now other than we'll discuss it at MIX - and that it is very, very cool.

I'm not sure but I think Silverlight will be this technology. As I can judge from presentations it is already supported at WP7S devices. All needed is to allow Silverlight applications to run out of mobile browser and become an application that user can run just like Mail or Calendar. And that feature is already present in Silverlight 3 so guys have to work just a little bit more to make us very happy.

What will be next? I think I'm not the only one who wants to write mobile .NET applications so perhaps we'll see a mobile development boom in .NET community. More Silverlight developers, greater Windows Phone adoption, huge popularity raise for Expression Blend and Visual Studio - wonderful situation for Microsoft.

All these are just suggestions and we'll have official details only at MIX that will take place in March 15-17 in Las Vegas. Keeping in mind that we'll see Internet Explorer 9 there - it will be an Event.

Friday, 12 February 2010

What do I need from Internet Explorer 9?

That's a really interesting question. I did not use any Internet Explorer as my main browser before Internet Explorer 8. IE8 changed my mind a bit and I've been using it for about a month and a half. Why did I jump off:

  1. Tabs opened slowly
  2. Pages loaded slowly
  3. No nice CSS features like border-radius
  4. Misses some FireBug features (although see next p.1)
  5. Too large header bars (comparing to Chrome)

What I liked:

  1. Wonderful tool for developers, it's my favorite at the moment (better than FireBug!)
  2. Tab grouping
  3. Extensibility

After IE8 I've tried to return back to Firefox but I could not stand it's strange behaviors anymore so I've switched to Chrome. What do I like in Chrome:

  1. Damn it's fast
  2. It's fast
  3. Did I mention it's fast?
  4. It has neat interface

So, what do I want from IE9? That's what:

  1. Please fix what I disliked in IE8
  2. Please add what I like in Chrome

This is a simple recipe, isn't it? :)

Saturday, 6 February 2010

Mock or stub?

When I was just getting into unit testing the subject question was rather hard to me: it’s not actually obvious for a novice. The answer to it came from a unit testing book I think is very nice and useful - “The Art of Unit Testing” by Roy Osherove.
Mock or stub?

So what’s the core difference between mock and stub? The answer is: mocks can fail tests and stubs can not. You may think this minor point is not important but it is. Basically when you test something you have the following two components: code under test (CUT) and test itself. There are two different cases for testing:

1. You may run CUT and validate it results – it’s actually testing CUT internals

2. You may run CUT and validate how it communicated with some external service – it’s actually testing CUT externals

The first case usually accompanied by stubs – some stub objects that you use to pass initialization information to CUT. They just provide data and do nothing more.

The second case is usually accompanied by stubs and a mock. Let’s say you have some IExternalService your CUT communicates with. After running CUT you want to be sure that two IExternalService methods were called with some valid parameters. You can write some custom class that implements this interface but the simplest option is to use a mock. Mock allows you to specify what methods in what order with what parameters should be called. After CUT run you can validate mock and see whether it fits your expectations.

The rule of thumb here is to have one mock in your test at max. Why is that? The answer is simple: when you use two mocks and validate both of them in a single test it means that you test two different pieces of logic in one test. That may be a result of scattered logic (violation of Single Responsibility Principle) or just a bad test design – anyway it’s a bad practice. What though should be remembered is that you can use as many stubs in your test as you wish – they just provide initialization data and can’t fail your test.

Wednesday, 3 February 2010

The Law of Leaky Abstractions

The concept of leaky abstractions was introduced by Joel Spolsky in his blog 7 years ago, but it's not as widely known as it deserves. So here is the basic definition:

All non-trivial abstractions, to some degree, are leaky

This simply means that if you're trying to hide something beneath your abstraction layer - it will almost always show itself up. These errors are hard to detect and have severe consequences. There are many examples of leaky abstractions:

  1. Some SQL queries are thousand times slower than their logical equivalents - DB implementation leaks into SQL
  2. SQL, in its turn, has abstractions over itself like NHibernate or Linq to Sql - and rather often you have to deal with SQL directly losing many abstraction benefits
  3. When you program GWT and write your code in Java it is later on translated to JavaScript. And what can be more confusing than having a JS error from running your Java code? :)
  4. .NET is an abstraction over Win32 and it has leaks sometimes - rarely, but these rare cases make people mad
  5. Any component framework like ASP.NET or JSF hides HTML/CSS and JS beneath them - guess what problems you may have?

What can be a conclusion of all above? It's all in Joel's original post!

Never hire a developer for working with some abstraction if he doesn't know direct underneath layer of it

It may sound strange or may be offensive for some of us but it's rather obvious: you should not hire an ASP.NET developer if he is scared of JavaScript or hire a so-claimed NHibernate guru if he doesn't know what 'DISTINCT' is?