Saturday, October 30, 2010

Fun with Mono.Cecil

I’ve always loved playing around with tools like Reflector and NDepend to take a look at the internals of my code base but with an external perspective than inside Visual Studio. 

Reflector was really useful to me to walk dependencies and hierarchies before I started using Resharper.  NDepend is an amazing piece of software that gives me a lot of metrics and other information.  Unfortunately maybe a bit too much, it’s hard to make head and tail a first glance.  I’ve never had the chance to play with the full version and the trial is very well done in the way you can use most of the software but it’s nagging you just one step before you could nail something really useful.

Now in Visual Studio 2010 we have the Dependency Graph which is really neat but difficult to use because of missing key features like a way to control or filter the noise of been force to graph the whole solution.  You always need to start pruning the graph from scratch and zooming on a single namespace and dependency is hard.  Never the less, with a bit of patience I can find a lot of information like dependency circles.  But again, when I find one I’m stuck with only visual information, no way to just extract a list of namespaces, types or methods to help me tackle the culprits.

So, that is where I am now.  I’d like to have something as powerful as Reflector to walk the links, complete as NDepend and Dependency Graph to get all the information I need to learn about, break bad dependencies but also prevent new ones from appearing in my code base.  Wouldn’t be nice to be able to query the code base for metrics, dependencies and general patterns in my code? Tests to help me keep my code clean?

Of course I know that NDepend got this CQL feature.  It’s really amazing but with the trial version of the software I can’t go far.  And to be honest the editor is a bit clumsy and the language a bit limiting.  No, what I need is a better experience, something really interactive to play with my queries until I get them right, something like the experience I get with LinqPad.  Then I can put the result of my efforts into a unit test to be sure I’ll won’t repeat the same mistakes again.

.Net reflection API is all good in theory but to use that I’ll need to be really cleaver because reflection works only by loading a dll in memory before querying it.  And due to the way the CLR works, a dll can’t be unloaded from the AppDomain meaning that you need to close the app every times you want reload a new version.  Too bad for that.  There are ways to go around that limitation but I want to keep it my solution simple.  So an alternative to reflection is Mono.Cecil.  I’ve been hearing a lot of good about this library in the past and I know that a lot of good software use it (like NDepend!) but I never took a look at it myself.

I think it’s time for me to give it a try.  I’ll blog about my experience with it trying to create a thin API on top of it to add metrics and a way to list all the bad dependencies in my code base.

Tuesday, November 17, 2009

Executable specifications podcast

I participated in a Visual Studio Talk Show podcast recording last week.

It is available now here (only in French sorry).

You can read more on the subject on the excellent site of Scott W. Ambler: Agile modeling.

Friday, October 16, 2009

Team Coverage with TestDriven.Net

I’ve been playing with NCover and NCoverExplorer Community Edition recently.  I also look at Team Coverage which is embedded inside Visual Studio 2008.

Rapidly, in understood that only MSTest is supported with Team Coverage.  I know that an add-in exists to support MbUnit tests but I did not look into it.  Actually, I always used TestDriven.Net so I’ve never complained about the fact that VS does not support MbUnit out of the box.

Then I remembered that TestDriven.Net proposed us a list of frameworks and tools to “Test With” in the context menu.  There you’ll find 2 options: Coverage and Team Coverage!

image

I first tried Coverage (with NCover) but the coverage results were empty and I’m not sure what went wrong exactly.  Next I’ve tried Team Coverage…  It worked on the first try, I got the data in the Code Coverage Results pane and code highlighting and everything else I had in NCoverExplorer!

image

From now on I’ll be using this trick when doing unit tests to check if I got it all covered.

Tuesday, October 13, 2009

Adding Resharper’s User File Template in the quick list

I tried to add a new file template for Resharper (shortcut ALT-INS in the solution explorer).  The template was for creating a new test class for MbUnit test framework.  Everything worked fine except that my new file template did not appear in the quick list.

I’ve looked online and found this post on Vadim’s blog, he was doing the exact same template that I was doing!!

Anyway, I’ve tried to call my new template via the Resharper menu and I found this option on one screen:

image image

After that I was able to use my new file template from the quick list (ALT-INS)

image 

I hope I only overlook something because I think this option should be available on the File Template edition screen (or in the Option like it used to be, look at Vadim’s post).

Update!!

After a few days I finally found out the easy (yet not very intuitive) way to modify the quick list.

In the Templates Explorer – File Templates (menu Resharper –> Live Templates…) on the left side is the available templates and on the right side is a preview of the quick list for C#, VB and other project types. 

To add a new template to the quick list simply drag the template on the left and drop it on the right side on the position you want.  To remove a template from the quick list just select it and click the delete button in the toolbar.

image

If you are curious here is my own File Template for MbUnit test fixture class

using System;

using MbUnit.Framework;

namespace $NAMESPACE$
{
[TestFixture]
public sealed class $CLASS$
{
#region Constructor

public $CLASS$()
{
}

#endregion

#region Test Methods

[Test]
public void Test()
{
$END$
}

#endregion
}
}