Tuesday, March 16, 2010

Testing with Unity, Policy injection and solving “Ambiguous match found. (Strategy type Instance Interception Strategy” problem

I have been chasing this for quite some time now, about 4 days on and off. But now I have nailed it, thanks to Christer Cederborg – my Unity beacon in the dependency injection mist.

OK – the problem is as follows; I have an object factory that encapsulates the calls to Unity for registration and resolving. This is done because I want to control if the resolved objects should be wrapped with PolicyInjection or not.

However I ran into problem when I created test for my code. In the top most layer an resolved started to fail for the layers beneath them. I started to get an error which basically said:

Ambiguous match found. (Strategy type Instance Interception Strategy…

As often, I didn’t understand the error message and started to chase down the wrong things. I first thought that this had to do with the fact that I was mocking (with Moq) the object that I was resolving. And since Moq creates a type on the fly I could not possible create a policy match for it.

But that was not it. In fact it was simple and of course my own doing.

My ObjektFactory is a singleton – I want to resolve against the same container. But the reference to the UnityContainer was done inline right where variable was declared. Ugly and it jumped up and bit me…

OK – the solution was to have a Init-method that creates a new UnityContainer. And to call it in the test-setup (before each test) and in the application before the ObjektFactory is configured.

Easy – when you looked at the right place.

Friday, March 12, 2010

Short and sweet introduction to RESTful services

I have heard a lot about REST and RESTful services but never have had the opportunity to work in project that uses that architectural style of working. So sadly I haven’t learned it.

Here is a short and sweet introduction to the subject that explains REST in a good way.

So – as I understand it – it’s just HTTP. The way HTTP is meant to be (URL to each resource, stateless, hyperlinks between resources and usages of the HTTP verbs). I like the simplicity.

Monday, March 08, 2010

Nice, easy and working description on how to compact a Virtual PC Image

I have looked so long for this; a short and sweet description on how to compact Virtual PC images. Here it is: http://kurtsh.spaces.live.com/blog/cns!DA410C7F7E038D!1902.entry


  1. Clear out the machine of filler
    I recommend installing CCLEANER.EXE from http://ccleaner.com which deletes all temporary files from the Windows installation, Internet Explorer cache, the Recycle Bin, and any number of other "garbage"/"temp" directories in the VPC.
  2. Disable hibernation
    If you have hiberation turned on, there's a colossal sized file called hiberfil.sys in the root of the .VHD.  Go into Power options in Control Panel and disable hiberation to get rid of this file.
  3. Capture the Virtual Disk Precompactor .ISO
    In the menu of Virtual PC windows for your machine, capture the Virtual Disk Precompactor .ISO file which is located at:
    c:\program files\microsoft virtual pc\virtual machine additions\virtual disk precompactor.iso
  4. Run the Precompactor
    Go to My Computer and double click on the mounted disk.  It will begin to run the precompactor software which will essentially zero out all the free space on the disk, making it available for elimination from the file by the Virtual Disk Wizard, which we will use later.  This process may take upwards of 15 minutes
  5. Shutdown the Virtual PC
  6. Run the VPC2007 Virtual Disk Wizard
    From the Virtual PC console, click File-Virtual Disk Wizard and "Edit an existing virtual disk".  It will ask you to identify the .VHD file to reduce in size - select it.
  7. Compact the .VHD
    Select the "Compact" option then select "Replacing the original file".  This will take a few minutes.

One thing to note is that bullet point #3 describes that you should attach the .ISO image that is referenced. This can be done directly in Virtual PC and the menu command is called capture (CD -> capture ISO image...). Maybe self explanatory for some but I looked around a while for that one...

Saturday, March 06, 2010

BDD with SpecFlow – some thoughts after a workshop at Elevate

Last Thursday I facilitated a Elevate-workshop, Avega on BDD with SpecFlow. It was, as always, a very nice learning experience for me, and hopefully also for the participants.

I wanted to take some time to put down my thoughts and findings about the framework, BDD in general and some other stuff I have ran into.

BDD – Behaviour driven design

I don’t want to go into explaining BDD since it's been done several times before (I can really recommend the last Steven Sanderson post – excellent!) in a much better ways than I could ever do. Go on and read them if you want – I’ll wait right here.

But I can share some aha-moments and experiences that has surfaced for me when preparing and doing the workshop.

It’s not in the tool

First, as a colleague told me yesterday: “It’s not in the tool”. First and foremost I think that BDD is a great way to discus and formalize the requirements together with your customer. I know that Gojko Adzic has formalized this into workshops where you flesh out the acceptance criteria together and I really like that idea.

That promotes an ubiquitous language and helps you to preserve that language when turning requirements into code.

A great way to get hold of behavior is to ask for example scenarios. These fit very well as the scenarios we’re writing when doing our features and scenarios in BDD. That is so much clearer and crisper than sending documents back and forth.

Implementing outside in

Finally when it comes down to implementing the scenarios you get a nice outside-in approach to your testing. Of course this was where I started. Being a programmer I was eager to see how to use this to write code. And as you might know that was where I started glancing on SpecFlow and BDD.

Starting from the acceptance criteria's or scenarios I was amazed how a design was pulled from the text and turned into code. Just-in-time so to speak

Gherkin

SpecFlow is supporting Gherkin 100 %. Gherkin is the language that is used in the Ruby BDD framework Cucumber. And SpecFlow is a great way to use standard Cucumber/Gherkin BDD-style on the .NET platform. That mean that you can read and understand any Cucumber literature be if for the Ruby, Java or the .NET platform.

Gherkin is a DSL (Domain Specfic Language) for writing user stories and scenarios, with the well know trio of: Given/When/Then. And as it is a formal language it has it’s own best practices and trick that you’ll need to pick up before being productive. Here is a great article series that take you from basic to advanced:

SpecFlow

Earlier on I said “It’s not in the tool” but of course you will need one. The tool I have used is SpecFlow. The thing that I like with SpecFlow is that is 100% Gherkin  compatible and doesn’t introduce any funky syntax to go to code. Ok, it’s cool – but come on: “= () => “ – who reads stuff like that?

Just as in standard Cucumber you also get stub code for your steps, when you run the scenarios the first time. I love that feature since it helps you along in a very nice way.

The binding between your scenarios and the code is done in a class with step definitions. The methods are decorated with attributes (Given/When/Then) that instructs SpecFlow which method to run for a certain step.

Here we found out some problems and opportunities:

  • The steps can be located in any class with the Binding-attribute. There is no connection between the scenario and the binding. It just a bunch of steps. If more than one attribute match a step in a scenario SpecFlow will throw and exception and inform you about it.
  • SpecFlow supports that you can have more than one attributes (two different [When]’s for example) on the same method. Using this technique you could have very specific strings in the attributes and still have them call the same method.
  • In the attributes for the steps you can use regular expressions to be able to send different data to the same step. This could also be a bit confusing since some regular expressions will match a “bigger” portion of a string than you first might have guessed. This feature is standard Gherkin.
  • Another, and maybe better way, to send data is the table construct in Gherkin. I found this a much better way to keep your scenarios clean and less sensitive for change.
    The syntax for tables are a bit special though:
    • You need to have space after the pipe (|) sign.
    • The table (of course) need headers, so that you can reference different columns in your code. The first line is the header.
  • A thing that confused me, but is the way it has to work, is that a pending step will stop the execution of the rest of the scenario.

All in all I think SpecFlow is an excellent choice when doing BDD on the .NET platform.

A bumpy ride – bugs and embarrassing moments

As me and Måns Sandström created the code and lab for the workshop we ran into more bugs in tools and frameworks than I ever seen… None of them had to do with SpecFlow and some of them had to do with me…

  • We used Resharper 5.0 Beta (different versions). I cannot imagining Visual Studio without Resharper, but this time I started to soon. Thanks to some great support some of the issues were solved during the weeks we developed the code.
  • I still cannot get Resharper to run the test from the .feature-file. “No test found in file” is the response that I get from Resharper for that… That worked for a while but has disappeared now. I haven’t reported a bug for that yet – not sure if it’s in Resharper or in SpecFlow.
  • I also reported a bug in Moq – but that was completely my fault. And I have to endure the embarrassing “user fault” in the report…
    That had to do with me using an overload and forgetting do an Verify for the overload.

So all in all – I’m hooked. I will sure try to get some more BDD approach to the project I’m in. I up to now I haven’t seen a better tool than SpecFlow for the job.

Wednesday, March 03, 2010

SOAPUi and MSBuild

After my last post I cannot withhold you from this nugget. It’s a guy (Todd) that have created some MSBuild targets to call SOAPUi from a build script.
I am about to try it out… I’ll get back to you if I run into problems.

[UPDATED]
Yup - it worked! Great! Thanks Todd.

Still have some issues in that the switch /endpoint will override all the endpoints in the SOAPUi project... But I'm on it...

SOAPUi and testing WCF Services – how I made it work

I have already blogged about SOAPUi but then it was more me thinking and seeing it was a good idea to use SOAPUi to do testing of service.

Now I have actually done this – and I love it so far. In the last project where we our deliverable was a service I think we spent about 30% of our time creating and maintaining the test client.

Not to speak about all the time we had to spend explaining for the client that “no this is NOT the GUI you will see later on”.

So the thought of a general test client for SOAP services is very compelling. And SOAPUi has done a great job making your job easy and fun… Almost all of the time.

They have an amazing amount of documentation. I guarantee that everything I am about to tell you is in there somewhere… This is my short story for our case.

So the case is as follows – I have a quite simple WCF service. One trick is that we need to call a common security service (let’s call it SECI) to get a token that we have pass into our operations.

We have worked contract first and started with a WSDL-file. I used WSCF.blue (great tool) to generate the service stub. And then…

  • I downloaded SOAPUi. Go on - do it now- it’s free!
  • I created a new project by pointing the wizard to my WSDL-file. I also asked the tool to create a starter testcase for me.
    Beware of the location or endpoint address – it’s read from the WSDL-file and most likely you will need to change that to your computer or the server where your service is deployed.
  • I then added a new WSDL to the SECI service to the project. This is needed to be able to call

I was now ready to create my test case with the test steps I need to call the SECI service and insert the token into the request I wanted to test.

  • First I created some parameters for my test suite. This is actually hidden away a bit.
    • Open the test suite editor by right clicking it and chose “Show TestSuite Editor”
    • In the lower section of the editor you’ll find a “tab” that is named Properties
    • Here you can add properties that will be visible for the whole test suite
    • I added some inparameters to the SECI service and the token I will pass on
  • I then called the SECI service, passing it values from the parameters I created in the earlier step.
  • To get the values from the response of the SECI service into the request to my service I used a Property Transfer. In it I transferred the value of the token node into a variable that holds the property for the token.
    It’s a quite deep structure with dropdown boxes and namespaces in the UI here but it’s not to hard to understand.
  • Finally I could easily insert the value of the token variable into my request, as before.

It wasn’t to hard after I got understanding about the variables for the testsuite. I sure beats having to code your test client manually…

Great work SOAPUi!

Friday, February 26, 2010

Vb.net considered harmful

I have once again been forced into using VB.NET which I am not very fond of.

My points up to now has been that the language in itself is not that bad. But the support (refactoring etc.) in Visual Studio is much worse than for C#. Also, there is a culture in the VB.NET community (as I understand) to hide complex stuff from your user. So for example you cannot see the references you have in your project until you hit “Show all files”. Another example for the sub-par support you get when using VB.NET.

My final point up to now is that the most common reason to chose VB.NET over C# is that you have VB6-programmers that is to be converted into VB.NET programmers. The problem is that VB.NET automatically (yeah, you can and SHOULD turn it off) include the Microsoft.VisualBasic in all new projects. Which gives you the possibility to continue using Visual Basic syntax and commands, such as CLen(“a string”) and completely miss the OO point in .NET framework.

You could argue that all this is opinions by me. And maybe it is, but as more advanced stuff was and is being introduced into in .NET Framework (from 3.0 and forward) VB.NET is cracking down according to me.

Lamdas and anonymous methods for example is suffering heavily from VB.NET demand on typing all parameters and the fact that you have to use a “_” as a line ending when formatting long lines.

Take a look at these two examples, from the Moq Quickstart:

C#

VB.NET

I used the Developer Fusion C# to VB.NET conversion tool to translate the code as I cannot make it by hand in VB.NET. I fail every time. I also used pastie to shared the code. It doesn't support VB.NET syntax. Sorry for that.

What we now have is the language obstructing you and confusing you with syntax. That makes the whole concept harder to understand – and it’s quite hard to grasp from the outset for VB6-developers.

So from my point of view VB.NET makes your program harder to read and understand, and you have lesser support in Visual Studio (don’t know about MonoDevelop) and finally it includes all of Visual Basic so you stand the risk that your developers use non-standard syntax and commands.

Friday, February 19, 2010

Great Git-reference for non-commandliners

I have started to take a look at git. It looks great and all, but I am not a command line guy…

This quick reference made my life a bit easier.

Wednesday, February 17, 2010

SOAPUi and others – regression testing services

I been looking around for some way of (regression)testing the WCF service we will build in my current project. I will not write another client and be forced to maintain it during the project. This took loads of time the last time I tried it.
The thing I will settle on is SOAPUi, which seems just awesome. I have already written about it – but didn’t get the time to try it for real. I sure will now!
But there are others – many of the free. Just see for yourself.
A nice complement is WSDLDisco that creates a portal for your services. This is a way to open up for ad-hoc testing for project members that not is comfortable with XML or concepts as requests/responses.

Resolving with Unity and Policy Injection using extension methods

I actually thought that this was going to be a small thing. The Unity application block is Microsoft’s IoC-container, part of the Enterprise Library. The Policy Injection application block (PIAB) is Microsoft’s AOP framework, also part of the Enterprise Library.

You’d think that it would be easy to integrate the two… But it’s not… that simple. Actually that is stranger than it first sound because apparently they are calling each other internally.

OK – I want it to be easy to combine them, so I have written two extension methods of UnityContainer that make it easer.

Here is the extension methods:

And here is some tests showing the usage:

Of course I haven’t thought this out by myself. That takes time. I want things fast. So I steal ;). Thanks goes to this blogpost that helped me on the way and Christer and Anders who supplied me with some great input on the way.

Yeah that’s right here is the complete code example. And here is a services that translates C# to VB.NET if that’s your preference, mr Scott. :)

Tuesday, February 16, 2010

The MVC song

This is a bit geeky but it actually has mostly true stuff in it:

Thanks Anders Granåker for a great tip.

But the guys from Rails Envy is not far behind in the geekery…

Friday, February 12, 2010

‘urn:nhibernate-mapping-2.2' has invalid child element 'property' and Fluent NHibnerate

OK – this was almost driving me crazy.

For demonstration purposes I had used the automapping feature of Fluent NHibnernate.  In a very standard way – I used all of the conventions out of the box.

Except… I forgot to check the conventions. For example for primary keys the convention is to name it Id. Exactly that! Not ID or id or anything.

Thursday, February 11, 2010

System.BadImageFormatException: Could not load file or assembly 'System.Data.SQLite'

I ran into this problem when I tried to re-open a solution I did a while back when labbing with Fluent NHibernate and SQLite.

Behind the cryptic error message lies and easy solution; I was running the 32-bit version of the SQLite-driver and runtime. That' doesn’t fly on my Windows 7 64-bit machine.

Here is a more through description and here is a link to the latest version of SQLite that will get you all the version (32 and 64 bits) of the SQLite.

Monday, February 08, 2010

Casting in VB.NET

OK – VB.NET is on the subject again. As I remember there was two things that led to big arguments and confusions: Casting and arrays.

I’ve already blogged about how arrays are handled here.

Here is an article that describes how casting is done in VB.NET. It also compares with C# which great for understanding.

And here is the points in short:

And just for the record... VB.NET delenda est...I don't like using it - but I have to.

Wednesday, February 03, 2010

PowerCommands for Visual Studio 2008

I’m back with Visual Studio 2008 after a few months only doing Visual Studio 2010 stuff. And… you miss some stuff. Things get old so fast. Sad.

Here’s some nifty tools that get you a bit closer; PowerCommands for Visual Studio 2008.

Aaah – now it feels a bit better.

Monday, February 01, 2010

Synchronization for consultants – it works!

I am so proud. My evil scheme to keep my calendar(s) in sync got tested for real today.

Got to new customer. Installed Outlook to Google Calendar. Started Outlook. Viola! All my events from my other calendars in place with the ones from my customer. So now they will not book me on days when I have other assignments etc.

The only thing is that you cannot think to much about it because it will drive you mad. My customer Outlook is synched with Gmail, my Avega outlook is also synched with the same account to Gmail. My phone synchs to Gmail… But it works. ‘Nuff said.

AutoMapper – get rid of your tedious mapping code

One thing that I really love being on a contract is that you’re almost immediately is forced to find solutions, whereas on a leisure project you rather do something else…

Here is another great tool; AutoMapper. It’s a framework that do all of that tedious mapping code you’re doing in for ViewModels or Messages in services. Boring and tedious to write and test. AutoMapper takes care of that – using a lot of Conventions.

Be sure to see the screencast that introduce a lot of the possibilities.

SpecFlow: BDD .NET-style

As you could read in my latest post I have be a bit frustrated with TDD and where to start, lately. BDD is of course the answer to that. But I must say that the frameworks are available to the .NET crowd is a bit weird. Either you have some really funky syntax (hey Anders, a new colleague and great guy) or it’s build on top on other stuff and where hard to work with.

I simply cannot see myself introduce any ordinary programmers to any of that.

But here is something that looks more like it… a bit at least; SpecFlow. It’s also built with an eye too RSpec, Cucumber and Ruby but build in the style of .NET and C#.

Here is a (silent) screencast, something about syntax and workflow and some great resources.

From this it even looks that they support Swedish… Great work guys!

I’ll be sure to look into this a bit more. Later. New assignment today.

Thursday, January 28, 2010

ASP.NET MVC, StructureMap and … TDD?

I’ve been playing around a bit with ASP.NET MVC and StructureMap (an IOC container). It all looks very nice and works wonder. During this I ran into an excellent blog post by Elija Manor on wiring StructureMap and ASP.NET MVC together. Beware of the favicon-problem though.

Again – i use NHibernate and Fluent NHibernate which so much nicer than the XML-stuff. The critics to Fluent NHibernate says that you cannot reach all functionality from Fluent NHibernate, but here is an example on how to set specific properties in your configuration. Helped me through this example.

Also found some great code examples from the TekPub NHibnernate series here.

OK – I've added “TDD?” in the title. I love TDD and it’s my preferred way of doing code, but I have a problem (to quote a thinker). I think TDD doesn’t help my through the broader strokes of my application.

Where do I start? How do I use the test to know that it’s time to add an repository, or an IOC Container? Do I TDD the IOC-code?

I asked the Swedish ALT.NET group a question and got some great tips, mostly they pointed me to BDD and those ideas. I also liked the idea of the Walking skeleton.

But I’m still confused. Can TDD really help you to design a system from the bottom up, or top-down (BDD) for that matter? Will the design be improved by the TDD-design technique? I’m not sure.

For now, my thinking is that it’s better to create a very simple design (MVC + repositories + IOC) to get your “skeleton to walk”, maybe with a functional/integration test that verify the functionality.

With that foundation in place it becomes easier to add some meat to the bones (is this skeleton metaphor taken too far yet?) with the normal TDD techniques and patterns.

Friday, January 22, 2010

Visual Studio 2010 web.config transformation

I happened to run into a feature I didn’t know of… Visual Studio 2010 (beta still… soon RC) includes a function for managing different .config-files for different environments. And support for transforming them on build/publish.

Here is a MSDN-article that introduces the concept and use it. And here is an article on the subject.

Pretty cool since up to now you’ve had to do it manually with build-tasks… Not so trivial.