473,748 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SecurityLe vel, 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 1415
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@(remo ve-this)delphi-ts.com> wrote in message news:OC******** ******@TK2MSFTN GP14.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.SecurityLe vel, 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@(remo ve-this)delphi-ts.com> wrote in message news:OC******** ******@TK2MSFTN GP14.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.SecurityLe vel, 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@(remo ve-this)delphi-ts.com> wrote in message
news:OC******** ******@TK2MSFTN GP14.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.SecurityLe vel, 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
7516
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, but this seems to rely on a call from the application, e.g. MyClass.Dispose()
18
4743
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
3
5710
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 have a base class with several sub classes. In runtime I want to create a instance of a sub class and assign it to a base class pointer. Nothing fancy about that. I also want to be able in runtime to decide witch type of sub class that is to be...
0
1754
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 called B One project that build a class library dll. In this class library we have a class called C In the user control project I have a project reference to the class library.
18
1981
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 instance of it. So how can a class be threadsafe by itself but an instance of it not be? I guess I don't get what exactly being threadsafe means. Multiple theads can use the same instance of a class?
8
4271
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 not valid"). How do I fix it ? using System.Diagnostics; .. .. class NewProcess: Process {
15
2171
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 (a_obj) of a class type (A), then we need to define A as well, but if B has a pointer to A, then we only need to forward declare A. I was told this is because the compiler needs to see the implemenation of A when allocating memory for a_obj. ...
1
2382
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 validiation the code is working properly but when the condition are fulfilled the code inside the if part is not executing. The following is the code for registration form UserAdd.php <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta...
0
8983
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8822
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9528
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.