| For More Information... |
|---|
| To learn more about ASP.NET be sure to visit the 4Guys ASP.NET Article Index. Before you dive into this article be sure you have read Ian's other articles on using OOP with ASP.NET: |
Overview:
So you're wandering around the Internet on your usual binge of daily information and you
spot yet another article on .Net and what it will do for you. Surprised? Probably not.
You can find roughly 2.9 trillion articles on data access using ASP.NET, but what about the
big picture? Why is it going to be better? The first thing that comes to my mind is not
"I can use ASP.NET to access my data!". I could already do that. But one thing I would like
to point out is the ability to use Object Oriented Programming (OOP) within the ASP.NET
environment. This is the most substantial difference between the old and new environments.
In my previous articles (OOP Overview | Encapsulation | Inheritence) I discussed just what OOP, encapsulation, and inheritance are and also how to apply them using C#. But Object Orientated Programming requires one more piece, polymorphism.
What is Polymorphism?
Polymorphism by definition means taking many forms. In C# it means the ability for classes
to share the same methods (actions) but implement them differently.
For instance, say we create a class called Shape and this class has a method
called .draw() which draws the shape onto the user interface. Then we create
two subclasses, using inheritance, of this Shape class. One called
Square, the other called Circle. Now obviously a square and
circle are two entirely different shapes, yet both classes have the .draw() method.
When the Square.draw() method is called it will draw a square on the user
interface. When the Circle.draw() method is called, it will draw a circle on
the user interface. So both classes can use the same methods but implement them differently.
Lets See the Code!
In my last article I showed how to create a
Profile class that stores user data and also a subclass called
ExtendedProfile that inherits from this Profile class and then
extends it, adding properties and methods. So let's use the same example to show
polymorphism in action. I decided that I needed to add a
.save() method that will allow both classes to save their data to an external
source. So I created and interface called ISaveData with the method signature
for the .save() method.
|
Side note: What is an interface? In simple terms, an interface allows you to create
class and method signatures and inherit these methods in your classes by simply implementing
this interface. You can then create methods that cross class boundaries, such as our
.save() method.
We then inherit this method into the base class, Profile, by using a
semi-colon in the class declaration:
public class Profile : ISaveData
|
Now this class inherits the .save() method from the ISaveData interface.
For the Profile class I decided that I wanted to save the data in binary
format. We will serialize the data and save it to a .bin file, where we can
later restore the object along with it's state.
|
This will save the state of the object in a binary file called profile.bin.
We can then later recreate the state of the object when it was saved by deserialization.
Side note: "Serialization is the process of converting a graph of objects, located in memory, into a linear sequence of bytes. That sequence of bytes can be sent elsewhere (for example, to a remote computer) and Deserialized, thereby making an exact clone in that remote memory of the original graph of objects. " -.Net Framework SDK documentation, Beta1.
We have now implemented the .save() method in our Profile
class. Since ExtendedProfile inherits from our Profile class,
ExtendedProfile can also create its own implementation of the .save()
method! We'll look at how to accomplish this in Part 2.