ASP.NET
  Home arrow ASP.NET arrow Page 2 - Object-Oriented Programming Applied: A Cus...
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

Object-Oriented Programming Applied: A Custom Data Class
By: Addison-Wesley/Prentice Hall PTR
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2005-08-11

    Table of Contents:
  • Object-Oriented Programming Applied: A Custom Data Class
  • Analyzing Design Requirements
  • The Constructors
  • Create, Update, and Delete Methods
  • Caching the Data for Better Performance
  • Getting More than One Record at a Time
  • Summary

  • 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


    Object-Oriented Programming Applied: A Custom Data Class - Analyzing Design Requirements


    (Page 2 of 7 )

    The ?rst step in designing any class is to identify your needs in human terms before writing code. In our case, we want to make it easy to get, update, and delete data from a table called Customers in SQL Server. None of the columns in our table allows null values.

    Table 5.1 The Customers table

    CustomerID (primary key/identity) int
    LastName nvarchar
    FirstName nvarchar
    Address nvarchar
    City nvarchar
    State nvarchar
    Zip nvarchar
    Phone nvarchar
    SignUpDate datetime

     

    Why are we using nvarchar instead of varchar? The difference is that nvarchar uses Unicode, the generally accepted standard of character encoding that includes a much larger character set. Using Unicode in your Web application means there’s less chance of getting weird characters generated by users in other countries. The tradeoff is that it takes up twice as much disk space, but in an age of giant inexpensive hard drives, this should hardly be a concern.

    We know that the Customers table has nine columns that we can manipulate. We also know that we want to create, update, and delete records in this table. The most obvious need we’ll have is to get data from the table. After we have the basics of our class nailed down, we’ll revise the class to cache data and explore ways to get a number of records at one time.

    Choosing Our Properties

    Let’s start writing our class by declaring it and creating the necessary references that we’ll need in Listing 5.2. We’ll also set up the properties and corresponding private variables.

    Listing 5.2 The start of our data class

    C#
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;

    namespace UberAspNet
    {
      public class Customer
      {
        private int _CustomerID;
        public int CustomerID
        {
          get {return _CustomerID;}
        }
        private string _LastName;
        public string LastName
        {
          get {return _LastName;}
          set {_LastName = value;}
        }

        private string _FirstName;
        public string FirstName
        {
          get {return _FirstName;}
          set {_FirstName = value;}
        }

        private string _Address;
        public string Address
        {
          get {return _Address;}
          set {_Address = value;}
        }

        private string _City;
        public string City
        {
          get {return _City;}
          set {_City = value;}
        }

        private string _State;
        public string State
        {
          get {return State;}
          set {_State = value;}
        }

        private string _Zip;
        public string Zip
        {
          get {return _Zip;}
          set {_Zip = value;}
        }

        private string _Phone;
        public string Phone
        {
          get {return _Phone;}
          set {_Phone = value;}
        }

        private DateTime _SignUpDate;
        public DateTime SignUpDate
        {
          get {return _SignUpDate;}
          set {_SignUpDate = value;}
        }
      }
    }

    VB.NET
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Web

    Namespace UberAspNet

      Public Class Customer

        Private _CustomerID As Integer
        Public ReadOnly Property CustomerID() As Integer
          Get
            Return _CustomerID
          End Get
        End Property

        Private _LastName As String
        Public Property LastName() As String
          Get
            Return _LastName
          End Get
          Set
            _LastName = value
          End Set
        End Property

        Private _FirstName As String
        Public Property FirstName() As String
          Get
            Return _FirstName
          End Get
          Set
            _FirstName = value
          End Set
        End Property

        Private _Address As String
        Public Property Address() As String
          Get
            Return _Address
          End Get
          Set
            _Address = value
          End Set
        End Property

        Private _City As String
        Public Property City() As String
          Get
            Return _City
          End Get
          Set
            _City = value
          End Set
        End Property

        Private _State As String 
        Public Property State() As String
          Get
            Return State
          End Get
          Set
            _State = value
          End Set
        End Property

        Private _Zip As String
        Public Property Zip() As String
          Get
            Return _Zip
          End Get
          Set
            _Zip = value
          End Set
        End Property

        Private _Phone As String 
        Public Property Phone() As String
          Get 
            Return _Phone
          End Get
          Set
            _Phone = value
          End Set
        End Property

        Private _SignUpDate As DateTime
        Public Property SignUpDate() As DateTime
          Get
            Return _SignUpDate
          End Get
          Set
            _SignUpDate = value
          End Set
        End Property

      End Class

    End Namespace

    The code is fairly straightforward. We’ve created a property to correspond to each of the columns in our database table, matching the data types. We’ve also created a private variable for each column for internal use in our class. The only unusual thing here is that we’ve made the CustomerID property read-only. That’s because our database table is designed to make this a primary key and an identity ?eld, meaning that SQL Server will number the column automatically when we add new rows. For that reason, we don’t want the class to have the ability to alter this property. This read-only strategy also protects other developers (or yourself if you can’t remember every detail about what you’ve written) from doing something that could break the program.

    In our case we’ve named the private variables with the same name as the properties, only with an underscore character in front of them. There are a number of different ways to name these according to various academic standards and recommendations, but ultimately it’s up to you. Be consistent in your naming conventions. If they confuse you, imagine what they might do to other developers who need to edit your code!

    You could declare default values for each private variable, but we’re going to defer those assignments to our constructors. We might want to assign different default values depending on the overload of the constructor called.

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


       · Great article! I can never over emphasize the importance of a good data abstraction...
       · Great Article!!
     

    Buy this book now. This article is excerpted from chapter five of the book Maximizing ASP.NET: Real World, Object-Oriented Development, written by Jeffery Putz (Addison-Wesley Professional, 2005; ISBN: 0321294475). Check it out at your favorite bookstore. Buy this book now.

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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