Toolbox

Code Exploration, SQL Queries, and More

Scott Mitchell

Contents

Analyze and Graphically Explore Your App’s Architecture
Upgrade Your SQL Query Editor
Take Screenshots Quickly and Easily
Developing .NET Apps

Analyze and Graphically Explore Your App’s Architecture

Large Microsoft .NET Framework applications can contain hundreds or thousands of classes organized into different assemblies that constitute distinct layers in the application’s architecture. While most developers work exclusively with a particular section of the architecture, senior developers and system architects are typically responsible for code and design decisions that impact various pieces of the architecture and their interconnections. For these code changes and architectural decisions, it is vital that senior developers and system architects have a solid understanding of the architecture’s structure and interdependencies. Commonly, developers resort to picking through the source code, which is often error-prone and time-consuming.

NDepend Provides a Matrix that Shows the Dependencies Among Various Assemblies

NDepend Provides a Matrix that Shows the Dependencies Among Various Assemblies(Click the
image for a larger view)

Fortunately, there’s an easier way to explore a .NET Framework app’s architecture at a high-level view. NDepend version 2.0, by SMAC­CHIA.COM, is a tool that helps examine and analyze the metrics, complexities, and dependencies between the various pieces of an app’s architecture. Once an app’s assemblies and source code have been selected and analyzed, NDepend provides a rich user interface for exploring a multitude of metrics and information.

At the top of the interface is a treemap of the architecture’s assemblies. (A treemap is a graph in which the size of each element is dependent on its "weight.") The treemap depicts various metrics such as the number of lines of code or number of methods, which provides a quick way to gauge the relative complexity of the components in the architecture.

NDepend provides a matrix that shows the dependencies among the various assemblies. You can drill down to the assemblies’ classes, and from there to the classes’ methods. Right-click to see what namespaces, classes, or methods either directly or indirectly call upon or use the selected assembly, class, or method. The matrix can also help pinpoint cyclic dependencies.

NDepend’s most impressive feature is its query language, CQL, which is similar in syntax to SQL. CQL statements can be used for ad hoc reporting. The following CQL query returns the 10 namespaces with the most types:

SELECT TOP 10 NAMESPACES ORDER BY NbTypes DESC

CQL queries are also useful for code quality audits. This query produces a warning if there are any methods that have over 30 lines of source code (which indicates a complex method that should likely be refactored):

WARN IF Count > 0 IN SELECT TOP 10 METHODS WHERE NbLinesOfCode > 30

Along with its UI, NDe­pend can be accessed through the command line, making it easy to integrate into an automated build process.

Price: $411 for a single user license.

www.NDepend.com

Upgrade Your SQL Query Editor

While Enterprise Manager and SQL Server™ Management Studio provide an easy-to-use graphical environment for performing database-related tasks, their native query editors come across to many as simplistic text editors that lack modern features. There’s no IntelliSense® or integrated source control support, for example. Visual Studio® provides Database Projects for grouping CREATE scripts and other commonly used T-SQL within the IDE, but these serve more as a repository for database-related scripts than an interface for writing, testing, and running queries.

However, there are a number of third-party SQL query editors that offer the features developers today take for granted. One such product is ApexSQL Edit version 2005.03, by ApexSQL. ApexSQL Edit boasts a number of time-saving features, the most impressive one being the Object MemberLists, which provides functionality much like IntelliSense. Type a query, like "SELECT * FROM...", and immediately a drop-down list appears listing the database’s tables. This feature also extends to columns, variables, parameters, procedures, and so forth. Once you start writing queries using MemberLists, having to go back to an editor without it feels like running underwater. This feature alone could save you countless keystrokes and typos and makes ApexSQL Edit an interesting tool for any developer’s toolbox.

Write, Test, or Execute Queries

Write, Test, or Execute Queries(Click the image for a larger view)

Another time-saving feature is the Object Explorer, which lists details about the selected table, stored procedure, view, UDF, or other database object. This information includes the object’s data, T-SQL statements to drop and recreate the object, and its permissions and dependencies. There’s also a Notes tab where you can add any comments regarding the object. (These comments are stored in a database table that must first be added to the database.) For database tables, the Object Explorer also lists the table’s columns and indexes. The Object Explorer makes it easy to quickly look up information about a table, view, or other object that you are currently writing a query against.

ApexSQL Edit also offers SQL Server Reporting Service integration, built-in Unit Testing support, powerful find and replace features, customized SQL formatting, and much more. If you spend any significant amount of time writing, testing, or executing queries through the query editors shipped with Enterprise Manager or Management Studio, it’s time to upgrade your query editor and give ApexSQL Edit a try.

Price: $349 for a single user license.

www.apexsql.com

Take Screenshots Quickly and Easily

There are many scenarios in which a developer may need to take a screenshot. They make for great visual aids in documentation and are useful in capturing anomalies or error messages when testing and debugging. With Windows®, the Print Screen key copies the current window to the clipboard. The screenshot can then be pasted directly into a document or pasted in an image editor and saved in one of any number of image file formats. However, when taking many screenshots and saving them to files, the workflow—hit Print Screen; paste the screenshot into an image editor; crop the image, if needed; save the image to disk—is slow and repetitive.

There are a variety of freeware and third-party products available to remove this monotony and improve efficiency. One such product is Cropper version 1.9, by Brian Scott, which is written in C# with the source code freely available. Cropper can capture an entire window or a defined region. To capture an entire window, simply hit the Print Screen key. Double-clicking the Cropper icon in the task tray brings up a floating frame whose position and dimensions specify the capture region.

Cropper can save the screen capture to the clipboard, have it sent directly to the printer, or automatically save it to disk as a BMP, PNG, or JPG file. If the thumbnail feature is activated, Cropper will save both the full-sized screenshot and a thumbnail of a specified size. When capturing non-rectangular windows, Cropper can be configured to fill those transparent regions—such as the background behind the curved corners—with a particular color.

Price: Free; includes source code.

Developing .NET Apps

Most successful computer trade books either focus on a very specific subset of a technology or provide a broad overview of the entire technology. How to Code .NET, by Christian Gross (Apress, 2006), doesn’t fall into either of these categories. While its title implies that the book is a weighty tome on creating .NET applications, its mere 232 pages indicate otherwise. It neither focuses on a specific subset of .NET nor does it provide a sweeping overview. Instead, it provides 30-50 pages each on four disparate topics: Test Driven Development (TDD), low-level .NET runtime and framework information, text-processing tips, and C# coding tricks.

The book starts, appropriately, with the TDD section. There are no wasted words here, just a quick overview of TDD’s principles, a look at NUnit, and a discussion on writing tests. After this, the rest of the chapter explores solutions to common TDD challenges, such as applying the principles of TDD to a project you’ve inherited that was not developed using TDD. The second chapter, .NET Runtime- and Framework-Related Solutions, offers explanations, tips, and tricks on lower-level topics. For example, there’s a discussion on the difference between value and reference types, code examples illustrating the benefits of delegates, and a look at the Object class’s Equals and GetHashCode methods and when you should override them.

The third chapter provides some handy text-related solutions, such as discussions and code snippets for parsing and searching strings. C# Coding Solutions, the book’s lengthiest chapter, looks at a number of C# features and patterns, including inheritance, nullable types, overloaded functions, interfaces, and the factory pattern.

At 232 pages, How to Code .NET does not cover any topic in great detail and therefore is not intended for the reader who wants to explore these topics in great depth. But the book provides a well-written, informative look at a smattering of topics. Most intermediate .NET developers would benefit from reading How to Code .NET as it provides a number of useful tips and serves as a good starting point for exploring some of the more advanced .NET concepts.

Price: $29.99.

www.apress.com

Send your questions and comments for Scott to  toolsmm@microsoft.com.

Scott Mitchell, author of numerous books and founder of 4GuysFromRolla.com, is an MVP who has been working with Microsoft Web technologies since 1998. Scott is an independent consultant, trainer, and writer. Reach him at Mitchell@4guysfromrolla.com or via his blog at ScottOnWriting.net.