Monday, May 18, 2009

NDepend - Code Anaysis Tool

Static code analyzers simplifies the management of a complex .NET code base. Code analyzers help architects and developers analyze code structure, specify design rules, plan massive refactoring, do effective code reviews and compare different versions of the code. The result is better communication, improved quality, easier maintenance and faster development.

Last week Patrick Smacchia, a C# MVP offererd me a pro license of NDepend to use the tool and buzz it on my blog if I find it useful. Thanks Patrick for introducing me to a great tool.

Compared to the existing code anlaysis tool NDepend has some unique feature that help you anlayze every aspect of your code and produces informative reports with detailed matrices, diagrams and charts that should help you improve your code. You can download and use a free trial version of NDepend from this link.

Some of the features that are unique and very useful are

Code Metrics Statistics - NDepend provides a full list of Code Metrics that let you analyze your developed application in a moment.

CQL - Support for Code Query Language (CQL) is really great. It helps display some information related to a specific matrix or set of matrices. CQL will help you answer in seconds your questions about your code base: Eg:

- Which public methods have more than 30 lines of code?


- Which classes implement System.IDisposable?

SELECT TYPES WHERE IsClass AND Implements "System.IDisposable"

- Which methods have been refactored recently and is not thoroughly covered by tests?

SELECT METHODS WHERE CodeWasChanged AND PercentageCoverage < 100

Assemblies Abstractness vs. Instability Diagram: This diagram simply compares instability of a project with its abstractness.diagram helps you see the dependency between your project and different .NET assemblies.

Integration: NDepend integrates well with Visual Studio and Reflector. This helps me to get a anaysis at development time also.

Saturday, May 9, 2009

Using Visual Studio 2008 Proxy Generation Tool to Create Custom Collections

The <CollectionMappings> section in the WCF mapping file (Reference.svcmap) allows you to specify your collection type to be generated via the Proxy when using the Visual Studio 2008 WCF Proxy generation tool.

E.g.: When creating application using WPF, I would really require my collections to be of ObservableCollection type rather than the List which my service method returns. I can force my Proxy Generation tool to create ObservableCollection types instead of Arrays by following the steps given below

1. Right Click Service Reference and Select Add Service Reference. Provide the service location details and generate the proxy.

2. Click Show All Files button on the solution explorer and expand the service reference and look for the Reference.svcmap file.

3. Under the <ClientOptions>node you will see <CollectionMappings>.Replace the contents to Map it to the System.Collections.ObjectModel.ObservableCollection as given below.


<CollectionMapping TypeName="System.Collections.ObjectModel.ObservableCollection`1" Category="List" />


4. Right Click the service reference and select Update Service Reference.

You can follow the same approach for generating your custom collections instead of the ObservableCollection type.