ASP. NET getting started tutorial (13)

Source: Internet
Author: User
Use business objects to work

Encapsulating logic in business components is an essential part of actual applications, such as web-based applications and other applications. In ASP. NET, business objects are built-in blocks of Multi-Layer Web applications.
Application/bin directory
There is a problem when using COM components in traditional ASP applications, that is, these components must be registered before use (a typical practice is to use the regsvr32 tool ). Remote Management of such applications is unrealistic because the registration tool must run on the local server. Even more troublesome, once these components are loaded by applications, they remain locked on the disk. To replace or remove them, the entire web server must be stopped.

ASP. NET tries to solve these problems by allowing components to be placed in a well-known directory and is automatically located at runtime. This well-known directory is usually called/bin, which is directly located under the root directory of the application (virtual directory defined by IIS. The advantage is that ASP. NET applications do not need to register components when using them-they just need to copy the components to the/bin directory or upload them to the/bin directory through FTP.
In addition to providing the "zero registration" method to deploy compiled components, ASP. NET does not require these components to be locked on the disk during runtime. ASP. NET copies the Assembly files found in the/bin directory and replaces them with the "shadow" copies. The original components can even be replaced when the web server is running, and the changes in the/bin directory are automatically obtained at the runtime. When changes are detected, ASP. NET allows the current execution of requests to complete and directs all new requests to use new components.

Introduce Business Objects
At the bottom layer, a business component is just a class. You can create or instantiate a business component from its web page. The following example defines a simple helloworld class. This class has a public Constructor (executed when a class instance is first created), a string attribute called firstname, and a sayhello method that uses the firstname attribute to display greetings.
Using system;
Using system. text;

Namespace helloworld {
Public class helloobj {
Private string _ name;

Public helloobj (){
_ Name = NULL;

}

Public String firstname {
Get {
Return _ name;
}
Set {
_ Name = value;
}
}

Public String sayhello (){
Stringbuilder sb = new stringbuilder ("hello ");
If (_ name! = NULL)
SB. append (_ name );
Else
SB. append ("world ");

SB. append ("! ");
Return sb. tostring ();
}
}
}
To compile such a class, the csung compiler (csc.exe) needs to be executed from the command line. The/T option notifies the compiler to create a class library (DLL), and The/out option notifies the compiler where to put the compilation result. In this example, the/bin directory of the application is under the "aspplus" virtual directory in this tutorial. It is assumed that the command line can be run in the directory where the instance is located, that is, the Directory of.../Quickstart/aspplus/samples/webforms/busobjs.
CSC/T: Library/out:.../../bin/helloobj. dll helloobj. CS
For Visual Basic, the equivalent compilation command is:
Vbc/T: Library/out:.../../bin/helloobjvb. dll helloobj. VB
For JScript, the equivalent compilation command is:
JSC/out:..././bin/helloobjjs. dll helloobj. js
Now this component can be used by any page of the application that needs it. The following helloobj. aspx describes this function.
C # helloobj. aspx
[Run] | [source file]

Note that the import command on the page specifies the namespace to be included. Once this command contains a namespace, you can use the class defined in the namespace on this page. The following command demonstrates the import command
<% @ Import namespace = "helloworld" %>
By default, ASP. NET loads all Assembly files from the/bin directory when the application starts. The Configuration System specifies the loading of assembly files. For details, see configuration overview. You can also use the configuration file to import additional assembly files into the application. For example:
<Configuration>
<Compilation>
<Assemblies>
<! -- The following assemblies are loaded explicitly from the global cache -->
<Add Assembly = "system. Data"/>
<Add Assembly = "system. Web. Services"/>
<Add Assembly = "system. Drawing"/>
<! -- This tells ASP. NET to load all assemblies from/bin -->
<Add Assembly = "*"/>
</Assemblies>
</Compilation>
</Configuration>
Note: The Assembly files loaded from the/bin directory are restricted to the application running range. This means that equivalent applications can potentially use different assembly files containing the same class name or namespace without configuration.

A simple two-layer Web Page
In this example, external components use classes to perform data access. This simplifies the Page code, improves readability, and separates user interface logic from system functions. The following example demonstrates a simple two-layer web page that uses the data access component to obtain product information.
C # twotier. aspx
[Run] | [source file]

The constructor in the data access component has a parameter used to specify the connection string of the product database. On the web page, call the getcategories method of the component to assemble the drop-down list and call the getproductsforcategory method of the component to display the product type selected by the user.

A simple three-tier Web Page
The three-tier application model extends the two-tier model, and contains business rules between user interfaces and data access logic. This model allows the user interface developer to work on a higher abstraction layer, rather than directly operating data through a lower-layer Data Access Component Interface. The typical use of business components in the middle layer is to implement business rules and ensure acceptance of database associations and primary keyword constraints. The following example uses the intermediate component to calculate the discount based on the two-digit vendor ID entered by the client.
C # threetire. aspx
[Run] | [source file]

This section
1. ASP. NET runtime searches for Business Objects (local assembly files) in the famous/bin directory, which is located in the root directory of the application. The/bin directory provides the following advantages:
A. No registration is required.
B. No server restart is required.
C. There is no namespace conflict.
2. You can use the import command in the. aspx file to make the application page use the classes in the Assembly.
3. two-tier applications simplify the code on the page, improve readability, and separate user interface logic from system functions.
4. The layer-3 mode application extends the layer-2 model, allowing the user interface developer to work on a higher abstraction layer. A typical use of intermediate business components is to implement business rules and ensure acceptance of database associations and primary key word constraints.

To be continued = the next part is to create a custom control.

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.