Importing Business Objects
At its most basic level, a business component is just a class for which you can create an instance from a Web Forms page that imports it. The following
example defines a simple HelloWorld class. The class has one public constructor (which is executed when an instance of the class is first
created), a single String property called FirstName, and a SayHello method that prints a greeting using the value of the
FirstName property.
Imports System
Imports System.Text
Namespace HelloWorld
Public Class HelloObj
Private _name As String
Public Sub New
MyBase.New()
_name = Nothing
End Sub
Public Property FirstName As String
Get
Return(_name)
End get
Set
_name = value
End Set
End Property
Public Function SayHello() As String
Dim sb As New StringBuilder("Hello ")
If (_name <> Nothing) Then
sb.Append(_name)
Else
sb.Append("World")
End If
sb.Append("!")
Return(sb.ToString())
End Function
End Class
End Namespace
VB
To compile this class, the C# compiler (Csc.exe) is run from the command line. The /t option tells the compiler to
build a library (DLL), and the /out option tells the compiler where to place the resulting assembly. In this case, the /bin
directory for the application is directly under the "aspplus" vroot of this tutorial, and it is assumed this command is being run from
the sample directory, that is, ...\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
The component is now available to any Web Forms page in the application that needs to use it. The following HelloObj.aspx example illustrates
this functionality.
Note the Import directive at the top of the page that specifies the namespace to include. Once the namespace is included using this directive,
the class can be used from within the Web Forms page. Because the assembly is pre-loaded by the ASP.NET runtime, only
a simple namespace import is required to make the component available. The following code example the Import directive.
<%@ Import Namespace="HelloWorld" %>
By default, ASP.NET loads all assemblies from the /bin directory when the application is started. The assemblies to load are specifed through
the configuration system. For details, see the Configuration Overview section. Additional
assemblies can be imported into an application using configuration as well. 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: Each assembly loaded from /bin is limited in scope to the application in which it is running. This means that peer applications could
potentially use different assemblies with the same class or namespace names, without conflicting.