Developer

Simplify .NET coding and maintenance with class libraries

Tony Patton explains how class libraries allows you to better organize code to foster code reuse and ease the maintenance task. In addition, any future code changes are easier to implement when the code is centrally located.

I was recently called onto a project to add features to existing applications. To me, this is one of the most challenging aspects of being a developer because the existing application strips away much of your control.

My project encompasses three applications that are similar in many ways. I quickly noticed that much of the code was redundant since the applications shared many functions. A lot of the duplicate code was in classes, so my first step was to create a class library to reduce maintenance headaches and ease the current task at hand: adding functionality.

Class libraries

You use class libraries when you're developing any type of .NET application. The .NET Framework includes the .NET Framework Class Library, an extensive collection of thousands of types (i.e., classes, interfaces, structures, delegates, and enumerations) that aim to encapsulate the functionality of core system and application services in order to make application programming easier and faster.

There are classes that you can use to manipulate the file system, access databases, serialize objects, and launch and synchronize multiple threads of execution, to name a few. To make working with these classes easy, you can group classes with similar functionality together in namespaces. When developing applications utilizing XML, the System.XML namespace is a necessity; it's also a class library. The .NET Framework compiles class libraries into DLLs, so the System.XML class library exists within the System.XML.dll file.

If you're using Visual Studio .NET, you may include a namespace (the DLL file) in the project's references section. Once you add a reference to an assembly, you can access any of the types in its namespaces by providing a fully qualified reference to the type. Typing the fully qualified name of each .NET type quickly becomes rather tiresome, particularly for types that are nested deep within a hierarchical namespace. You can, however, use the Imports (VB.NET) and using (C#) directives to utilize a particular namespace. This allows the compiler to resolve a particular type reference, thus eliminating the need to provide a fully qualified path.

Develop your own class libraries

In addition to the vast number of class libraries included with the .NET Framework, you can create your own. This allows you to create a collection of classes that you may use in multiple applications, and easily make them available to other developers. Additionally, it provides a central location for class maintenance. It reduces the need to include code in multiple projects with multiple maintenance access points.

To create a class library in Visual Studio .NET, select File | New | Project | Visual C# Projects | Class Library. Select your project name and the appropriate directory using the Browse button and click OK. Visual Studio .NET adds two classes to your project: AssemblyInfo and Class1. The AssemblyInfo class file contains details of the project (assembly information) such as name, copyright, version information, and so on. Class1 is the default name given to a class with subsequent classes incrementing the numeric suffix. You can easily rename this class (and namespace) to suit your needs.

The following code listing shows the default class added to a C# class library project, minus the default comment lines:

using System;
namespace ClassLibrary {
public class Class1 {
public Class1() {
} } }

Here's the VB.NET equivalent:

Public Class Class1
End Class

Or, you may decide to create your code using a simple text editor. Saving a file with the appropriate code extension (cs for C# and vb for VB.NET) makes it appear as a source code file. You can use the command-line compiler for your language to create the resulting DLL file.

Example

Now you're ready to create your own class library. I'll use code from a previous .NET newsletter that demonstrated extending the System.Web.UI.Page class. We'll create this class within its own class library. The class performs these tasks:

  • Extends the System.Web.UI.Page class
  • Disables caching
  • Creates a hidden field called statusFlag with a value of zero
  • Adds a JavaScript function to the head portion of the page
  • Executes JavaScript code upon page startup/load

Listing A contains the class library code.

Notice that the code is created within the BuilderClassLibrary namespace. Once you create and compile this class library, a DLL file, BuilderClassLibrary.dll, is available for use within other applications. You may use the library in other applications by adding a reference to it. You can achieve this in the reference list within Visual Studio .NET, and the reference (/r) switch when using a command-line compiler. I'll demonstrate this momentarily.

The next step is using your class library in another application. The code in Listing B show a basic ASP.NET Web form (the code behind file) that takes advantage of the class library. The Web form's class is derived from the BaseClass in the class library.

Since the code needs to be compiled, I use the C# command-line compiler. It uses the \out switch to tell the system where to place the output, as well as in what file. The reference switch (\r) is used to include the class library, the Web form's source code (WebForm1.aspx.cs) and the application's global file (Global.asax.cs) in the resulting dll. View Listing C. For consistency, view the Web form's aspx file in Listing D.

VB.NET equivalent

Up to this point, I've used C# as the language of choice, but VB.NET (or any other .NET language) could have been used. Listing E features the VB.NET equivalent for the class library.

Here's the command-line option when using the VB.NET compiler:

vbc /target:library /out:bin\BuilderExtendPageClass.dll
/r:BuilderClassLibrary.dll Global.asax.vb WebForm1.aspx.vb

And, here's the Web form's .aspx file when VB.NET is used in the code behind file:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="BuilderExtendsPageClassVBNet.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>WebForm1</title>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server"></form>
</body></html>

The code that utilizes the VB.NET class is the same as the C# listing. That is a great aspect of using class libraries; you may build a library in VB.NET, but you can easily use it in your C# applications (and vice versa).

Simplify development

The use of class libraries allows you to better organize code to foster code reuse and ease the maintenance task. In addition, any future code changes are easier to implement when the code is centrally located.

TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!

About Tony Patton

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks