473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

When to use a class

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
Nov 19 '05 #1
3 1397
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:OC**************@TK2MSFTNGP14.phx.gbl...
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
Nov 19 '05 #2
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:OC**************@TK2MSFTNGP14.phx.gbl...
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
Nov 19 '05 #3
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:OC**************@TK2MSFTNGP14.phx.gbl...
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

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Charles Law | last post by:
I have an application that creates a class. The class has unmanaged resources, so must end gracefully. How can I guarantee that the unmanaged resources are freed? I have looked at IDisposable,...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
0
by: tony | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. In this user control we have a class...
18
by: cj | last post by:
members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe. I'm under the impression before you can use a class you have to make an...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
15
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
1
by: robin1983 | last post by:
Dear All, I got stuck in simple problem, I have a two php file one for registration form and one for to check and insert into the table. The problem is that when I get any kind error in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.