.NET
  Home arrow .NET arrow Methods for an Application`s Business Logi...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Methods for an Application`s Business Logic Layer
By: Addison-Wesley/Prentice Hall PTR
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2009-12-31

    Table of Contents:
  • Methods for an Application`s Business Logic Layer
  • Passing Parameters
  • Documenting the Method
  • Overloading Methods
  • Defining Shared Methods

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Methods for an Application`s Business Logic Layer


    (Page 1 of 5 )

    In this fifth part of a six-part series on building the business logic layer of a .NET application, you'll learn all about methods: defining them, creating them, overloading them, and more. It is excerpted from chapter five of the book Doing Objects in Visual Basic 2005, written by Deborah Kurata (Addison-Wesley, 2008; ISBN: 0321320492).

     Defining Methods

    The methods of a class define the behavior and functionality associated with the class. Methods are implemented as subroutines and functions. For example, a Product class has Create and Save methods.

    This section details the process of creating a method. It then covers some additional techniques for defining methods.

    Creating a Method

    Methods define the logic in your application. Create a method in a class for each set of business logic identified for the class during the design phase.

    Implement a method using a subroutine when the method does not need to return a value, or a function if the method does need to return a value.

    To define a method:

    1. Open the class in the Code Editor.
    2. Create the subroutine or function for the method.
      For example:

      Public Function Create

      Use good naming conventions for your method name. The recommended convention is to use the method’s human-readable name, concatenating the words and using Pascal case, whereby each word in the name is capitalized.

      The purpose of this particular method is to create an instance of the business object class using the Factory pattern, so it was named Create. Some developers don’t like to use that name because it could imply that a new item, such as a new product, is being created when instead an instance is created for an existing item. Alternatively, you could name this method CreateInstance or GetProduct or simply Retrieve
    3. Add the parameters appropriate for the method.

      For example:

      Public Function Create(ByVal prodID As Integer)

      Parameters define the data that is passed into or out of the function or subroutine. The number, name, and type of the parameters depend on the data that needs to be passed. In this example, the product ID is passed in to create an object populated with data for the defined ID.

      Use good naming conventions for your parameter names. The recommended standard is to use a logical parameter name, concatenating the words and using camel case, whereby the first letter is lowercase and the beginning of every other word is capitalized. Be sure that the parameter names do not conflict with any of your property names. 
    4. If you are defining a function, define the method’s return type.

      For example:

      Public Function Create(ByVal prodID As Integer) _
                                As Product


      The return type depends on the data that needs to be passed back from the function. In this example, the return type is an instance of the Product class. 
    5. Press the Enter key to automatically generate the method’s remaining structure:

      Public Function Create(ByVal prodID As Integer) _
                                As Product

      End Function
    6. Add code within the method to perform the desired operation.
    7. If you’re implementing a Function, use the Return statement to return the value.  

    NOTE FOR VB6 DEVELOPERS: Use the Return statement instead of using the method’s name to return a value.


    The purpose of this particular Create method is to create an instance of the class. As discussed earlier in this chapter, objects are often created from a class using the Factory pattern. The Create method is then used, instead of the constructor, to create instances of the class.

    A Create method used to create an instance of a class would look similar to this:

    Public Function Create(ByVal prodID As Integer) As Product
        Dim prod As Product 
       
    ' Create a new instance
        prod = New Product()

        ' Populate the object
        If prodID = 1 Then 
            
    prod.ProductID = 1 
            prod.ProductName = "Mithril Coat"
            prod.InventoryDate = #4/1/2006#
        End If

        Return prod
    End Function

    The first line of this function declares an object variable. The New keyword is then used to create a new instance of the Product class. The object properties are then populated. Notice that these are hard-coded in this case. The property values will be assigned from data in a database in Chapter 8. For now, the values are hard-coded so that the Create method works at this point without needing the data access layer in place just yet. The last line of the function returns the instantiated and populated Product object.

    Although these steps demonstrate a Create method, you can create any type of method using these steps. Alternatively, you can use the Class Designer, as described in the next chapter, to assist you in defining the methods of your class.

    Use methods to perform all of the processing required by your application. To create good methods, ensure that each method has a single purpose and that the method is no longer than about one page. If a method is long, break it into multiple methods. This makes each method much easier to build and maintain.  


     Building Along

    For the Purchase Tracker sample application:

    • Open the Product class in the Code Editor.
    • Add the Create method, as defined in this section.
    • Open the ProductWin form in the Code Editor.
    • In the Load event for the ProductWin form, modify the code to call the Create method and display the resulting values:

      Dim prod as Product
      prod = New Product
      prod = prod.Create(1)
      Debug.WriteLine(prod.ProductID)   ' Displays 1
      Debug.WriteLine(prod.ProductName)
                            ' Displays Mithril Coat
      Debug.WriteLine(prod.InventoryDate)
                            ' Displays 4/1/2006 12:00:00 AM

    Run the application. It displays your splash screen and then shows the MDI parent form. Select Products | Manage Products to display the ProductWin form. The debug messages appear in the Immediate window (Debug | Windows | Immediate) or in the Output window (Debug | Windows | Output), depending on your settings.


    More .NET Articles
    More By Addison-Wesley/Prentice Hall PTR


     

    .NET ARTICLES

    - Introducing .NET
    - Building Business Objects for an Application
    - Methods for an Application`s Business Logic ...
    - Properties of an Application`s Business Logi...
    - Classes and Properties in an Application`s B...
    - Organizing Code for the Business Logic Layer...
    - Building the Business Logic Layer
    - Completing a Business Layer with Windows Wor...
    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek