Mole : Visual Studio Visualizer

Visualizer’s are of great help when debugging your application.Visualizer’s essentially provide a the complete view of the data in separate modal dialogue window.I guess almost all of us have worked with DataSet’s and in order to see the data contained inside the DataSet , we hover our mouse cursor over the dataset variable and then select the magnifying glass.This in turn opens up a separate window , showing the content of DataSet.Visual Studio debugger comes with visualizer’s for plain text , HTML , XML based data , DataSet and its corresponding dependents.If you haven’t understood what visualizer’s are actually , then refer to the following screenshot.

SampleVisualizer

In the above screenshot , once the magnifying glass corresponding to “LastName” property is clicked , the actual value will be displayed in a separate window.This separate popup window is actually known as a Visualizer in Visual Studio.If you are interested in learning more about Visualizer’s the check out the following blog :- Custom Visual Studio Visualizer

Mole

I came to know about MOLE while reading an article in the blog Dr. WPF.Mole is also a visualizer built for Visual Studio.The team who have developed Mole have already published an excellent article on CodeProject.You can check out that article from following link :- Mole For Visual Studio - With Editing - Visualize All Project Types

Essentially Mole was targeted towards helping developers who where working on WPF.many of the feature's in Mole Visualizer like Logical Tree , Visual Tree etc helped the WPF developer’s in understanding the overall structure of elements in the application and also Mole helped them in viewing and understanding the corresponding XAML mark-up.

Mole has still a lot to offer to those people who are not working on WPF.And to know that lets start first by getting Mole visualizer into our Visual Studio.You can download the Mole visualizer dll from the download link provided at the CodeProject site by the team of Mole.Once downloaded copy and paste the Mole.Visualizer.dll at anyone of the following locations :

  • %Start\Documents\Visual Studio 2008\Visualizers\
  • %Root%\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers\

Mole helps in visualizing following three things:

  • Current state of any custom object
  • Current State of any Collection in the code
  • Current state of any collection which is in turn inside some custom object

Lets take up each of the above mentioned points one by one.Before that , I will provide one small sample code which will help in explaining the usage of Mole even more clearly.Here’s the code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wintellect.PowerCollections;

namespace MoleVisualizerText
{
class Program
{
static void Main(string[] args)
{
Person adam = new Person("Adam", "Smith", 23);
Person mark = new Person("Mark", "Ion", 29);

List<Location> adamLocation = new List<Location>();
adamLocation.Add(new Location("London", 2, LocationInterest.Yes));
adamLocation.Add(new Location("Tokyo",3,LocationInterest.Yes));
adamLocation.Add(new Location("Manila",1,LocationInterest.No));
adam.Location = adamLocation;

Console.WriteLine(adam);
Console.WriteLine(mark);

Console.ReadLine();
}
}

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public List<Location> Location { get; set; }

public Person(string FirstName, string LastName, int Age)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.Age = Age;
}

public override string ToString()
{
return FirstName + " " + LastName + " " + Age;
}
}

public class Location
{
public string LocationName { get; set; }
public int NumberOfYears { get; set; }
public LocationInterest Interest { get; set; }

public Location(string Location, int TimeSpent, LocationInterest interest)
{
this.LocationName = Location;
this.NumberOfYears = TimeSpent;
this.Interest = interest;
}
}

public enum LocationInterest
{
Yes,
No
}
}

Following are the key points of above mentioned code

  • Person class used to represent a given Person.
  • Person class contain fields like FirstName , LastName , Age to hold the respective value’s.
  • Also Person Class contain property called “Location” which is a collection of type “Location”.
  • Type “Location” is used to represent the various location wherein a given person has stayed , for how many years he/she stayed there and finally did he/she really enjoyed staying there or not.In order to represent the last point I have used an Enum called “LocationInterest”.

Current state of any custom object

In order to view the current state of a particular object in your code in Mole , you will first have to put a break point on any line of your code.

image

Once that's done , then run the application and wait for the breakpoint to be hit.At that moment open the watch window and add the following expression in watch window : new WeakReference(<name of the custom object>).In my case I have created an instance of Type “Person” and named it as “adam”.So the expression in watch window becomes new WeakReference(adam).Following screenshot will explain it more clearly.

image

So with the above declaration , I have attached a watch expression on “adam” which is an instance of type “Person”.Finally click on the magnifying glass to open the Mole visualizer.

image

Mole visaulizer has got lots of options and covering them in this article is not possible.Fore more detailed explanation you can check out the article on CodeProject site from the team of Mole.In the above diagram , we can see the current state of “adam” instance.It clearly explains that the properties like FirstName , LastName , Age have been set but the last property i.e. Location is null and has still not been initialized.

If you think this is just normal , then here is the sweetest part.Mole actually remembers the instances which has been added in the watch expression , even in case of application restarts.So once you have added “adam” in watch window , you can view its state at any point of time while debugging the application.

Current State of any Collection in the code

Similar to viewing custom objects , you can also view collections (List , Dictionary etc) in Mole.And the way to do is almost same as that of custom objects which is explained in the above segment.In the above mentioned code , I have defined “adamLocation” as a collection of type “Location” to hold the details of location adam has visited.So I will attach adamLocation in watch window.

image

Once done , click on the magnifying glass to open the Mole visualizer to view the various items in the adamLocation collection.

image

Since I have added details about three locations , I can see three entries like [001],[002],[003] in the visualizer.To see the exact details , click on anyone of the hyperlinks of Value field.

image

Since I clicked on item number [001] , I can see the corresponding details.So with Mole , viewing the details of your collection is so easy.

Well but that's not all.If viewing of details is concerned , then the next view provided by Mole will really take you by surprise.Lets see …..

Current state of any collection which is in turn inside some custom object

Person adam = new Person("Adam", "Smith", 23);

List<Location> adamLocation = new List<Location>();
adamLocation.Add(new Location("London", 2, LocationInterest.Yes));
adamLocation.Add(new Location("Tokyo",3,LocationInterest.Yes));
adamLocation.Add(new Location("Manila",1,LocationInterest.No));
adam.Location = adamLocation;

In the above code,I have created an instance of type “Person” and named it as “adam”.Then a collection of type “Location” and in last line I assigned that collection variable to the Location property of “adam” instance.Finally “adam” instance has some static information and a collection of locations where he has stayed.

Following the same way as we did in previous two steps , put a breakpoint , add the “adam” instance in watch window and once the breakpoint is hit , click the magnifying glass corresponding to the “adam” instance in watch window.Following is the screen shot of the Mole visualizer.

image

The view looks pretty ordinary.Here click on the “Collection” hyperlink .Once clicked view changes to the following :-

image

Interesting thing to note here is that the “View Data” button is now enabled.Click on the button to open a new popup window , which displays the details of the Location collection in a nice , tabular manner.

image

I think this is the biggest advantage of Mole i.e. showing the details of collection in a nice , readable manner that too with very few minimal clicks.

Mole is quiet a big enough to be covered in a single article.And I also say that the coverage of Mole has already been done in an excellent manner in the official article by the team of Mole.I wrote this article to highlight one of the feature of Mole which I really liked and I guess everyone else will like it too.

Contact Me

rk.pawan@gmail.com | +1-737-202-7676 | LinkedIn | Facebook | Github |

2 Comments

Comments have been disabled for this content.