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
|