Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > When to use a class

Reply
Thread Tools

When to use a class

 
 
David Lozzi
Guest
Posts: n/a
 
      02-07-2005
Howdy,

I've discovered how to create and use a class in ASP.NET. However, when is the best time to use a class? For example, I am currently using session variables to store user information (user id, user name, full name, security level, department, etc.). Would I do better to create a class instead? Also, if I did, would I simply store the ID in the session and when accessing the class (User.FullName, User.SecurityLevel, etc.) then check the database? I'm not sure what the best route is.

thanks.

--
David Lozzi
Web Applications/Network Specialist
Delphi Technology Solutions, Inc.
dlozzi(remove-this)@delphi-ts.com


 
Reply With Quote
 
 
 
 
Steve C. Orr [MVP, MCSD]
Guest
Posts: n/a
 
      02-07-2005
For basic needs like you describe, a separate class may not be necessary (even though you're still using classes because every page is a class in ASP.NET.)

If your needs get fancier in the future, you might want to create a separate user class though. The user class could encapsulate functionality such as managing permisssions, changing passwords, updating demographic data in the database, etc. It can be good to keep this kind of functionality together in a separate class for easy maintenance and reuse.
This would be the beginning of a middle tier business object, which you can read more about in this highly recommended book:
http://www.amazon.com/exec/obidos/AS...&link_code=as1

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net



"David Lozzi" <dlozzi@(remove-this)delphi-ts.com> wrote in message news:(E-Mail Removed)...
Howdy,

I've discovered how to create and use a class in ASP.NET. However, when is the best time to use a class? For example, I am currently using session variables to store user information (user id, user name, full name, security level, department, etc.). Would I do better to create a class instead? Also, if I did, would I simply store the ID in the session and when accessing the class (User.FullName, User.SecurityLevel, etc.) then check the database? I'm not sure what the best route is.

thanks.

--
David Lozzi
Web Applications/Network Specialist
Delphi Technology Solutions, Inc.
dlozzi(remove-this)@delphi-ts.com


 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      02-07-2005
David,

There's no real right and wrong answer for this type of question, other than to use the method which uses the least amount of resources on your server and returns pages to the user as quickly as possible. If your site has lots of concurrent users, it might be more efficient to store only a small amount of data in the Session object and then fetch other information out of the database as and when required.
"David Lozzi" <dlozzi@(remove-this)delphi-ts.com> wrote in message news:(E-Mail Removed)...
Howdy,

I've discovered how to create and use a class in ASP.NET. However, when is the best time to use a class? For example, I am currently using session variables to store user information (user id, user name, full name, security level, department, etc.). Would I do better to create a class instead? Also, if I did, would I simply store the ID in the session and when accessing the class (User.FullName, User.SecurityLevel, etc.) then check the database? I'm not sure what the best route is.

thanks.

--
David Lozzi
Web Applications/Network Specialist
Delphi Technology Solutions, Inc.
dlozzi(remove-this)@delphi-ts.com


 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      02-07-2005
Hi David,

You are correct to want to understand what your tools are before attempting
to use them. In OOP, understanding what classes are is as important as
knowing what Integers are. After all, a class is a data type, just as an
Integer is. It wouldn't make much sense to use Integers if you didn't know
anything about them. You might just as well walk onto a construction site,
pick up a hammer gun, and inject a nail into your foot, eh?

A class is a definition for a data type. Data falls into 2 broad categories:
State and Process. A function is also a data type. It is a Process. It
manipulates data and returns a value. It executes. An Integer, on the other
hand, is State. It is a storage of data that is not process, but simply
data, such as the number 1.

A structure is a primitive data type that can hold both State and Process.
It can have members which are data, and members which are functions or
procedures. The structure is actually the ancestor of the class. It is the
first data type that was designed to hold both State and Process.

A class is an object-oriented structure. It holds both State and Process. It
has additional object-oriented characteristics, just as everything else in
OOP does. It has inheritance, polymorphism, encapsulation, etc. So, while
you can't inherit a structure, and derive another structure from it, you
certainly CAN inherit a class and derive another class from it.

All classes in .Net inherit (at least) System.Object. System.Object has the
bare minimum of what is needed to define a class. The beauty of OOP is that
you can define a base class which has all the characteristics common to a
whole "class" of classes, and define each of the classes as inheriting the
base class, eliminating the need to duplicate all of the code in the base
class. So, the CLR, for example, looks much like a tree, with System.Object
at the root, and a whole plethora of derived classes branching out from
there.

So, when to use a class? Well, if you've done procedural programming for
awhile, you should be aware that, from time to time, multiple functions need
to do the same types of operations. When this happens, rather than
duplicating code all over the place, you create another function that
performs the common task, and call it from your other functions.

The idea of a structure, which is procedural in nature, is that not only do
multiple functions sometimes need to perform the same types of operations,
sometimes they need to perform the same types of operations on the same
data, or the same types of data. A structure, which stores both State and
Process, fills the bill. OOP is simply an extension of procedural
programming, in which things are more encapsulated. In a procedural program,
you don't have (or have as easily) a mechanism for inheritance. In OOP, you
do. You have classes.

Designing classes requires an understanding of OOP, especially inheritance.
Good class design can reduce the amount of code that you work with (and
debug), and simplify your code overall, making it much easier to debug and
maintain. The basic principle to pay attention to is looking for duplicated
code, data, or functionality. When you see 2 blocks of code doing similar
things, you should suspect that some classes might be in order.

Of course, there's a lot more to designing classes than that. OOP isn't
called "object-oriented" for nothing. Generally, for example, a class holds
State and Process that are related in some way. In other words, classes are
logically structured. You wouldn't, for example, create a File IO class, and
have it execute a database operation. That would be like putting your shoes
in the refrigerator. Yes, the refrigerator is just fine for storing shoes,
but how are you going to remember that the refrigerator is where you put
them? A swiss army knife is a handy little gadget to have, but if it was
much larger, you might never find the nail clippers!

As for when to use them, well, if you're coding with .Net, you already are!
The CLR is nothing but a huge class library.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"David Lozzi" <dlozzi@(remove-this)delphi-ts.com> wrote in message
news:(E-Mail Removed)...
Howdy,

I've discovered how to create and use a class in ASP.NET. However, when is
the best time to use a class? For example, I am currently using session
variables to store user information (user id, user name, full name, security
level, department, etc.). Would I do better to create a class instead? Also,
if I did, would I simply store the ID in the session and when accessing the
class (User.FullName, User.SecurityLevel, etc.) then check the database? I'm
not sure what the best route is.

thanks.

--
David Lozzi
Web Applications/Network Specialist
Delphi Technology Solutions, Inc.
dlozzi(remove-this)@delphi-ts.com



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
How to use genericized Class class hiwa Java 2 05-07-2005 06:38 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Use a class as a variable type in another class / include file. Toke H?iland-J?rgensen C++ 10 01-01-2004 04:31 AM



Advertisments