I am really excited to post about this new tool called Debugger Canvas which adds an awesome visual experience to your debugging.
Here is a quick video demo and it is planned to be shipped around June.
For more information please visit the Microsoft Research website.
I am always up for a new ideas and experimenting new stuff and one of the cool stuff I always read in “What’s new in .net 4.0” is the code contract. I mean it definitely sounds familiar as WCF have various forms of contracts, interfaces are contracts etc.
I started digging a bit into it and found out that it sits inside the “System.Diagnostics.Contract” assembly and that give me a clear direction where was this heading. For me I wanted to check out how much of code checking it can do before we execute the code.
But like most of the new and cool stuff they are not that easy for the first time and there are a lot of gotchas and hoops you have to go through before you can start using it.
So here is my set of steps to get started with it.
Download it from here. The first gotchas was here itself for “Static Checking” download the Premium Edition and for that you must have Visual Studio 2008 Team System, Visual Studio 2010 Premium Edition or Visual Studio 2010 Ultimate Edition.
Start Visual Studio go to project properties and make sure you have the “Code Contracts” as the new tab in the properties.
Lets gets started with writing some code
and write a simple test for the add method.
Now when we build the test and run it there are no surprises and the test passes with flying(green) colours.
Lets say instead of passing a positive values we pass a negative one and as expected the test failed and got our exception.
I am glad that we wrote the test and it was caught before the code went into the production but wish if there was some tool which could tell me about this exception even before I could execute the code. Sounds crazy isn’t it ? ( reminds me of Minority Report where the perpetrators are caught before they have committed the crime ).
No the idea is not that crazy and lets see how we can use Code contract to anticipate these type of code pitfalls.
Go to the Static Checking section and check the following checkboxes
- Perform Static Contraqct Checking
- Check in Background
- Show squigglies
and in Runtime Checking check the “Perform Runtime Contract Checking”. now we will add a bit of magic into our “Add Product” method ..
All we have done is just added one method “EndContractBlock()” of the Contract class right after out exception and lets built the test project.
And this is pure magic. The Code contract reports a warning since we are violating the value of the passed parameter with a blue squiggly as we turned that option on.
As you can this is just a tip of the iceberg and imagine what you can achieve when you started define these contracts using annotation and decorating your classes with it, this will definitely make your API/library and your testing more robust.
The other area where I see a lot of use could be of a legacy .NET app which doesn’t have good unit test or any test at all, you can assert these contract block to identify various defects in the legacy .NET app.
But one of the cool things about LinqPad is that it is a great code snippet IDE too , with LinqPad you will never ever have a need to write a Console Application for a quick prototype.
So let’s assume you have downloaded and install LinqPad successfully. In order to add an external assembly click Query > Query Properties > Additional References.
Click on “Add” to add a new assembly.
You can type in the assembly you want to add and Autocompletion will take care of it.
Import any additional namespace you might have with in the selected assembly. Like in my case I am importing the System.Web.Script.Serialization namespace.
Lets write some code…
This is one gotchas I want to highlight when you first start using LinqPad. When you hit the run button you see it says “Error compling query” and it took me a while to figure it out. It’s complaining becasue in LinqPad by default it assumes you are trying to execute some expression so lets change it to “C# program”.
Now when we run the program it shows the desired result.
If you are web developer or thinking into moving to the lighter side (with a pun ) then you simply can’t ignore JQuery and in my personal opinion invest some time and energy into it and it will pay you off in the long run.
This post is about the jQuery snippets which I think is a great way of learning jQuery syntax. You can download it from here. Once it’s downloaded unzip the file, run the msi and complete the installation.
Before we start using the snippets lets browse to the “Documents\Visual Studio 2010\Code Snippets\Visual Web Developer” folder and as you can see there are two folders “My HTML Snippet” and “My JScript Snippets”.
Under “HTML Snippets” folder
Under JScript Snippets” folder
If you open one of these snippet files you can see it is quite similar to what we did using the snippet designer and shows the full potential of using snippets in your own development environment.
Lets start using the snippets and get some jquery code in our page. Below is a simple html page and when we type the keyword “jq”
and when we hit tab twice the snippet generates the base jquery code, it’s that simple.
Now it’s not too scary to get use to of jQuery syntax .
Before completing the trilogy let’s have a quick recap on what we have done so far.
One of the biggest gotchas for mocking framework is that you can’t mock a sealed class and can only mock either an interface or an abstract class. If you every try doing it to a sealed class you get this exception.
Type to mock must be an interface or an abstract or non-sealed class.
We learned in the previous posts how we can design our classes using SOLID and TDD principle so that you can use mocking framework to test it in isolation, but how about a piece of code which has a dependency on an existing .NET framework class which is sealed or doesn’t provide an interface or an abstract. How would you approach mocking with that.
For example we had a lot of problems when we were using LinqToSql as it’s DataContext class does not provides and interface and the System.Data.Linq.Table class is a sealed class and the work arounds were not that great.
It’s an interesting problem and this is where an interesting framework comes into picture and it’s called Moles Framework. Moles is part of the Microsoft pex and moles research project, it can be downloaded from visual studio gallery but for PEX you need a MSDN subscription.
I haven’t played with it yet but through the documentation I could see what its potential is, and if I come across something which I can’t mockup I will use it rather extracting interfaces and working on workarounds.
A friend of mine showed me this plug-in for Visual Studio 2010 which is great for grouping items.
and you can read all about it at Mokosh Blog post.I really like to group various interfaces and its implementing classes together as shown above.
The power commands tools are really good productivity tool and I have been using it since VS2008 . One of the main features i like about this tool is “Copy Reference” which comes very handy at times working through big project restructure. Have a quick browse through the “Gallery” & you never know you might find something interesting