Using business objects to work
These days, New Year's Day has been dizzy, so it's hard to wake up :-) So I have been dragging a little longer, I am sorry.
Packaging logic in business components is actual applications, such as web-based applications and other applications, essential
section. In ASP.NET, business objects are blocks built in 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 used before use.
Registration (typical approach is to use the Regsvr32 tool). Remote management This type of application is very unreal, because
The registration tool must be run in the local server. What's more trouble is that once these components are loaded by the application, they are magnetic
Keep the lock on the disk. To replace or remove them, the entire web server must stop.
ASP.NET is trying to resolve these questions by allowing the components to be placed in well-known directorys.
question. This well-known directory is often called / bin, directly in the root directory of the application (defined by IIS)
Under contents. This kind of benefit is that the ASP.NET application does not need to register when using components --- only need to copy components
Make the / bin directory or upload to the / bin directory via FTP.
In addition to providing "zero registration" ways to deploy the compiled components, ASP.NET does not require these components in runtime.
Keep the lock on the disc. Its insider is, ASP.NET copies these assembly files found in / bin directory and replaced
It is used to use these "shadow" copies. The original components can even be replaced during the WEB server running,
The change of the / bin directory is automatically obtained. When the change is detected, the ASP.NET allows the current execution.
Request to complete and direct all new requests to use new components.
Introduce business objects
From the primer, the business component is just a class, you can create or instantiate it from the introduction of its web page. under
An example of the face defines a simple HelloWorld class. This class has a common constructor (as an example of a class
When it was created, it was executed), a String property called FirstName, and a use firstna
The property of the Me is displayed for the SayHello method for the greetings.
Using system;
Using system.text;
Namespace helloworld {
Public class helloobj {
Private string _name;
Public helloobj () {
_name = null;
}
Public string dimstname {
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 ();
}
}
}
In order to compile this class, the C # compiler (CSC.exe) needs to be executed from the command line. / t option Notification The compiler creates one
Class library (DLL), / OUT option notifies the compiler What location is placed in the compiler. In this example, the application's / bin
The catalog is in the "ASPLUS" virtual directory of this tutorial. And assume that the command line can run in the directory where the instance is located, ie
... / QuickStart / aspplus / Samples / WebForms / BusObjs Directory.
CSC / T: library /out:../../..../bin/helloobj.dll Helloobj.cs
For Visual Basic, the equivalent compilation command is:
VBR / T: library /out:../../..../bin/helloobjvb.dll Helloobj.vb
For JScript, the equivalent compilation command is:
JSC /Oout:../../../../bin/helloobjjs.dll Helloobj.js
This component can now be used in any page that needs to be used in the application. The following helloobj.aspx description
This feature.
C # helloobj.aspx
[Run] | [[Source File]
Note The import command above the page specifies the namespace to include. Once this command contains namespace
You can use the classes defined in this page in this page. The following command illustrates the Import instruction
By default, when the application starts, the ASP.NET is loaded into all assembly files from the / bin directory. Assemble
The load is specified by the configuration system. See the Configuration Overview section for details. Extrahui
Editing files can also be poured into the application using the configuration file. E.g:
Note: Assemble files loaded from the / bin directory are limited to the scope of the application run. This means that equivalent
The application can potentially use different compilation files containing the same name or namespace, without having to match
.
A simple two-layer web page
The class used by the external components in this example is used to perform data access. This simplifies the code of the page, improves readability, and
And the user interface logic is separated from the system function. The following example demonstrates a simple two-layer web page that uses
Data access components to obtain information about the product.
C # twotier.aspx
[Run] | [[Source File]
The constructor in the data access component has a parameter to specify the connection string of the product database. Web page call
Component's getCategories method to assemble the drop-down list, call the getProductSforcategory method of the component to display
Sign in the type of product selected by the user.
A simple three-layer web page
The three-layer application model extensions the case of two-layer model, including business between user interface and data access logic
Rules. This model allows the user interface developer to work in a higher abstraction layer instead of passing the low-level data access.
Component interface direct data. The typical usage of the business components of the intermediate layer is to implement business rules to ensure database associations.
And the Lord keyword constraint acceptance. The following example uses an intermediate component to enter the two-digit vendor ID input according to the client.
Calculate the discount.
C # threetire.aspx
[Run] | [[Source File]
This chapter section
1.asp.net runtime Find a business object in the famous / bin directory (local assembly file), this directory is located
Use the root directory of the program. The / bin directory provides the following advantages:
a. No need to register.
b. No server restart is required.
c. There is no conflict with a namespace.
2. By using the Import instruction in the .aspx file, you can use the page of the application to use the class in the assembly.
3. The application of the two-layer mode simplifies the code in the page, improves readability, separating the user from the system function.
Port logic.
4. Three-layer mode application extends two-layer model, allowing user interface developers working at higher abstractions. Middle
The typical usage of business components is to implement business rules to ensure that database associations and primary keyword constraints.
Waiting == The next part is to create a custom control, so stay tuned