Create Custom Components

Aliases enable you to reference classes without full qualification of the class. Aliases are set by placing the using keyword before a namespace. Using aliases can help reduce the length of your code, making it easier to read.

Example:

Using Transformer = system.xml. xsl.xsltransform;

Batch files are very useful to take care of repetitive tasks like compiling a component. For example, the C# components for this chapter all come with batch files on the CD-ROM that have the csc commands for compiling the component. Look in the Code directory for this chapter and find a batch file with the same name as the component you create in the task (for example, SimpleComponent.bat is used to create SimpleComponent). Open this file with a text editor and find the following source: csc /t:library /out:bin\SimpleComponent.dll SimpleComponent.cs. You can use these batch files by simply typing in the name of the component (for example, SimpleComponent) while at the command prompt. Note: You must navigate to the directory where the batch file and the component source code are located.

J Windows Update

1 Jc—j Type the name oF a program; folder, document, or 1 rS—1 Internet resource, and Windows will open it for you.

-II

1 1

Cancel 1 Browse,., 1

—O Click Start O Run to open the dialog box.

| Type cmd in the Open field.

Click OK to open the command prompt.

13 C:\WINNT\System32\cmd.exe

^injxjl

Microsoft Windows 2000 [Uer<

ion 5.00.2195]

Hi

CO Copyright 1905-2000 Microsoft Corp. C:\>cd C:\InetPub\wwwroot C:\Inetpub\wwwroot>nkdir bin

C:\InetpubWvwroot>csc /tllibrary /out:bin\SinpleComponent.dll SinpleCompo

My Computer

CO Copyright 1905-2000 Microsoft Corp. C:\>cd C:\InetPub\wwwroot C:\Inetpub\wwwroot>nkdir bin

C:\InetpubWvwroot>csc /tllibrary /out:bin\SinpleComponent.dll SinpleCompo

Microsoft (R) Uisual Copyright (C) Micros.

C:\Inetpub\wwwroot>

aI C4t Compiler Uei

Change directories to where you saved the source file by using the cd command.

Create a /bin directory for your compiled libraries.

Use the csc command to compile the class at the command prompt.

Note: See page 34 for instructions on compiling.

CONTINUED

Components enable you to create distributed, reusable architectures. If you put your business logic and data access into components, you put yourself in a better situation for addressing application development challenges. The challenges can be issues with security, scalability, performance, stability, or reusability.

In terms of reusability, when you create components in .NET with managed code, you need to decide if the component is part of a private assembly or a global assembly. In many cases, you put components into private assemblies. This is the simplest way to create, manage, and use components. No special registration process is needed for a private assembly, except for putting the compiled DLL in the /bin directory of your Web site. Using private assemblies for your components makes it very simple to deploy ASP.NET applications. All you need to do is xcopy the files to a Web server.

Global assemblies entail more detail. A global assembly needs to be put in the Global Assembly Cache (GAC). This is required to register a global assembly. If the component is registered in the global assembly, then you do not have the ability to just xcopy the files for moving a Web site. You will also need to incorporate a registration process for your global assemblies.

^CREArEASIMPLECOMPONENT^CONTINUED)

^CREArEASIMPLECOMPONENT^CONTINUED)

% Untitled - Notepad

File Edit Format Help

cFONTFACE ^"Verdana":

-H3><ASP:LABEL ID="labelMessage" RUNAT="Server" />-;/H3 </FORM=-

LE Open the GenericTemplate.aspx template from the Code Templates directory.

E Add the Page_Load function.

% Untitled - Notepad

File Edit Format Help

LE Open the GenericTemplate.aspx template from the Code Templates directory.

£ Import the SimpleComponent namespace.

E Add the Page_Load function.

Create a new variable of type SimpleComponent.

cFONTFACE ="Verdana">

Import Namespace-'SimpleComponent" %> <HTML=> cHEAD=-

•¡SCRIPT LANGUAGE-'C#" RUNAT="Setw"=-public void Page_Load(ubject sender, EventArgs E){

SimpleComponent simplecomponentMessaqe - new SimpleComponentO

Istring stringMessage = sirnplecomponentMessage.SayWelcomeStatementnil labelMessiage.Text - stringMessage; |

cFONTFACE ^"Verdana":

-H3><ASP:LABEL ID="labelMessage" RUNAT="Server" />-;/H3 </FORM=-

-E Create a new string variable and read the result of SayWelcomeStatement into that string.

le Set the label on the page equal to what was returned from SayWelcomeStatement.

J Add a server form.

Add a label to the server form.

J Add a server form.

Add a label to the server form.

CREATE CUSTOM COMPONENTS

You can package up multiple components into deployable units called assemblies. Assemblies are how the .NET Framework manages components for: deployment, version control, reuse, activation scoping, and security permissions. When creating an assembly, you need to decide on whether you want the assembly to be private or global. There are pros and cons to either choice. The main benefit for choosing private assemblies is the ease of use, especially with deployment. Private assemblies support the xcopy deployment, which is not available for COM components.

To support some of the advanced features of .NET components, like sharing a component across multiple applications, you will need to create a global assembly. Each computer where the common language runtime is installed has a machine-wide code cache called the global assembly cache. You should share assemblies by installing them into the global assembly cache only when you need to. Typically, for ASP.NET applications you will create private assemblies and put them into the /bin directory of your Web site.

° Save the file and request it from the Web server.

■ A welcome message appears.

° Save the file and request it from the Web server.

■ A welcome message appears.

Was this article helpful?

0 0

Post a comment