Asp. NET Hands-on tutorial 13 (end)

Source: Internet
Author: User
Tags command line constructor root directory
Asp.net| tutorials to work with business objects
These days New Year's Day is dizzy, finally sober come to:-) So it's a long time to drag, I'm sorry.
Encapsulating logic in business components is a practical application, such as web-based applications and other applications that are essential
Part. In ASP.net, business objects are blocks built into multi-tier Web applications.
/bin Directory for Applications
The problem with using COM components in traditional ASP applications is that these components must be
Registration (typically using the Regsvr32 tool). Remote management of this type of application is impractical because
The registration tool must be running on the local server. What's even more troubling is that once these components are loaded by the application, the magnetic
Hold the lock on the disk. To replace or remove them, the entire Web server must stop.
Asp. NET attempts to resolve these questions by allowing the components to be placed in a well-known directory and automatically found at run time
Problem. This well-known directory is often called/bin, directly located in the root directory of the application (the virtual
Table of Contents). The advantage is that the ASP.net application does not need to register when it uses components---just need to duplicate the component
To/bin directory or upload to/bin directory via FTP.
In addition to providing a "0 registration" method for deploying compiled components, ASP. NET does not require these components to be at run time in the magnetic
Keep the lock on the disk. The inside story is, ASP. NET copies the assembly files found in the/bin directory and replaces
Into the load these "shadow" copies were used. The original component can be replaced even while the Web server is running.
Changes to the/bin directory are automatically obtained at run time. When the change is detected, ASP. NET allows the currently executing
The request completes, and the command post has a new request to use the new component.
Introducing Business Objects
From the bottom up, the business component is just a class, and you can create or instantiate it from the Web page that introduces it. Under
The example of a plane defines a simple HelloWorld class. The class has a common constructor (when an instance of a class
It is executed at a time when it is created), a string attribute called FirstName, and a use of the Firstna
Me property to display the SayHello method of greeting.
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 this class, the C # compiler (Csc.exe) needs to be executed from the command line. The/t option notifies the compiler to create a
Class Library (DLL), the/OUT option tells the compiler where to put the results of the compilation. In this case, the application's/bin
The directory is in the "Aspplus" virtual directory in this tutorial. and assume that the command line can run in the directory where the instance resides, that is,
... \quickstart\aspplus amples\webforms\busobjs directory run.
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 in the application that needs to use it. The following Helloobj.aspx explains
This function.
C # helloobj.aspx
[Run] | [[Source file]
Note the import instruction above the page specifies the namespace to include. Once you have used this command to include the namespace
, you can use the class defined in the namespace in this page. The following command example shows the import directive
By default, ASP.net loads all assembly files from the/bin directory when the application is started. The Assembly text
The loading of the components is specified by the configuration system. For more information, see the Configuration Overview section. The extra Sinks
You can also use a configuration file to pour into your application. For example:
Note: The assembly files loaded from the/bin directory are limited to the application's operating range. This means that the same
Applications can potentially use different assembly files that contain the same class name or namespace without having to match
Reset
A simple two-tier Web page
The classes used by external components in this example are used to perform data access. This simplifies the code for the page, improves readability, and
And the user interface logic is separated from the system function. The following example shows a simple two-tier Web page that uses the
The data access component to obtain information about the product.
C # twotier.aspx
[Run] | [[Source file]
The constructor in the data access component comes with a parameter that specifies the connection string for the product database. Web page calls
Component's GetCategories method to assemble the Drop-down list, calling the component's Getproductsforcategory method to display
Shows the type of product selected by the user.
A simple three-tier Web page
The three-tier application model extends the situation of the two-tier model, including the quotient between the user interface and the data access logic
Rules of affairs. This model allows user interface developers to work at higher levels of abstraction, rather than through low-level data access
Component interfaces manipulate data directly. The typical use of business components in the middle tier is to implement business rules and ensure database association
And the main key constraint of acceptance. The following example uses an intermediate component that is based on a two-digit vendor ID entered by the client
Calculate discounts.
C # threetire.aspx
[Run] | [[Source file]
section of this chapter
1.asp.net run time to find business objects in the famous/bin directory (local assembler file), which is located in the
In the root directory of the program. The/bin directory provides the following benefits:
A. No registration required.
B. No server reboot required.
C. There is no conflict of namespaces.
2. By using the import directive in an. aspx file, you can have the application's pages use the classes in the assembly.
3. Two-layer mode application simplifies the code in the page, improves the readability, and separates the user from the system function.
Mouth logic.
4. The three-tier model application extends the two-tier model, allowing user interface developers to work at higher levels of abstraction. of the Middle
The typical usage of business components is to enforce business rules, ensure database association and the acceptance of primary key constraints.
To be continued = = The next section is to create a custom control, please look forward to


Related Article

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.