ASP.NET
  Home arrow ASP.NET arrow Page 3 - Class Frameworks in VB .NET
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
ASP.NET

Class Frameworks in VB .NET
By: Eugene Gilerson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 27
    2003-02-17

    Table of Contents:
  • Class Frameworks in VB .NET
  • Defining Data Access
  • Creating Business Objects
  • Presentation Layer
  • Conclusion

  • 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


    Class Frameworks in VB .NET - Creating Business Objects


    (Page 3 of 5 )

    On the second page of the application, users are presented with editable Name, Price and Number of Units fields for the particular product and read-only Supplier information field. User can then modify and save the data to database.

    An obvious approach now would be to use MyDatabase class and execute “select” and “update” statements as required. Yet again – not a great idea from code reusability perspective. Imagine many similar pages across the site, executing validations and database queries that are very much alike. It would be hard to code and maintain. Also, if you were to modify properties of products on the Web and through a client-server interface would require multiple instances of the same code in both applications.

    In fact, this trail of thought goes right along with Object-oriented programming paradigm, where “real-life” business objects are mapped into classes with methods and properties. Since our “edit” page displays both product and supplier information, both Product and Supplier business entities are prime candidates for being converted into a class.

    Any Product has a Supplier. Because of application requirements, objects of Product class can be modified, while Supplier objects are read-only. Both objects will need to interact with each other and access the database. Therefore, we will put them in same namespace as MyDatabase class.

    Let’s define the Supplier class first. Supplier has an ID, Name and Country properties, all read-only, and a one-argument constructor which accepts Supplier ID and initializes member variables based on database query:

    Public ReadOnly Property ID() As Int32
      Get
        Return intID
      End Get
    End Property

    Sub New (p_intID As Int32)
      '-- Call DB and initialize object variables
      Dim dtrSupplier As SqlDataReader
      dtrSupplier = MyDatabase.GetReader("SELECT * FROM Suppliers WHERE SupplierID = " & p_intID)

      If dtrSupplier.Read() Then
        intID = p_intID
        strName = dtrSupplier("CompanyName")
        strCountry = dtrSupplier("Country")
      End If

      dtrSupplier.Close()
    End Sub


    The Name and Country properties are very similar to ID property. Once a Supplier object is created, the constructor reads database table and sets up the properties of the new Supplier object based on supplier ID.

    Now let’s build the Product class. It also has a read-only ID property, and a one-argument constructor that contains logic very similar to the one in Supplier class. The Name, Price, and NumUnits are all read/write properties, such as:

    Public Property NumUnits() As Int32
      Get
        Return intNumUnits
      End Get

      Set (ByVal p_intValue As Int32)
        intNumUnits = p_intValue
        blnHasChanged = True
      End Set
    End Property


    Set methods are very important – this is where you often would implement data validation logic. For example, if someone is calling the Set NumUnits with a value of –10, the code should not update NumUnits property of the product object.

    Also, notice blnHasChanged variable. It is set to False in Product class constructor, and is later reset to True when any of the properties is modified. You will see why this is important in a moment.

    Product class will also “contain” a supplier, and we’ll expose a Supplier object as a read-only property of Product class:

    Public ReadOnly Property Supplier() As Supplier
      Get
        Return objSupplier
      End Get
    End Property


    And here is what one-argument constructor of the Product class looks like:

    Sub New (p_intID As Int32)
      '-- Call DB and initialize object variables
      Dim dtrProduct As SqlDataReader
      dtrProduct = MyDatabase.GetReader("SELECT * FROM Products WHERE ProductID = " & p_intID)

      If dtrProduct.Read() Then
        intID = p_intID
        strName = dtrProduct("ProductName")
        intNumUnits = CType(dtrProduct("UnitsInStock"), Int32)
        decPrice = CType(dtrProduct("UnitPrice"), Decimal)
        objSupplier = New Supplier(dtrProduct("SupplierID"))
      End If

      blnHasChanged = False
      dtrProduct.Close()
    End Sub


    The most interesting statement in the code above is the following:

    objSupplier = New Supplier(dtrProduct("SupplierID"))

    Putting object structure to work, not only Product class exposes a read-only Supplier property, but it creates a new Supplier object, which in turn automatically sets its own properties by executing the one-argument constructor of Supplier class.

    Since all other product information can be updated, the class also requires a Save() method. This method will accept no parameters, and it’s only function will be to save current properties of the product object back to the database table. We will now use blnHasChanged variable to ensure that we only make a call to database when at least one of the properties of the Product object has been updated:

    Public Sub Save()
      Dim strUpdate As String

      If blnHasChanged = True Then
        '-- Data has been updated, save it to database
        strUpdate = "UPDATE Products SET "
        strUpdate += "ProductName = '" & Me.Name & "', "
        strUpdate += "UnitsInStock = " & Me.NumUnits & ", "
        strUpdate += "UnitPrice = " & Me.Price & " "
        strUpdate += "WHERE ProductID = " & Me.ID

        MyDatabase.ExecNonQuery(strUpdate)
      End If
    End Sub


    The Product class really takes advantage of all our prior efforts – it uses both Supplier and MyDatabase classes to access database and encapsulate the data. We’ve now created a class framework we can use in ASP.NET pages, stand-alone client/server or even console .NET applications.

    All the code for our namespace is now complete (see file AspnetObjects.vb, or use compiled version – AspnetObjects.dll).

    More ASP.NET Articles
    More By Eugene Gilerson


       · Hi, before reading your article, I always wondered "why we need OOP technique in...
     

    ASP.NET ARTICLES

    - How Caching Means More Ca-ching, Part 2
    - How Caching Means More Ca-ching, Part 1
    - Reading a Delimited File Using ASP.Net and V...
    - What is .Net and Where is ASP.NET?
    - An Object Driven Interface with .Net
    - Create Your Own Guestbook In ASP.NET
    - HTTP File Download Without User Interaction ...
    - Dynamically Using Methods in ASP.NET
    - Changing the Page Size Interactively in a Da...
    - XML Serialization in ASP.NET
    - Using Objects in ASP.NET: Part 1/2
    - IE Web Controls in VB.NET
    - Class Frameworks in VB .NET
    - Cryptographic Objects in C#: Part 1
    - Sample Chapter: Pure ASP.Net







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT