OOP and instanstiation question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a class Person that includes several properties (i.e. Name, ID) and a
method storeData() that stores these same values to a database.
What's the correct method of instantiating this class when another method in
another class is storing data to the properties and calling storeData()? I'm
currently doing it this way:
protected void Button_Click(...)
{
Person person1 = new Person();
person person2 = new Person();
person1.Name = "John Smith";
person1.ID = "123";
person2.storeData(person1);
}

Is this the correct way?

Thanks.
 
See my may 2006 blog entry
http://sholliday.spaces.live.com/?_...ogview&_c=blogpart&partqs=amonth=5&ayear=2006

Hopefully the sample will provide an illustration to avoid common mistakes.
Those mistakes are:

1. Intertwined business layer logic and database connectivity.
2. Substituting the SQLHelper class ~in place of~ the application's
DataLayer object. The SQLHelper (as the name implies) is there to
assist/help the DataLayer object, not replace it.
3. Keeping objects (their properties, methods and sometimes events)
seperate from the code which creates them.


You're flirting with #1.
And if you start down the road of #1, you'll probably end up doing #3.
 
Is this the correct way?

Why not make your storeData() method static so that you can call it like
this:

protected void Button_Click(...)
{
Person person1 = new Person();
person1.Name = "John Smith";
person1.ID = "123";
Person.storeData(person1);
}

Best Regards,
Dustin Campbell
Developer Express Inc.
 
I have a class Person that includes several properties (i.e. Name, ID) and a
method storeData() that stores these same values to a database.
What's the correct method of instantiating this class when another method in
another class is storing data to the properties and calling storeData()? I'm
currently doing it this way:
protected void Button_Click(...)
{
Person person1 = new Person();
person person2 = new Person();
The c'tor indicates person2 should be of type Person rather than type
person ... or is it the otherway around?
person1.Name = "John Smith";
person1.ID = "123";
person2.storeData(person1);
}

Is this the correct way?
It depends if person1 and person2 are of the same type or not. If they
are of the same type then the storeData method should serialize the
object's data as in
person1.storeData();

If Person and person are different types then you're moving in the
right direction but the naming should be more distinctive/descriptive.
Even then I would go with
person1.storeData();
with the method being something like
public void storeData()
{
new PersonDB().storeData(this);
/*
PersonDB personDB = new PersonDB()
personDB.storeData(this);
*/
}

regards
A.G.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top