Each. NET developers should download 10 of essential tools

Source: Internet
Author: User
Tags assert constructor expression include versions volatile root directory visual studio
Download this article discussion:

• NUnit used to write unit tests

• NDoc for creating code document data

• NAnt used to build the solution

• Codesmith for generating code

• FxCop for monitoring code

• Snippet Compiler for compiling small amounts of code

• Two different converter tools: ASP.net version converter and Visual Studio. NET Project Converter

• Regulator for generating regular expressions

•. NET Reflector for profiling assemblies


The following techniques are used in this article:

. NET, C #, or Visual Basic. NET, visual Studio. Net

Unless you use the best tool you can get, you can't expect to build a first-class application. In addition to well-known tools like Visual studio®.net, there are many small, less-known tools available from the. NET community. In this article, I'll introduce you to some of the best free tools available for. NET Development. I will guide you through a quick tutorial on how to use each of these tools-some tools can save you a minute in many cases, while others may radically change the way you write your code. Because I'm going to cover so many different tools in this article, I can't discuss each of these tools in detail, but you should know enough about each tool to determine which tools are useful to your project.

Snippet Compiler
Snippet Compiler is a small, Windows®-based application that you can use to write, compile, and run code. This tool is useful if you have a small code snippet and you do not want to create a complete Visual Studio. NET Project for it (and all files that accompany the project).

For example, suppose I want to show you how to get from Microsoft?. NET Framework to start another application. In snippet Compiler, I'll start by creating a new file that will create a small console application. You can create snippets of code inside the Main method of the console application, and that's exactly what I'm going to do here. The following code fragment demonstrates how to create a Notepad instance from the. NET Framework:

System.Diagnostics.Process proc = new System.Diagnostics.Process ();
Proc. Startinfo.filename= "notepad.exe";
Proc. Start ();
Proc. WaitForExit ();

Of course the code fragment itself cannot be compiled, and this is where snippet Compiler is. Figure 1 shows this code example in Snippet Compiler.



Fig. 1 Snippet Compiler

To test the code fragment, just press the play button (the green triangle) and it will run in debug mode. The code fragment will generate a pop-up console application, and Notepad will be displayed. When you close Notepad, the console application is also closed.

As far as I'm concerned, I was trying to create a small example for someone who asked me for help to discover that snippet Compiler was so valuable-if you don't use it, I usually have to create a new project to make sure that every part compiles and then sends the code snippet to the caller, and delete the item. Snippet Compiler makes the process easier and more enjoyable.

Snippet Compiler is written by Jeff Key and can be downloaded from Http://www.sliver.com/dotnet/SnippetCompiler.

Regulator
Regulator is the last one added to my list of first class tools. It is a very distinctive tool that makes it easy to build and test regular expressions. There is a renewed interest in regular expressions because they are well supported in the. NET framework. Regular expressions are used to define patterns in strings based on character, frequency, and character order. Their most common use is as a means of validating user input validity or as a way to find a string in a larger string-for example, to find a URL or e-mail address on a Web page.

Regulator allows you to enter a regular expression and some input to run the expression against it. This way, before you implement the regular expression in your application, you can see what it will do and what kinds of matches it will return. Figure 2 shows the regulator with a simple regular expression.



Figure 2

The regular expression is included in the document-in the example, it is [0-9]*, which should match any number of numbers in a row. The box in the lower-right side contains input for the regular expression, and the bottom-left box shows the matches that the regular expression finds in the input. Writing and testing regular expressions in such a separate application is much easier than trying to handle them in your application.

One of the best features in regulator is the ability to search online regular expression libraries in regexlib.com. For example, if you enter the string "phone" in the search box, you will find more than 20 different regular expressions that can match the various phone numbers, including expressions for the UK, Australia, and many other phone numbers. Regulator is written by Roy Osherove and can be downloaded in http://royo.is-a-geek.com/regulator.

Codesmith
Codesmith is a template-based code generation tool that uses a syntax similar to asp.net to generate any type of code or text. Unlike many other code generation tools, Codesmith does not ask you to subscribe to a particular application design or architecture. With Codesmith, you can generate anything that includes simple, strongly typed collections and complete applications.

When you build your application, you often need to do some specific tasks repeatedly, such as writing data access code or building a custom collection. Codesmith is especially useful at these times, because you can write templates to automate these tasks, not only to increase your productivity, but to automate the most tedious tasks. Codesmith comes with a number of templates, including templates for all. NET collection types and templates for building stored procedures, but the real power of the tool is the ability to create custom templates. To get you started, I'll give you a quick overview of how to build a custom template.

To build a custom template
Codesmith templates are just a few text files that you can create in any text editor. Their only requirement is to save them with the. cst file name extension. The sample template that I am going to generate will accept a string and then generate a class based on that string. The first step in creating a template is to add a template header, which declares the language of the template, the target language, and the brief template description:

<%@ codetemplate language= "C #"
Targetlanguage= "C #"
description= "Car Template"%>

The next section of the template is a property declaration, where you can declare a property that will be specified each time the template is run. For this template, the only property I want to use is just a string, so the property declaration looks like this:

<%@ Property Name= "ClassName" type= "strings" category= "context"
description= "Class Name"%>

The property declaration causes the ClassName property to appear in the Codesmith property window so that it can be specified when the template is run. The next step is to actually generate the template body, which is very similar to encoding with ASP.net. The following is the main body of the template:

Custom Template

public sealed class <%= ClassName%>
{
private static volatile <%= ClassName%> _instance;
Private <%= ClassName%> () {}
private static readonly Object _syncroot = new Object ();

public static <%= ClassName%> Value
{
Get
{
if (_instance = null)
{
Lock (_syncroot)
{
if (_instance = null)
{
_instance = new <%= ClassName%> ();
}
}
}
return _instance;
}
}
}

Singletonclass

public sealed class Singletonclass
{
private static volatile singletonclass _instance;
Private Singletonclass () {}
private static readonly Object _syncroot = new Object ();

public static Singletonclass Value
{
Get
{
if (_instance = null)
{
Lock (_syncroot)
{
if (_instance = null)
{
_instance = new Singletonclass ();
}
}
}
return _instance;
}
}
}



As you can see, the template accepts string input and uses the class name to generate a separate class. In the template body, use the same start and end tags as in asp.net. In the template, I just insert property values, but you can also use any type of. NET code inside those tags. After the template is complete, you can load it into codesmith by double-clicking it or opening it from the Codesmith application. Figure 4 shows the template that has been loaded into the codesmith.



Figure 4

You can see that the property on the left is exactly the property I declared in the template. If I type "Singletonclass" as the class name and click the Generate button, the class that is displayed at the bottom of the template code is generated.

Codesmith is fairly easy to use and can produce some incredible results if applied correctly. One of the most common parts of a code-generation application is the data access layer. Codesmith includes a special assembly called Schemaexplorer that can be used from a table, stored procedure, or almost any other SQL Server. object to generate the template.

Codesmith is written by Eric J. Smith and can be downloaded at http://www.ericjsmith.net/codesmith.

Nunit
NUnit is an open source unit test framework that is generated for the. NET Framework. NUnit enables you to test your application's specific functionality by writing tests in your favorite language. When you first write code, unit testing is a good way to test the functionality of your code, and it provides a way to test your application for regression. The NUnit application provides a framework for writing unit tests, as well as a graphical interface for running these tests and viewing the results.

Write NUnit Test
As an example, I will test the functionality of the Hashtable class in the. NET framework to determine whether two objects can be added and then retrieved. My first step is to add a reference to the Nunit.framework assembly that gives me access to the properties and methods of the NUnit framework. Next, I'll create a class and mark it with the Testfixture property. This property enables NUnit to know that the class contains NUnit tests:

Using System;
Using System.Collections;
Using Nunit.framework;

Namespace Nunitexample
{
[Testfixture]
public class Hashtabletest {
Public Hashtabletest () {

}
}
}

Next, I will create a method and mark it with the [test] property so that NUnit knows that the method is a test. Then I'll set up a Hashtable and add two values to it, and then use the Assert.AreEqual method to see if I can retrieve the same value that I added to Hashtable, as shown in the following code:

[Test]
public void Hashtableaddtest ()
{
Hashtable ht = new Hashtable ();

Ht. ADD ("Key1", "Value1");
Ht. ADD ("Key2", "Value2");

Assert.AreEqual ("Value1", ht["Key1"), "wrong object returned!");
Assert.AreEqual ("Value2", ht["Key2"), "wrong object returned!");
}

This confirms that I can first add a value to the Hashtable and then retrieve the corresponding value-a simple test, but capable of performing NUnit functions. There are many test types and various Assert methods that you can use to test each part of your code.

To run this test, I need to build the project, open the built assembly in the NUnit application, and click the Run button. Figure 5 shows the results. When I saw that big green stripe, I had a feeling of excitement and dizziness because it let me know that the test had passed. This simple example shows how convenient and powerful NUnit and unit tests are. Because you can write unit tests that can be saved, and you can rerun the unit test every time you change your code, you can not only detect defects in your code more easily, but you will eventually be able to deliver better applications.



Figure 5 NUnit

NUnit is an open source project and can be downloaded from http://www.nunit.org. There is also an excellent NUnit Visual studio. NET add-in that allows you to run unit tests directly from Visual Studio. You can find it in Http://sourceforge.net/projects/nunitaddin. For more information about NUnit and its status in test-driven development, see the article "Test-driven C #: Improve the design and flexibility of Your Project with Extreme Programm ing techniques "(msdn®magazine April 2004 issue).

FxCop
The. NET framework is very powerful, which means there is a great chance of creating a good application, but there is also the possibility of creating bad programs. FxCop is one of the tools that help you create better applications by enabling you to parse assemblies and using a number of different rules to check whether it conforms to these rules. FxCop is accompanied by a fixed number of rules created by Microsoft, but you can also create and include your own rules. For example, if you decide that all classes should have a default constructor with no arguments, you can write a rule to ensure that each class on the Assembly has a constructor. That way, you'll get a certain amount of consistency, regardless of who writes the code. If you need more information about creating custom rules, see John Robbins's Bugslayer column on this topic (Msdn®magazine June 2004).

So, let's look at the actual running FxCop and see what bugs I've found in the Nunitexample assembly I've been working on. When you open FxCop, you first need to create a FXCOP project and then add the assembly that you want to test. After you add the assembly to the project, you can press ANALYZE,FXCOP to parse the assembly. The errors and warnings found in this assembly are shown in Figure 6.



Figure 6

FxCop found a couple of problems in my program set. You can double-click an error to see more information, including the rule description and where to find more information. (One of the interesting things you can do is run FxCop on the frame assembly and see what's going on.) )

FxCop can help you create better, more consistent code, but it does not compensate for poor application design or very simple and clumsy programming. FxCop is also not a substitute for peer code checking, but because it can catch a large number of errors before the code is checked, you can spend more time solving serious problems without worrying about naming conventions. FxCop is developed by Microsoft and can be downloaded from Http://www.gotdotnet.com/team/fxcop.

Lutz Roeder's. NET Reflector
The next essential tool is called. NET Reflector, a class browser and an anti-compiler that can parse an assembly and show you all its secrets. The. NET Framework introduces the world to a reflection concept that can be used to analyze any. NET based code, whether it is a single class or a complete assembly. Reflection can also be used to retrieve information about the various classes, methods, and properties contained in a particular assembly. With. NET Reflector, you can browse the classes and methods of assemblies, analyze the Microsoft intermediate language (MSIL) generated by these classes and methods, and decompile these classes and methods and view the equivalent classes and methods in C # or Visual basic®.net.

To demonstrate how. NET Reflector works, I will load and analyze the Nunitexample assemblies that have been shown earlier. Figure 7 shows the assembly that was loaded in. NET Reflector.



Figure 7 Nunitexample Assembly

Within. NET Reflector, there are a variety of tools that you can use to further analyze the assembly. To see the MSIL that makes up a method, click the method and select Disassembler from the menu.

In addition to being able to view MSIL, you can view the C # form of the method by selecting Decompiler under the Tools menu. By changing your selection under the Languages menu, you can also view the form after the method is recompiled to Visual Basic. NET or Delphi. The following is the code generated by. NET Reflector:

public void Hashtableaddtest ()
{
Hashtable Hashtable1;
Hashtable1 = new Hashtable ();
Hashtable1. ADD ("Key1", "Value1");
Hashtable1. ADD ("Key2", "Value2");
Assert.AreEqual ("Value1", hashtable1["Key1"),
"Wrong Object returned!");
Assert.AreEqual ("Value2", hashtable1["Key2"),
"Wrong Object returned!");
}

The preceding code looks much like the code I actually wrote for the method. The following is the actual code in the assembly:

public void Hashtableaddtest ()
{
Hashtable ht = new Hashtable ();

Ht. ADD ("Key1", "Value1");
Ht. ADD ("Key2", "Value2");

Assert.AreEqual ("Value1", ht["Key1"),
"Wrong Object returned!");
Assert.AreEqual ("Value2", ht["Key2"),
"Wrong Object returned!");
}

Although there are some small differences in the above code, they are functionally identical.

Although the example is a good way to show the contrast between the actual code and the decompile code, it doesn't seem to me to represent the best use of. NET Reflector-analyzing. NET Framework assemblies and methods. The. NET Framework provides a number of different ways to perform similar operations. For example, if you need to read a set of data from XML, there are several different ways to do it using XmlDocument, XPathNavigator, or XmlReader. By using. NET Reflector, you can see what Microsoft uses when writing the ReadXml method of a dataset, or what they did when they read data from a configuration file.. NET Reflector It's also an excellent way to learn about the best implementation strategies: Create objects such as httphandlers or configuration handlers, because you can see how Microsoft workgroups actually build these objects in the framework.

The. NET Reflector is written by Lutz Roeder and can be downloaded from http://www.aisto.com/roeder/dotnet.

NDoc
Writing code documentation is almost always a daunting task. I'm not talking about early design documents, not even more detailed design documents; I'm talking about the methods and properties on the record class. The NDoc tool can use reflection to parse an assembly and automatically generate documentation for code using XML generated from C # XML annotations. XML annotations apply only to C #, but there is a visual Studio. NET power Toy named Vbcommenter that can do similar work for visual Basic. Net. In addition, the next version of Visual Studio will support XML annotations for more languages.

When you use NDoc, you're still writing technical documentation for your code, but you're doing the writing in the code (in an XML annotation), which is easier to endure. When using NDoc, the first step is to open the XML annotation generation feature for your assembly. Right-click the item and select Properties | Configuration Properties | Build, and then in the XML documentation file option, enter the path to save the XML file. When the project is built, an XML file is created that contains all the XML annotations. The following is a way to write a document in XML in the NUnit example:

<summary>
This is test adds a number of values to the Hashtable collection
And then retrieves those values and checks if they match.
</summary>
[Test]
public void Hashtableaddtest ()
{
Method is here
}

The XML document data about the method is extracted and saved in an XML file, as follows:

<member name= "M:nunitexample.hashtabletest.hashtableaddtest" >
<summary>this Test adds a number of values to the Hashtable collection
And then retrieves those values and checks if they match.</summary>
</member>

NDoc uses reflection to examine your assembly, then read the XML in the document, and match them. NDoc uses this data to create any number of different document formats, including HTML Help Files (CHM). After the XML file is generated, the next step is to load the assembly and XML files into the NDoc so that they can be processed. You can easily complete the work by opening NDoc and clicking the Add button.

After you load the assembly and XML files into NDoc and use the available attribute ranges to customize the output, clicking the Generate button initiates the process of generating the document data. Using the default properties, NDoc can generate some very attractive and useful. html and. chm files to automate the original tedious tasks in a fast and efficient manner.

NDoc is an open source project and can be downloaded from http://ndoc.sourceforge.net.

NAnt
NAnt is a. NET build tool that, unlike the current version of Visual Studio. NET, makes it easy to create a build process for your project. When you have a large number of developers working on a single project, you cannot rely on building from a single user's seat. You also do not want the project to be built regularly manually. You prefer to create an automated build process that runs every night. NANT enables you to build solutions, copy files, run NUnit tests, send e-mail messages, and so on. Unfortunately, NAnt lacks a beautiful graphical interface, but it does have a console application and an XML file that specifies which tasks should be completed during the build process. Note that MSBuild (the new build platform for Visual Studio 2005) prepares each robust build scenario and is driven in a similar fashion by an xml-based project file.

The actual operation of the NANT
In this example, I will create a NAnt version file for the Nunitexample solution that was created earlier. First, I need to create an XML file with a. build extension, place it in the root directory of my project, and then add an XML declaration to the top of the file. The first mark I need to add to this file is the project tag:

<?xml version= "1.0"?>
<project name= "NUnit Example" default= "Build" basedir= "." >
<description>the NUnit Example project</description>
</project>

The project tag is also used to set the project name, default target, and base directory. The Description tag is used to set a brief description of the project.

Next, I'll add a property tag that can be used to store the settings in a single location, which can then be accessed from anywhere in the file. In this example, I will create a property named Debug, which I can then set to TRUE or false to reflect whether I want to compile the project under Debug configuration. (Finally, this particular property does not really affect how the project is built; it's just a variable you set up, and it will be read when you really determine how to build the project.) )

Next, I need to create a target tag. A project can contain more than one target that can be specified at NAnt run time. If target is not specified, the default target (the target that I set in the project Element) is used. In this example, the default target is build. Let's look at the target element, which will contain most of the build information:

<target name= "Build" description= "compiles the source code" >
</target>

Within the target element, I will set the name of target to build and create a description of what that target will do. I will also create a CSC element that specifies the data that should be passed to the CSC C # compiler. Let's take a look at the CSC element:

&LT;CSC target= "library" output= ". \bin\debug\nunitexample.dll"
Debug= "${debug}" >
<references>
<includes name= "C:\program files\nunit v2.1\bin\nunit.framework.dll"/>
</references>
<sources>
<includes name= "HashtableTest.cs"/>
</sources>
</csc>

First, I must set target for the CSC element. In this example, I will create a. dll file, so I set target to the library. Next, I must set output for the CSC element, which is the location where the. dll file will be created. Finally, I need to set the Debug property, which determines whether the project is compiled in Debug. Because I created a property earlier to store the value, I can use the following string to access the property's value: ${debug}. The CSC element also contains child elements. I need to create two elements: The references element tells NANT which assemblies need to be referenced for the project, and the sources element tells NAnt which files to include in the build process. In this example, I referenced the NUnit.Framework.dll assembly and included the HashtableTest.cs file. The complete build file is shown in the following code. (You will typically also create a clean target to delete the generated files, but for brevity I have omitted them.) )

<?xml version= "1.0"?>
<project name= "NUnit Example" default= "Build" basedir= "." >
<description>the NUnit Example project</description>
<property name= "Debug" value= "true"/>
<target name= "Build" description= "compiles the source code" >
&LT;CSC target= "library" output= ". \bin\debug\nunitexample.dll"
Debug= "${debug}" >
<references>
<includes name= "C:\program files\nunit
V2.1\bin\nunit.framework.dll "/>
</references>
<sources>
<includes name= "HashtableTest.cs"/>
</sources>
</csc>
</target>
</project>

To build this file, I need to go to the root of my project (where the makefile is located), and then execute Nant.exe from that location. If the build succeeds, you can find the. dll and. pdb files in the application's Bin directory. Although using NANT is certainly not as simple as clicking Build in Visual Studio, it is still a very powerful tool for developing builds that run on an automated schedule. NAnt also includes useful features such as the ability to run unit tests or to copy additional files that are not supported by the current Visual Studio build process.

NAnt is an open source project and can be downloaded from http://nant.sourceforge.net.

Conversion tools
I've put two separate tools together under the title "Conversion Tool." Both of these tools are very simple, but they can be extremely useful. The first tool is the ASP.net version converter, which can be used to convert ASP. NET (the virtual directory runs under it). The second tool is Visual Studio Converter, which can be used to convert project files from Visual Studio. NET 2002 to Visual Studio. NET 2003.

When IIS processes the request, it looks at the extension of the file being requested, and then delegates the request to the ISAPI extension or processes the request based on the extension mapping for that Web site or virtual directory. This is exactly how ASP.net works; The extension mappings are registered for all ASP.net extensions, and these extension mappings are mapped to aspnet_isapi.dll. This is a perfect way to work unless you have ASP.net 1.1 installed-it will upgrade the extension map to the new version of Aspnet_isapi.dll. This can cause errors when an application built on ASP.net 1.0 tries to run with version 1.1. To work around this problem, you can convert all extension mappings to version 1.0 aspnet_isapi.dll, but it will be tedious to do this manually because there are 18 extension mappings. This is when the ASP.net version converter can play a role. With this small utility, you can transform the version of the. NET framework used by any single asp.net application.



Figure 9 asp.net version Converter

Figure 9 shows the ASP.net version converter that is actually running. It's very simple to use, just select the appropriate application, and then select the. NET framework version that you want the application to use. The tool will then use the Aspnet_regiis.exe command-line tool to convert the application to the framework of the selected version. The tool will become more useful as future versions of ASP.net and the. NET framework are released.

The ASP.net version converter is written by Denis Bauer and can be downloaded from http://www.denisbauer.com/NETTools/ASPNETVersionSwitcher.aspx.

The Visual Studio. NET Project Converter (see Figure 10) is very similar to the ASP.net version converter, except that it is used to convert the version of the Visual Studio project file. Although there is only a small difference between versions 1.0 and 1.1 of the. NET Framework, once you convert the project file from Visual Studio. NET 2002 to Visual Studio. NET 2003, you will not be able to convert it back. While this may not be a problem most of the time (because there is little disruptive change between the. NET Framework version 1.0 and version 1.1), at some point you may need to convert the project back. The converter can convert any solution or project file from Visual Studio 7.1 (Visual Studio. NET 2003) to Visual Studio 7.0 (Visual Studio. NET 2002) and reverse when necessary Change.



Map Visual Studio. NET Project Converter

The Visual Studio. NET Project Converter is written by Dacris Software. The tool can be downloaded from the http://www.codeproject.com/macro/vsconvert.asp.



Summary
This article introduces the above tools in a cursory fashion, but I have tried to provide you with at least enough information to arouse your curiosity. I believe this article has given you some insight into a few free tools, and you can start using these tools immediately to write better projects. At the same time, I urge you to make sure you have all the other appropriate tools available, whether it's the latest version of Visual Studio, a powerful computer, or a free utility. Having the right tools will make all the difference.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.