Components

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 9.  Assemblies and Deployment


Components

The term component is widely used in discussions about software, although there is no consensus about exactly what the term means. The basic concept of component is a black box piece of software that can be reused. By this loose definition, a DLL would be a component. Usually, somewhat more is meant , such as some kind of "object orientation." Examples of such object-oriented components are COM objects, JavaBeans, and CORBA objects.

It would be useful to compare objects from an object-oriented programming language with COM components. An object encapsulates data and behavior, and it facilitates reuse at the source code level . If you are working entirely in one programming language, such as C++ or Java, you can gain great benefit by using a class library that provides reusable code in the form of a hierarchy of classes. An example of such a class library in C++ is Microsoft Foundation Classes (MFC). But you cannot use MFC classes in a Visual Basic program. By contrast, a COM component is a binary piece of software that can be reused in many different programming languages. For example, you can use COM components and ActiveX controls (a particularly rich type of COM component, typically with a graphical user interface) in Visual Basic programs. The component could be implemented in some other language, such as C++. The Visual Basic programmer does not care.

A limitation of COM components is lack of support for implementation inheritance. You cannot start with a base component and inherit its methods . (You can achieve similar reuse by other techniques, such as containment and aggregation, but such reuse is not as easy or convenient as inheritance.) Another drawback of COM components is the requirement that the component implement "plumbing" code that allows it to be called in a black box fashion from another piece of software. Visual Basic 6 hid the plumbing code, but it was there, and VB6 programs could only use a subset of the capabilities of COM. In C++ you could fully utilize COM, but there was a lot of work to be done in implementing the plumbing code. Specialized libraries like the Active Template Library (ATL) could do a lot of the work for you, but that required you to learn yet another piece of technology, and it applied only to C++.

Components in .NET

The .NET Framework provides an exceptionally attractive environment for creating and using software components. By simply setting an appropriate compiler switch or choosing a specific project type in Visual Studio, you can build a class library , which is the .NET version of a component and is a DLL that packages the code for a set of classes. There is no special plumbing code that must be provided. These class library DLLs can easily be used by other .NET programs, and you can mix .NET languages freely . Also, you can inherit from a class implemented in a class library, and this inheritance mechanism extends across languages, since a class library is a binary component.

Class Libraries at the Command Line

To create a .NET class library from the command line, you must compile using the switch /target:library . You can abbreviate the /target switch as simply /t . The following command compiles the file Customer.vb as a class library, creating the file Customer.dll .

 vbc /t:library Customer.vb 

To use the class library from another program, you must obtain a reference to the class library. Compiling at the command line, you can use the /reference or /r switch. The following command compiles the test program TestCustomer.vb using the class library Customer.dll . It creates the executable TestCustomer.exe .

 vbc /r:Customer.dll TestCustomer.vb 

If you would like to try out building and using a class library at the command line, go to the directory CustomerCL . This directory contains both the files Customer.vb and the test program TestCustomer.vb . There is also a batch file build.bat that builds the class library and the console application that exercises it. The application TestCustomer.exe has the following output:

 graphics/codeexample.gif 1  Rocket        Squirrel      rocky@frosbitefalls.com    2  Bullwinkle    Moose         moose@wossamotta.edu New customer Christopher Robin id = 3    1  Rocket        Squirrel      rocky@frosbitefalls.com    2  Bullwinkle    Moose         moose@wossamotta.edu    3  Christopher   Robin         chris@pooh.com 

Class Libraries Using Visual Studio

Visual Studio makes it very easy to work with .NET class libraries. You can create a class library by using the Class Library project type. You can use Solution Explorer to add references. It is all quite painless. There is one nuance involved when using Visual Studio that you need to be aware of. Visual Studio by default creates a root namespace based on the name of the project. You must include this root namespace in your Imports statements. It is sometimes simpler to make this default namespace blank, which you can do in the Properties for the project.

In this section we illustrate the complete process of creating a class library in one project and creating a client application that uses the class library in another project. This exercise will not involve creating any new code. We provide all the code needed in a monolithic application. Rather, you will work on creating a component version of the application, consisting of a class library and a Windows application.

A Monolithic Application
graphics/codeexample.gif

The folder ConsoleMonolithic contains a solution with both the Customers class and a GUI test program for exercising it. Figure 9-1 shows the main window of the application. The currently registered customers are shown in a list box, and buttons are provided to register a customer, unregister a customer, and change the email address. (For convenience, the Customers class provides two preregistered customers.) A customer is selected by clicking in the list box, and this ID can then be used for unregistering or changing an email address.

Figure 9-1. Main window of program for exercising Customers class.

graphics/09fig01.jpg

Creating a Class Library
graphics/codeexample.gif

Our first task will be to use Visual Studio to create a class library containing the Customers class. If you would like to follow along on the computer, you can do your work in the Demos directory for this chapter. The final project is available in the folder CustomerLib .

graphics/codeexample.gif
  1. Bring up the New Project dialog from the menu File New Project. For Project Types choose Visual Basic Projects, and for Templates choose Class Library. Navigate to the desired Location, and type CustomerLib for the Name. See Figure 9-2. Click OK.

    Figure 9-2. Creating a new Class Library project using Visual Studio.
    graphics/codeexample.gif

    graphics/09fig02.jpg

  2. Delete Class1.vb from the new solution.

  3. Copy the file Customer.vb from the CustomerMonolithic folder to the folder for this new solution, and add this file to the solution.

  4. Bring up the Properties for the CustomerLib project (right-click in Solution Explorer and choose Properties from the context menu). Change the Assembly name to Customer, and the Root namespace to blank. See Figure 9-3. Click OK.

    Figure 9-3. Changing the assembly name and root namespace.

    graphics/09fig03.jpg

  5. You should now be able to build the class library. The new DLL Customer.dll will be in the bin subdirectory.

If you would like to perform an immediate test of your new DLL, you could copy the test program TestCustomer.exe from CustomerCL into the bin folder. You should now be able to run the test program from the command line, and it will be using the new DLL that you just created.

Working with References in Visual Studio
graphics/codeexample.gif

Next we would like to use Visual Studio to create a Windows application that obtains a reference to our Customer.dll . Again, if you would like to follow along on the computer, you can do your work in the Demos directory for this project. The final project is available in the folder CustomerGui .

  1. Bring up the New Project dialog from the menu File New Project. This time, create a Windows application with name CustomerGui .

  2. Delete the file Form1.vb from the new solution.

  3. Copy the files CustomerForm.vb , RegisterDialog.vb , and EmailDialog.vb from the CustomerMonolithic folder. Add these files to the new solution.

  4. Copy the file Customer.dll from the CustomerLib\bin folder into the source file folder of the CustomerGui solution.

  5. In Solution Explorer, right-click over References and choose Add Reference from the context menu, bringing up the Add References dialog. Click the Browse button, bringing up the Select Component dialog. If necessary, you can then navigate to find the component you want to add. In this case, because of our previous copying of the Customer.dll , you should find the DLL immediately. Double-click on it. You should then see the DLL in the Selected Components list box, as illustrated in Figure 9-4. Click OK.

    Figure 9-4. Adding a reference using Visual Studio.

    graphics/09fig04.jpg

  6. You can now try building the application. You will get an error message, stemming from having previously deleted Form1.vb .

     'Sub Main' was not found in 'CustomerGui.Form1' 
  7. You will need to make CustomerForm your Startup object. You can do this by right-clicking on CustomerGui in Solution Explorer to bring up the properties for the project. You can then choose the Startup object form the dropdown list, as illustrated in Figure 9-5. Click OK.

    Figure 9-5. Specifying the Startup object.

    graphics/09fig05.jpg

  8. You should now be able to build and run the Windows application. It should behave identically to the monolithic application we looked at earlier.

References at Compile Time and Run Time

The assembly Customer.dll is used both at compile time and at runtime. At compile time the metadata in the assembly is used, and at runtime the code is called. When you add a reference to a DLL using Solution Explorer, Visual Studio will copy the DLL into the bin folder, so it will reside in the proper place at runtime. In this case we are using "private" deployment in which all assemblies reside in the same directory. With .NET it is also possible to have "shared" deployment, using the Global Assembly Cache. We will discuss these issues in detail in the rest of this chapter.


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net