OOP and instanstiation question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VMI

    #1

    OOP and instanstiation question

    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.storeDa ta(person1);
    }

    Is this the correct way?

    Thanks.
  • sloan

    #2
    Re: OOP and instanstiation question


    See my may 2006 blog entry


    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.




    "VMI" <VMI@discussion s.microsoft.com wrote in message
    news:E5A66CD8-873A-4037-8F9B-D5B60536670B@mi crosoft.com...
    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.storeDa ta(person1);
    }
    >
    Is this the correct way?
    >
    Thanks.

    Comment

    • Dustin Campbell

      #3
      Re: OOP and instanstiation question

      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.storeDat a(person1);
      }

      Best Regards,
      Dustin Campbell
      Developer Express Inc.


      Comment

      • Registered User

        #4
        Re: OOP and instanstiation question

        On Tue, 10 Oct 2006 12:33:01 -0700, VMI
        <VMI@discussion s.microsoft.com wrote:
        >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.storeDa ta(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.storeDa ta();

        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.storeDa ta();
        with the method being something like
        public void storeData()
        {
        new PersonDB().stor eData(this);
        /*
        PersonDB personDB = new PersonDB()
        personDB.storeD ata(this);
        */
        }

        regards
        A.G.

        Comment

        Working...