When to use a class

Discussion in 'ASP .Net' started by David Lozzi, Feb 7, 2005.

  1. David Lozzi

    David Lozzi Guest

    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
    David Lozzi, Feb 7, 2005
    #1
    1. Advertising

  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/A...9024?creative=327641&camp=14573&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:...
    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
    Steve C. Orr [MVP, MCSD], Feb 7, 2005
    #2
    1. Advertising

  3. David Lozzi

    Mark Rae Guest

    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:...
    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
    Mark Rae, Feb 7, 2005
    #3
  4. 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:...
    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
    Kevin Spencer, Feb 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    2
    Views:
    416
    Tony Morris
    May 7, 2005
  2. E11
    Replies:
    1
    Views:
    4,716
    Thomas Weidenfeller
    Oct 12, 2005
  3. Toke H?iland-J?rgensen
    Replies:
    10
    Views:
    633
    Toke H?iland-J?rgensen
    Jan 1, 2004
  4. christopher diggins
    Replies:
    16
    Views:
    735
    Pete Becker
    May 4, 2005
  5. Joseph Turian
    Replies:
    5
    Views:
    578
Loading...

Share This Page