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.
Next: The Constructors >>
More ASP.NET Articles
More By Addison-Wesley/Prentice Hall PTR
|
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.
|
|