logo

ASP.NET: Representing Data As Objects in Classes

We've now seen how to use an ASP.NET Wen Application to connect to a database and show data from it. To do this the code in our ASPX files directly accessed the "data layer". There's a better way.

Adding Some Class(es)

The beauty of ASP.NET programming for me, is the ability to quickly and easily create your own classes.

To demonstrate let's create an Animal class. First add a folder called Objects to the your project and then add a Class file called Animal.cs, like so:

image

This will generate a file with the code snippet seen below:

image

It has used the namespace Zoo.Objects because I put the class file in a folder called "Objects". First thing I'd do is change the namespace simply to "Zoo".

Then we can start to add properties to the Animal class, so that when we create objects based on it, we can reference things like Name, Age, Type etc.

Before long it will look like this:

image

With this new class in place and saved, switch back to the Page_Load event in the Default.asp.cs file and try typing things like this to create a new object based on the Animal class:

image

As you begin to type "Animal" you'll notice VS's type-ahead already knows about it (no messy importing of classes or classpath redefining to do like in Java).

Notice how all the properties are strongly-types. You can only assign a DateTime to the dateOfArrival property etc.

In practice you'd rarely do this. More often than not you'd load Animal-based objects from a backend database. Let's see how.

First thing to do is add a Constructor to the Animal class that takes a DataRow as its parameter. Knowing the names of the columns we'd expect in the data we can then assign the object properties based on the values received. Like so:

image

Notice above that I added a new class to the same package called AnimalFactory. This "factory" will be responsible for returning to us a List (which we can iterate over using foreach() etc) of Animals.

Note: The AnimalFactory and its methods are static! This means we don't need to first create an object based on it before being able to use the methods. So, instead of needing to write:

AnimalFactory factory = new AnimalFactory();
List<Animal> animals = factory.GetAllAnimals();

You'd just use this line of code:

List<Animal> animals = AnimalFactory.GetAllAnimals();

You'll see this in use in a moment.

All references to the Animal TableAdapter and actual SQL Column names are now only ever contained within the Animal Class itself, rather than being littered about the site in all our .aspx.cs files.

The Default.aspx.cs file now simply looks like this:

image

Note: The calls to "using System.Data" and "using Zoo.ZooTableAdapters" have now gone from this file.

Now, we can do funky things like this:

image

Or as an alternative in shorthand:

image

In both cases the result when viewed on the web is like so:

image

Notice the column names (which are still being auto-generated by the GridView element) now come from the properties we defined for the class, rather than the column names of the SQL table.

Again, remember, that I'm not saying this is how best to create a "view" of data in the real world. We're still just proving a point.

Summary

Hopefully this has shown you how easy it is to create your own objects/classes in C#. Not only how easy it is but also how amazing type-ahead is in Visual Studio. I've always known the principles of Object Orientated programming and how to do this in the likes of Java, but it more-often-than-not requires remembering properties and class names etc. In VS it makes using your own code a breeze!

What this doesn't show is the power of custom classes. Later on we'll take it further and add methods such as Save() and Delete() to the Animal class and methods like GetAnimalByID() to the AnimalFactory class.

For now though, this should be good enough to get you familiar with the concept. Hopefully, before long and like me, you'll get your head round it and never look back.

Before I embarked on learning ASP.NET 18 months or so ago my OO skills were lacking to say the least. Now, thanks mainly to how easy VS makes it, I'm coming along leaps and bounds.

Comments

    • avatar
    • Nick
    • Wed 2 Feb 2011 09:31 AM

    I agree with your last statement, my oo coding was the same, and using these type aheads for these classes and all, makes it so much easier.

    Keep up the good work. I am enjoying seeing you write these articles and reminding me of my learning curve on this stuff.....where were you when I first switched over??

      • avatar
      • Jake Howlett
      • Wed 2 Feb 2011 10:04 AM

      I was probably banging my head against a big domino-shaped brick wall.

    • avatar
    • Sam
    • Wed 2 Feb 2011 03:36 PM

    Another question - I'm getting an error that "The type or namespace name 'AnimalsTableAdapter' could not be found (are you missing a using directive or an assembly reference?)". I already figured out that I needed to add the System.Data namespace in order to get the DataRow parameter - is there a different namespace I need to reference here?

      • avatar
      • Jake Howlett
      • Wed 2 Feb 2011 03:41 PM

      Woops. Yeah I missed off the "using" section of the Animal.cs file in the grab above.

      You'll need the following line too:

      using Zoo.ZooTableAdapters;

      My bad

      Hide the rest of this thread

        • avatar
        • Sam
        • Thu 3 Feb 2011 09:49 AM

        No worries - it's working great now. Thanks again!

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Wed 2 Feb 2011

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content