Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 4th, 2004, 01:24 PM
Authorized User
 
Join Date: Aug 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Designing data access classes in ASP.NET

Hello,

I am wondering what the best practice is when it comes to creating get/set accessors in a reusable class in .NET when you are accessing information via a SQL database. Here is an example:

This is a "Member" class that allows access to a members information.

public class member
{
public string FirstName
{
get {return mFname;}
}

public string LastName
{
get {return mLname;}
}

public DateTime Birthdate
{
get {return new DateTime(birthYear,birthMonth,birthDay);}
}

// here is a function that gathers the info from a db.

public void getMememberInfo(int ID)
        {
            SqlConnection objCon = new SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"));
            objCon.Open();

                string SQL = "SELECT * FROM member WHERE id="+ID;
                SqlCommand objCommand = new SqlCommand(SQL, objCon);
                SqlDataReader objReader = objCommand.ExecuteReader();

                while (objReader.Read())
                {
                    mFname = objReader["mem_fname"].ToString();
                                        birthMonth = (int) objReader["bday_month"];
                    birthDay = (int) objReader["bday_day"];
                    birthYear = (int) objReader["bday_year"];
                }
                objCon.Close();
        }
}


Basically, I would like to know when setting these properties, like name, is the best way to create a set accessor for each property and then create a static function which creates a connection, runs a database update query? This seems inefficient. Also, is this the best way to collect the member info and pass it to the Get accessors?

Thanks,

-- shawn


 
Old August 4th, 2004, 02:02 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What you have is pretty much the way I do it.

Regarding the Set accessors: usually it's not a good idea to have each one hit the DB. Just set the internal fields, then run an update method to update the whole thing.
 
Old August 4th, 2004, 02:32 PM
Authorized User
 
Join Date: Aug 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Whats the best way to fire the update events? Would it be better to create a function that you can call to update manually or internal to the get/set class that auto detects changes to the internal fields?

 
Old August 4th, 2004, 02:37 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I wouldn't recommend auto-updating as this would cause accessive DB hits when you only need one.

Create an update method on the class that performs the single update. Usually the consuming code would know that it is updating the object and that the update would require a save to the db, so you can set all the fields, then call the update method.





Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET v2.0 designing and programming madlo BOOK: ASP.NET Website Programming Problem-Design-Solution 5 January 4th, 2006 09:12 AM
Using classes in Data Access Pages jmischo Access VBA 0 December 14th, 2004 06:11 PM
SQL Access/ASP.NET data access issue saeta57 SQL Server ASP 1 July 4th, 2004 04:29 PM
SQL Access/ASP.NET data access issue saeta57 Classic ASP Databases 1 July 4th, 2004 03:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.