home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / aspplus / doc / businessobjs.aspx < prev    next >
Encoding:
Text File  |  2000-06-08  |  9.9 KB  |  210 lines

  1.  
  2. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  3.  
  4. <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
  5.  
  6. <h4>Working With Business Objects</h4>
  7. <p>
  8.  
  9. <!-- #include virtual="/quickstart/aspplus/include/wftoc5-1.inc" -->
  10. <p>
  11. <hr>
  12.  
  13. Encapsulating logic in business components is an essential part of any real-world application, web-based or otherwise.  In ASP+, business
  14. objects are the building blocks for multi-tiered web applications, such as those with a layer for data access or common application rules.
  15. In this section we demonstrate how to write some simple components and include them in your application's Web Form pages.
  16. <p>
  17.  
  18. <!--BEGIN SECTION-->
  19. <a name="bindir">
  20. <span class="subhead">The Application /Bin Directory</span>
  21. <p>
  22. A problem with using the classic COM model for web application components is that those components must be registered (typically 
  23. using the regsvr32 utility) before they can be used from a traditional ASP application.  Remote administration of these types of 
  24. applications is often not possible, because the registration utility must be run locally on the server.  To make matters more difficult,
  25. these components remain locked on disk once they are loaded by an application, and the entire web server must be stopped before
  26. these components can be replaced or removed.
  27. <p>
  28. ASP+ attempts to solve these problems by allowing components to be placed in a well-known directory, to be automatically found
  29. at run-time.  This well-known directory is always named <b>/bin</b>, and is located immediately under the root directory for the 
  30. application (a virtual directory defined by IIS).  The benefit is that no registration is required to make components available to the 
  31. ASP+ application -- components may be deployed by simply copying to the /bin dir or performing an FTP file transfer.
  32. <p>
  33. In addition to providing a zero-registration way to deploy compiled components, ASP+ does not require these components to remain
  34. locked on disk at runtime.  Behind the scenes, ASP+ duplicates the assemblies found in /bin and loads these "shadow-copies" instead.  
  35. The original components may be replaced even while the web server is still running, and changes to the /bin directory are automatically 
  36. picked up by the runtime.  When a change is detected, ASP+ will gracefully allow currently executing requests to complete, and direct
  37. all new incoming requests to the application which uses the new component(s).
  38.  
  39.  
  40. <!--BEGIN SECTION-->
  41. <br>
  42. <a name="importing">
  43. <br>
  44. <span class="subhead">Importing Business Objects</span>
  45. <p>
  46. At its most basic level, a business component is just a class which can be instantiated from a Web Form which imports it.  In this 
  47. example, we define a simple HelloWorld class.  Our class has one public constructor (which is executed when the class is first 
  48. instantiated), a single String property called "FirstName", and a "SayHello" method which prints a greeting using the value of the 
  49. FirstName property.
  50. <p>
  51.  
  52. <div class="code"><pre>
  53. namespace HelloWorld 
  54. {
  55. using System;
  56. using System.Text;
  57.  
  58.   public class HelloObj 
  59.   {
  60.     private String _name;
  61.  
  62.     public HelloObj() {
  63.         _name = null;      
  64.     }
  65.  
  66.     public String FirstName 
  67.     {
  68.       get {  return _name; }
  69.       set {  _name = value; }
  70.     }
  71.  
  72.     public String SayHello() 
  73.     {
  74.       StringBuilder sb = new StringBuilder("Hello ");
  75.       if (_name != null)
  76.          sb.Append(_name);
  77.       else
  78.          sb.Append("World");
  79.       sb.Append("!");
  80.       return sb.ToString();
  81.     }
  82.   }
  83. }
  84. </pre></div>
  85.  
  86. <p>
  87. To compile this class, we run the C# compiler (csc.exe) from the command-line.  The <b>/t</b> option tells the compiler we want to 
  88. build a library (dll), and the <b>/out</b> option tells the compiler where to place the resulting assembly.  In this case, the /bin 
  89. directory for our application is directly under the "aspplus" vroot of this tutorial.
  90. <p>
  91.  
  92. <div class="code"><pre>
  93. csc /t:library /out:..\..\..\bin\HelloObj.dll HelloObj.cs
  94. </pre></div>
  95.  
  96. <p>
  97. The component is now available to any Web Form in the application that wishes to use it.  The following HelloObj.aspx example illustrates 
  98. this:
  99. <p>
  100.  
  101. <Acme:SourceRef 
  102.   RunSample="/quickstart/aspplus/samples/webforms/busobjs/HelloObj.aspx" 
  103.   ViewSource="/quickstart/aspplus/samples/webforms/busobjs/HelloObj.src"
  104.   Icon="/quickstart/aspplus/images/helloobj.gif"
  105.   Caption="HelloObj.aspx"
  106.   runat="server" />
  107.  
  108.  
  109. <p>
  110. Note the following Import directive at the top of the page that specifies the namespace to include.  Once the namespace is included using this directive,
  111.  the class may be used from within the Web Form.  Because the assembly is pre-loaded by the ASP+ runtime, only
  112. a simple namespace import is required to make the component available.
  113. <p>
  114.  
  115. <div class="code"><pre>
  116. <%@ Import Namespace="HelloWorld" %>
  117. </pre></div>
  118.  
  119. <p>
  120. By default, ASP+ loads all assemblies from the /bin directory when the application is started.  The assemblies to load are specifed via 
  121. the configuration system (see the <a href="configoverview.aspx">Configuration Overview</a> section for more detail).  Additional
  122. assemblies may be imported into an application using configuration as well.  For example:
  123. <p>
  124.  
  125. <div class="code"><pre>
  126. <configuration>
  127.     <compilation>
  128.         <assemblies>
  129.             <!--The following assemblies are loaded explicitly from the global cache-->
  130.             <add assembly="System.Data"/>
  131.             <add assembly="System.Web.Services"/>
  132.             <add assembly="System.Drawing"/>
  133.             <!--This tells ASP+ to load all assemblies from /bin-->
  134.             <add assembly="*"/> 
  135.         </assemblies>
  136.     </compilation>
  137. </configuration>
  138. </pre></div>
  139.  
  140. <b>Note: </b> Each assembly loaded from /bin is limited in scope to the application in which it is running. This means that peer applications could 
  141. potentially use different assemblies with the same class or namespace names, without conflicting. 
  142.  
  143. <!--BEGIN SECTION-->
  144. <br>
  145. <a name="twotier">
  146. <br>
  147. <span class="subhead">A Simple Two-Tier Web Form</span>
  148. <p>
  149.  
  150. The classic use for an external component is to perform data access.  This simplifies the code in your page, improving readability and 
  151. separating your user interface (UI) logic from the backend functionality.  The following example demonstrates a simple 
  152. two-tiered Web Form using a data access component to retrieve product information.
  153. <p>
  154.  
  155. <Acme:SourceRef 
  156.   RunSample="/quickstart/aspplus/samples/webforms/busobjs/TwoTier.aspx" 
  157.   ViewSource="/quickstart/aspplus/samples/webforms/busobjs/TwoTier.src"
  158.   Icon="/quickstart/aspplus/images/twotier.gif"
  159.   Caption="TwoTier.aspx"
  160.   runat="server" />
  161.  
  162. <p>
  163. The data access component takes a single parameter to its constructor specifying the connection string to the product database.
  164. The Web Form calls the component's GetCategories method to populate a dropdown list, and calls the component's GetProductsForCategory
  165. method to display the products for the category selected by the user.
  166.  
  167. <!--BEGIN SECTION-->
  168. <br>
  169. <a name="threetier">
  170. <br>
  171. <span class="subhead">A Simple Three-Tier Web Form</span>
  172. <p>
  173. A three-tiered application model extends the two-tiered scenario to include business rules between the UI and data access logic.  This 
  174. model allows UI developers to work with a higher level of abstraction rather than directly manipulating data through low-level data 
  175. access component APIs.  The middle business component typically enforces business rules and ensures that the relationships and 
  176. primary key restraints of the database are honored.  In our simple example, the middle component calculates a discount based on 
  177. a two-digit Vendor ID entered by the client.
  178.  
  179. <p>
  180.  
  181. <Acme:SourceRef 
  182.   RunSample="/quickstart/aspplus/samples/webforms/busobjs/ThreeTier.aspx" 
  183.   ViewSource="/quickstart/aspplus/samples/webforms/busobjs/ThreeTier.src"
  184.   Icon="/quickstart/aspplus/images/threetier.gif"
  185.   Caption="ThreeTier.aspx"
  186.   runat="server" />
  187.  
  188.  
  189. <!--BEGIN SECTION-->
  190. <a name="endofsection">
  191.  
  192. <h4>Section Summary</h4>
  193. <ol>
  194. <li>The ASP+ runtime finds business objects (local assemblies) in a well-known /bin directory, directly under the application root. The /bin directory offers the following advantages: <p>
  195. <ul>
  196. <li><b>No Registration Required.</b> No registration is required to make an assembly available to pages in the application. It is available by virtue of its location in the /bin directory. Compiled code can be deployed by simply copying or FTP'ing to this location. 
  197. <li><b>No Server Restart Required.</b> When any part of an ASP+ application is changed (for example, replacing a .dll in /bin), new requests immediately begin execution against the changed file(s). Currently executing requests are allowed to complete before the old application is gracefully torn down. The web server does not require a restart when you change your application, even when replacing compiled code. 
  198. <li><b>No Namespace Conflicts.</b> 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. 
  199. </ul>
  200. <p>
  201. <li>Classes in an assembly are made available to a page in the application using an Import directive within the aspx file. 
  202. <li>Two-tiered applications simplify the code in a page, improving readability and separating user interface (UI) logic 
  203. from backend functionality.
  204. <li>Three-tiered applications extend the two-tiered model to enable UI developers to work with a higher level of abstraction. The middle 
  205. business component typically enforces business rules and ensures that the relationships and primary key restraints of the database are 
  206. honored.
  207. </ol>
  208. <p>
  209.  
  210. <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->