Test Method Snippet

I created a useful code snippet that I thought I would share. The shortcut is tm and the snippet outputs the following text, giving you the skeleton for a unit test:

[TestMethod]
public void TestMethod()
{
	//  Arrange
	
	//  Act
	
	//  Assert                        
	
}

You can download that snippet by clicking the following button:

Also, I got sick of typing mvcaction4 every time I wanted to scaffold out an Action Method. So I cracked open the snippet file for that snippet using the a text editor and changed the contents of the shortcut element to mac4. Much easier to type and very easy to remember as well!

MembershipReboot Presentation for ADNUG

On Wednesday night I delivered a presentation about MembershipReboot, a claims-aware authentication library for ASP.NET. Here are some links relevant to the content, in addition to the PowerPoint presentation which I used:

Powerpoint presentation:

Hitting Host Machine’s SQL Server from within a VM

I had a situation at home where I was running some code in Visual Studio on a virtual machine. The virtual machine was running using VMWare Player on my desktop machine.

I needed to access the database which was running on my host machine (the desktop machine) from within the VM. So I had to come up with a Connection String which was able to target the host machine. The following Connection String achieves that:

<add name="DefaultConnection" connectionString="Data Source=192.168.25.1,2310;Network Library=DBMSSOCN;Initial Catalog=AdventureWorld;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

Note, I had to open port 2310 on the host machine’s firewall.

Now, I don’t need to install Sql Server on my virtual machines (which always seems to be a pain in the proverbial).

NDepend

I’ve wanted to take a look at NDepend for quite a while now. So today I am going to write about my initial impression of this excellent static-analysis tool. To get an immediate feel for what NDepend is and what it does, there is a short video that can be viewed here.

Managed code comes with many advantages. And one of the most significant (possibly the most significant) is the mountain of meta-data that .NET assemblies carry about themselves, and those that they interact with. This plethora of information can be tapped, analysed and presented in a meaningful way which gives coders/architects a great, bird’s eye view of the state of a given code base.

This post will not be a tutorial. But it will contain screenshots and some explanations. It will fall short of being a tutorial, because I am not setting out steps from end-to-end. Think of it as a summary of one developer’s experience analysing a real .NET solution with NDepend. The solution that I will analyse is my Home Library sample application (which is nowhere near finished and currently in a state of rampant architectural experimentation).

The first thing I wanted to check out once I had run the analysis on my solution (including all of its projects) was the metrics stuff. That looks interesting to me and I immediately decided to hone in on the Most Complex... - Types metric. When you select that from the NDepend menu (NDepend > Metric > Most Complex… > Types), it loads 2 separate panes:

  1. Search Results pane
  2. Metrics pane (which shows a treemap of the results)

Once I got my head around what I was looking at, it occurred to me that it was showing me the same information, but through different prisms:

Panes

Figure 1 – Metrics and Results Panes

Looking carefully at Figure 1, we can see that the way in which that information was retrieved was by way of the query that can be seen in the Search Results pane. Now that is pretty awesome, because NDepend has its own version of LINQ called CQLinq, which is used to pull out data from the static analysis (I am not sure if it is a fully-fledged LINQ provider, as I haven’t seen the word “provider” in any of their doco – but still über cool to have your own flavour of LINQ). Their documentation about syntax etc is a very good reference for writing CQLinq.

If you hover over the various methods in the tree-view (at the foot of the Search Results pane), you will see the corresponding rectangle in the treemap light up. You can see in Figure 2 that
I have honed in on the ExpressionBuilder type, as I do recall that carrying quite a bit of complexity. By the size of the rectangle in contrast to the rest of the treemap, that complexity is represented well. Obviously, the UI-related types in this WinForms app have the most complexity, each with quite a large Initialize method for all the controls on the forms.

Hover Cause and Effect

Figure 2 – Hover Cause and Effect

Another excellent feature in the Metrics pane is a drop down list which filters the top n items which match the query. In fact, if you look over to the right in the query window, you can see Take(10) has been added to the CQLinq query in Figure 3:

Top n

Figure 3 – Top n

I was correct in my feeling about the ExpressionBuilder class, as it is featuring in that top 10. If you click on the Edit Query button in the Search Results pane (visible in Figures 1 and 2 above), it launches a pane with a text-box for query manipulation. You can then add a new query, which is a pretty cool extension point. But that’s just the tip of the iceberg when it comes to customising NDepend to your needs.

The other feature that I wanted to highlight, as something that I found really cool, is the ability to see Dependency Graphs of your code. Figure 4 shows a graph I created from the NDepend menu NDepend > Graph> View ApplicationAssemblies Only:

Dependency Graph Assemblies

Figure 4 – Dependency Graph Assemblies


That’s a really nice representation of my architecture (just as I envisioned it). You can see the UI stuff towards the left, and then as you move through the middle (the domain), you eventually hit the data stuff on the right.

You can also create one for namespaces. Check out Figure 5 below:

Namespaces

Figure 5 – Mutually Dependent Namespaces


I’ve highlighted a problem there, which the tool found, regarding mutually dependant namespaces. The tooltips (the light-yellow boxes) provide some information to assist me in hunting down the reason for this unseemly coupling issue. This is a very valuable view of the relationship between your objects. You can also drill down further by right-clicking on the connector “edges” between the nodes in the graph to look at member dependencies etc.

The really cool thing about those tooltips is that they are dynamic. When you change a query or selected-item in a drop-box, the data displayed in those tooltips changes before your very eyes.

There are a heap of other things NDepend offers, some of which will have to be the subject for a future post.

But I’ll leave the reader with this – the developers of NDepend use NDepend to develop it. To use their words, they eat their own dog-food. And a very high quality kibble it is.

WebAPI Method Returning 404

There is something I absolutely hate when programming. When you hit a bump in the road that brings everything to a halt; and it is something which should definitely not be getting in the way. I hesitate to call them bugs. It’s just those little annoyances where I have overlooked some little thing and it has cost me hours.

Welcome to my day today. I had an ASP.NET MVC application which, when I created it, did not have the WebAPI stuff in the solution (I deliberately unchecked the checkbox which would have included all that stuff). So, when I decided that I did, in fact, want to use the WebAPI, I manually added all the necessary nuget packages etc.

There was one thing I got wrong. And troubleshooting this took me around 1.5 hours. Apparently, you need to register the WebAPI routes before the standard MVC routes:

protected void Application_Start()
{
  AreaRegistration.RegisterAllAreas();
  FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  GlobalConfiguration.Configure(WebApiConfig.Register); // this 1st
  RouteConfig.RegisterRoutes(RouteTable.Routes); // then this
  BundleConfig.RegisterBundles(BundleTable.Bundles);
}

I’m not saying it shouldn’t be that way. It’s just that, when you are trying to get that thing, which should have taken 5 minutes, working 90 minutes later, you get a little peeved. Especially when you hear the clock ticking and time slipping away. In effect, it’s not even having wheels on the ground until the annoying oversight is rectified.

Filing this one under stuff I do not want to forget. Ever.