Archive

Posts Tagged ‘VS Tools’

Debugger Canvas

May 26, 2011 2 comments

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.

Debugger Canvas

For more information please visit the Microsoft Research website.

Code Contracts

September 1, 2010 4 comments

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.

Step 1:

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.

Step 2

Start Visual Studio go to project properties and make sure you have the “Code Contracts” as the new tab in the properties.

Code contract tab in Visual Studio

Step 3

Lets gets started with writing some code

using System;
using System.Diagnostics.Contracts;

namespace CodeContract
{
    public class ProductRepository
    {
        public void  AddProduct(Int32 productId,string description)
        {
            if (productId <= 0)
            {
                throw new ArgumentException();
                }
            //… Rest of the implementation …
        }
    }
}

and write a simple test for the add method.


[TestMethod]
public void Test()
{
       var dc = new ProductRepository();
       dc.AddProduct(10, "Chai");
}

Now when we build the test and run it there are no surprises and the test passes with flying(green) colours.

Test Build

Lets say instead of passing a positive values we pass a negative one and as expected the test failed and got our exception.

Test Failed

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 ..

using System;
using System.Diagnostics.Contracts;

namespace CodeContract
{
    public class ProductRepository
    {
        public void  AddProduct(Int32 productId,string description)
        {
            if (productId <= 0)
            {
                throw new ArgumentException();
                //return;
            }
            Contract.EndContractBlock();
            //… Rest of the implementation …
        }
    }
}

All we have done is just added one method “EndContractBlock()” of the Contract class right after out exception and lets built the test project.

Code contract reports a warning
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.

LinqPad

August 16, 2010 1 comment

I know you guys have already heard a lot about LinqPad and how great it is. If not here is a blog about LinqPad and it’s a great tool for learning LINQ.

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.

Today I am going to show how to use external assembly or custom assemblies to use in this code snippet IDE. For illustration sake I’m going to use the JavascriptSerializer class to see whether my dotnet class will get serialize or not as a JSON Object and if it serializes then how would my JSON object will look like

So let’s assume you have downloaded and install LinqPad successfully. In order to add an external assembly click Query > Query Properties > Additional References.

Linq Pad Query Properties

Click on “Add” to add a new assembly.

Adding a new Assembly

You can type in the assembly you want to add and Autocompletion will take care of it.

Select Assembly

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.

Namespace import

Lets write some code…

Code block

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”.

C# Expression option

Now when we run the program it shows the desired result.

JSON Object

JQuery Snippets

August 12, 2010 1 comment

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.

A couple of weeks ago I blogged about the free JQuery ebook and I also blogged about how to roll out your own code snippet using the code snippet designer.

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 the HTML Snippets folder

Under JScript 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”

Just type in jq

and when we hit tab twice the snippet generates the base jquery code, it’s that simple.

Code generated by the snippet

Now it’s not too scary to get use to of jQuery syntax .

The Art of Mocking – Part 3

August 10, 2010 Leave a comment

Before completing the trilogy let’s have a quick recap on what we have done so far.

The Art of Mocking – Part 1
The Art of Mocking – Part 2

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.

Lind Table class

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.

Group Items Plugin

A friend of mine showed me this plug-in for Visual Studio 2010 which is great for grouping items.

Grouping items plug-in

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.

Power Commands for Visual Studio

June 9, 2010 1 comment

Check out the PowerCommands for Visual Studio 2010 which Scott Hanselman has blogged.

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

Follow

Get every new post delivered to your Inbox.