Showing posts with label tools. Show all posts
Showing posts with label tools. Show all posts

Sunday, 6 September 2015

Practice Test Driven Development with C# Exercises on Exercism.io

Image

I was recently introduced to Exercism.io which is a place where you can download a number of C# exercises to work on. Exercism.io not only supports C# but also other programming languages. Each exercise comes with a pre-written suite of unit tests and it is your task to implement the required functionality in order to make the unit tests pass.

The exercises are a good way to get familiar with Test Driven Development (TDD) without having to write the tests. You can still semi-follow the prescribed red-green-refactor approach, where you initially have a test that fails, you then write an implementation to make the test pass and then you refactor your implementation until you're happy with it.

The Exercism.io website provides a useful command line interface tool which you use to fetch exercises and then also submit your solutions. Once a solution is submitted, it is made available on Exercism.io for others to "nitpick". This is a great feature where others who have done the same exercise can view your solution and suggest improvements. Although I've found the C# community on Exercism.io to be fairly quiet (with only a few people nitpicking), I can see this being a very useful feature once more people start to use the site.

If you've also heard of Project Euler (which I blogged about here), then yes, Exercism.io is similar in principle, but there are differences which you'll find quickly after trying both sites. I've tried a few exercises in the C# section of Exercism.io - they are fun and a good to practice your C#/.NET framework knowledge. Highly recommend this site to anyone who enjoys small programming exercises in their spare time and anyone who is interested in trying TDD. You can get started here.

Wednesday, 15 July 2015

Using Gacutil on a Non-Development Server

A short while ago I came across an issue with using the Visual Studio gacutil tool on a non-development server. By non-development server, I mean a system which does not have Visual Studio installed on it with the associated .NET Software Development Kit (SDK). I thought it may be useful to share the problem and solution here in case anyone else runs into the same issue.

The gacutil tool is part of the Visual Studio SDK and is used to install a strongly named assembly to the Global Assembly Cache (GAC). In my case, I had written a .NET 4 class library which was to be consumed by an application which lives on the same machine. As part of my deployment testing, I used the gacutil tool through the Visual Studio Developer Command Prompt to install the assembly in my local GAC. The result was successful as shown in the screenshot below.

Image

I then had to install the same class library in the GAC of a test server. The test server had .NET Framework version 4 installed but because it doesn't have Visual Studio installed, it did not have the Developer Command Prompt and gacutil on the server. I then located the gacutil.exe and gacutil.exe.config files from my laptop file system, the location in my case was:

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools

I copied the gacutil.exe and gacutil.exe.config files onto the test server and opened command prompt (cmd.exe) as administrator. Running the gacutil install command yielded no success response from gacutil, as shown in the screenshot below.

Image

Surprisingly, gacutil gave no feedback as to what happened. After doing a bit of online research, I learned that gacutil has a dependency on a dll file named "gacutlrc.dll". In my laptop file system, I found this dependency in the folder location:

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\1033

I then created a folder named "1033" in the same directory that I copied gacutil.exe to on my test server. I copied the "gacutlrc.dll" file into the 1033 folder and executed the gacutil install command with a successful result.
Image

In summary, if you are using the gacutil command on a non-development server, ensure you have the following in a single folder:
  • gacutil.exe and gacutil.exe.config
  • a folder named "1033" with the gacutlrc.dll file within it
It should then just be a case of opening a command prompt window as an administrator, navigating to the directory containing gacutil.exe and running the gacutil /i [path to assembly] command.

Friday, 19 December 2014

DotNetZip - Zip Compression and Decompression .NET 4 and Earlier

Quick post about a useful third-party zip compression library if you're working in .NET framework version 4 or earlier.

I am working on a SQL Server 2012 Integration Services (SSIS) package. One of the steps in the package is to decompress a set of zip archives. Unfortunately, .NET script tasks in the SSIS package can only target .NET framework version 4 and earlier. This means that I couldn't make use of the new zip compression classes introduced in .NET 4.5 (see System.IO.Compression).

Fortunately though, there are a handful of open source .NET zip libraries available. The one I opted for is called DotNetZip. DotNetZip has an intuitive API and is working well with a large number of files (I am decompressing approximately 15,000 archives). The library is available as a NuGet package. The two snippets below show just how easy it is to compress and decompress files using zip.

To compress a file into a zip archive:
using (var zipFile = new ZipFile())
{
    // The empty string parameter ensures the file is archived to the 
    // root of the archive
    zipFile.AddFile(@"C:\Test\Data.txt"string.Empty);
    zipFile.Save(@"C:\Test\Data.zip");
}

To decompress files out of a zip archive:
using (var zipFile = new ZipFile(@"C:\Test\Data.zip"))
{
    zipFile.ExtractAll(@"C:\Test\Output");
}

Sunday, 10 August 2014

C# Static Code Analysis with NDepend

I was fortunate enough to recently obtain a Pro license for NDepend. If you've never heard of NDepend, it is a powerful .NET code analysis tool which provides useful information to support you in writing better code. As I work on a large codebase, my initial worry was that NDepend may slow down my user experience with Visual Studio. However, after using it for one month, I have not come across a single moment in which NDepend slowed my IDE experience.

I'll quickly admit that I have no extensive experience of using code analysis tools, therefore I can't comment on how NDepend compares to other tools in the market - but I can comment on the features that I've found very useful. A quick glance on the documentation section of the NDepend website shows the powerful feature set that the tool supports. Below I briefly go through some of the features that I've found useful to date. I still feel that I've barely scratched the surface of some of the features that NDepend supports (particularly CQLinq), so the hope is to come back and update this post as and when I find something else I like.

Visual Studio Support and Integration - NDepend supports all the major versions of Visual Studio (2008, 2010, 2012 and 2013). I installed NDepend (the Pro license) on Visual Studio 2010 Professional Edition and have had no issues since installation. I have also installed a trial version of NDepend on the more later Visual Studio 2013 and found that it works just as good. The installation process was quick and straight forward. I particularly like the non intrusive nature of the tool - after installation, you get an "NDepend" menu option in Visual Studio. From this menu you can run an analysis on your projects. There is also a small circle icon which appears in the bottom right corner of Visual Studio - if you hover your mouse over it you'll get quick access to certain options (like running an analysis or viewing the dashboard) and also information on the number of NDepend code rules violated (more on Code Queries and Rules below).

NDepend Dashboard - Once an NDepend analysis completes, you get the option to view the NDepend Dashboard. The dashboard itself opens within Visual Studio as a new tab and provides a comprehensive array of information. Some of the information you get is:

- Lines of code (split by the number of lines you've written and the number of lines not written by you)
- Method complexity information
- Quantity information on assemblies, namespaces, methods, fields, source files and lines of comments
- Code coverage by tests
- Third party usage
- Information on violated code rules

Note that NDepend also is able to create a report in HTML which provides all this information and more. I imagine that saving "snapshots" of these reports over time would really help in assessing how your codebase is growing in terms of quality over time.

Code Rules and Queries - Perhaps one of the main features which makes NDepend so powerful is CQLinq. CQLinq stands for Code Query LINQ which allows you to query your codebase using LINQ-based queries. NDepend comes with a large number of predefined queries which give you useful information out of the box. Using the "Queries and Rules Explorer" panel you can see the CQLinq code for each query and also define your own query. You can think of CQLinq as a way to very easily reflect on your codebase using simple LINQ-based syntax that you'll already be familiar with. CQLinq provides a very useful mechanism for you to extend NDepend based on the information you want to extract from your code base. It also opens the opportunity for you to share your CQLinq queries with other NDepend users. You can find out more about CQLinq here.

Additional Visual Studio Features - In addition to its core features, NDepend provides a number of extra useful features like code diffing, dependency analysis, visualisation of code metrics and richer code search features. From what I understand, NDepend search is essentially a user interface over CQLinq.

As mentioned above, I'm a fairly new NDepend user. I find it an intuitive tool to use and quickly found myself benefitting from the information the tool provides. I'm particularly now interested in trying out NDepend on a greenfield project and seeing how it affects and shapes the day-to-day development of a new codebase. The features mentioned above are a very small subset of NDepends full feature set. If you're interested in trying out and knowing more about the tool - you can download it from here and read about all the supported features here. I also encourage you to go through the very useful documentation part of the site.