Maximizing.ASP.dot.NET.Real.World.ObjectOriented.Development [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Maximizing.ASP.dot.NET.Real.World.ObjectOriented.Development [Electronic resources] - نسخه متنی

Jeffrey Putz

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Breaking Away from the Linear Script Way of Thinking


The use of scripting languages on the Web can be credited with bringing us out of the dark ages, causing us to move from static HTML pages to rich applications that let us solve problems, all from a relatively lightweight applicationthe browser. Script is easy to write, which is precisely the reason that ASP, PHP, Cold Fusion, and other scripting languages have been so popular over the years, drawing in people who otherwise might not have been developing for the Web.

Scripting platforms of course have their disadvantages. The worst of them is "spaghetti code," where programming code is mixed in with HTML in little blocks. It's hard to read and maintain. The classic example of this is getting records from a database and then looping through them to create a table. Listing 1.1 shows how we did it in ASP.

Listing 1.1. Old-fashioned ASP


<table>
<%Set db = Server.CreateObject("ADODB.Connection")
db.Open strConnectionString
Set ThePosts = Server.CreateObject("ADODB.Recordset")
sql = "SELECT * FROM MyTable"
ThePosts.Open sql, db, adOpenStatic, adLockReadOnly, adCmdText
Do While Not ThePosts.EOF%>
<tr>
<td><%=rsThePosts("Field1")%></td>
<td><%=rsThePosts("Field2")%></td>
<td><%=rsThePosts("Field3")%></td>
<td><%=rsThePosts("Field4")%></td>
</tr>
<%rsThePosts.MoveNext
Loop
ThePosts.Close
db.Close
Set db = Nothing%>
</table>

Listing 1.2. Getting data in ASP.NET

C#


SqlConnection objConnection = new SqlConnection(myConnectionString);
objConnection.Open();
SqlCommand objCommand = new SqlCommand("SELECT * FROM MyTable", objConnection);
MyDataGrid.DataSource = objCommand.ExecuteReader();
MyDataGrid.DataBind();
objConnection.Close();

In our ASP.NET example (Chapter 5, "Object-Oriented Programming Applied: A Custom Data Class." In that chapter, we'll give you an example of how to encapsulate data access to a particular table that you can use repeatedly in your application, writing SQL and data plumbing only once. Listing 1.3 shows you how the process of instantiating and using a class works.


People often confuse the terms "class," "instance," (or the act of instantiating) and "object." A class is the code itself, or the blueprint, if you will. An instance of the class (a piece of running code) is an object. You can have many instances of the same class. That object in memory is basically its own miniature running program, and you can create as many as you want. Listing 1.3, for example, has two instances of the Customer class. They are two objects that execute the same code. We'll cover classes and objects in more detail later.

The term "encapsulation" gets thrown around a lot, and in a nutshell (if you'll pardon the pun), it's used to describe the wrapping of some bit of functionality into a discrete package. In the object-oriented world, this package is a class.

Listing 1.3. Instantiating and using a class

C#


// Instantiate the Customer class using the default constructor
Customer objCustomer = new Customer();
// Assign some of its properties
objCustomer.LastName = "Jones";
objCustomer.FirstName = "Jeff";
// Call its Create() method to save the values in the database,
// and get its new primary key (CustomerID) value
int intCustomerID = objCustomer.Create();
// Instantiate the Customer class using the constructor that takes
// the CustomerID as a parameter
Customer objCustomer2 = new Customer(intCustomerID);
Trace.Write("LastName: " + objCustomer2.LastName);
Trace.Write("FirstName: " + objCustomer2.FirstName);
// Change the value of the first name then save the changes
// to the database
objCustomer2.FirstName = "Stephanie";
objCustomer2.Update();
// On second thought, let's just delete the record entirely
objCustomer2.Delete();

VB.NET


' Instantiate the Customer class using the default constructor
Dim objCustomer As New Customer()
' Assign some of its properties
objCustomer.LastName = "Jones"
objCustomer.FirstName = "Jeff"
' Call its Create() method to save the values in the database,
' and get its new primary key (CustomerID) value
Dim intCustomerID As Integer = objCustomer.Create()
' Instantiate the Customer class using the constructor that takes
' the CustomerID as a parameter
Dim objCustomer2 As New Customer(intCustomerID)
Trace.Write(("LastName: " + objCustomer2.LastName))
Trace.Write(("FirstName: " + objCustomer2.FirstName))
' Change the value of the first name then save the changes
' to the database
objCustomer2.FirstName = "Stephanie"
objCustomer2.Update()
' On second thought, let's just delete the record entirely
objCustomer2.Delete()

In this example, we never see anything about connections, readers, or queries, even though we manipulate data several times. We've written a class that does all of these things for us and encapsulates that functionality into a nice little package that we can use repeatedly in our application. We'll get to the specifics of that class later in the book.

These examples illustrate that you can look at object-oriented programming in two different ways. On one hand, there's the "user" view of a class. To a user, the internal implementation of a class is unimportant as long as the user knows that it does what he or she needs it to do. On the other hand, you have the "programmer" view of a class, where the design and code that makes the class work is important. If you look at it from both sides, you may better understand the benefits of object-oriented programming.


/ 146