Advice on good OO design

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

    Advice on good OO design

    Hi,

    I'm just getting started with real OO design and programming and am
    after a little advice.

    Basically, I've got a Customer class and an Agency class. The Agency
    class inherits all the properties and methods from Customer but I have
    come accross a couple of problems:

    I've got a Save, Add and Delete method in both of the objects. I'm going
    on the assumption that it's a good idea to allow each object to control
    itself. Each of these methods in the Agency class override the methods
    in the Customer class and the base.Save etc methods are added to each of
    the methods in the Agency class to fire off the assiciated method in the
    Customer class before performing it's own Save etc.

    I would like to pass in a database connection object to these classes
    and force the Save methods to run in a transaction as there's no point
    saving the Agency details without the Customer details. This is the area
    that is causing me problems. I am currently passing the connection and
    transaction objects directly into the Agency object via a constructor
    and I then set relevant properties in the Customer object using
    base.localDbCon nection so that the Customer object has a connection to
    work with. Bearing in mind that I am probably going to introduce
    additional variants of Customer later, is this a good way of managing
    the database connections etc ?

    Any advice would be appreciated.

    Rob




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Chris Hornberger

    #2
    Re: Advice on good OO design

    I don't think you should allow your customer class (put aside any
    inheritence issues a sec) to add, save or delete themselves at all.
    Think about it from a Real World perspective. You don't call up John
    the customer and ask him to drop by and delete himself from your
    records. You have an administrator.. . or a "factory class"... handle
    the responsibilitie s of creating, modifying and removing customers
    (and other objects) from the data storage container.

    Rob Thomas <rob@rtcomputer systems.com> wrote in message news:<uwoeLY2VD HA.1984@TK2MSFT NGP11.phx.gbl>. ..[color=blue]
    > Hi,
    >
    > I'm just getting started with real OO design and programming and am
    > after a little advice.
    >
    > Basically, I've got a Customer class and an Agency class. The Agency
    > class inherits all the properties and methods from Customer but I have
    > come accross a couple of problems:
    >
    > I've got a Save, Add and Delete method in both of the objects. I'm going
    > on the assumption that it's a good idea to allow each object to control
    > itself. Each of these methods in the Agency class override the methods
    > in the Customer class and the base.Save etc methods are added to each of
    > the methods in the Agency class to fire off the assiciated method in the
    > Customer class before performing it's own Save etc.
    >
    > I would like to pass in a database connection object to these classes
    > and force the Save methods to run in a transaction as there's no point
    > saving the Agency details without the Customer details. This is the area
    > that is causing me problems. I am currently passing the connection and
    > transaction objects directly into the Agency object via a constructor
    > and I then set relevant properties in the Customer object using
    > base.localDbCon nection so that the Customer object has a connection to
    > work with. Bearing in mind that I am probably going to introduce
    > additional variants of Customer later, is this a good way of managing
    > the database connections etc ?
    >
    > Any advice would be appreciated.
    >
    > Rob
    >
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it![/color]

    Comment

    • james

      #3
      Re: Advice on good OO design

      This is known as Responsibility Driven OOP. The question to ask is
      "who's responsibility is it to perform this action". Your example of
      the Customer coming over to delete himself from the database
      is a perfect example of placing the responsibility on the wrong class.
      Great example !

      This is the biggest mistake new OO programmers make. They use
      objects to do procedural programming. I have found that by asking
      the simple question or responsibility, that these problems can be
      turned from procedural to OO


      JIM


      "Chris Hornberger" <chris@chornbe. com> wrote in message
      news:53ec8913.0 307310855.58c13 372@posting.goo gle.com...[color=blue]
      > I don't think you should allow your customer class (put aside any
      > inheritence issues a sec) to add, save or delete themselves at all.
      > Think about it from a Real World perspective. You don't call up John
      > the customer and ask him to drop by and delete himself from your
      > records. You have an administrator.. . or a "factory class"... handle
      > the responsibilitie s of creating, modifying and removing customers
      > (and other objects) from the data storage container.
      >
      > Rob Thomas <rob@rtcomputer systems.com> wrote in message[/color]
      news:<uwoeLY2VD HA.1984@TK2MSFT NGP11.phx.gbl>. ..[color=blue][color=green]
      > > Hi,
      > >
      > > I'm just getting started with real OO design and programming and am
      > > after a little advice.
      > >
      > > Basically, I've got a Customer class and an Agency class. The Agency
      > > class inherits all the properties and methods from Customer but I have
      > > come accross a couple of problems:
      > >
      > > I've got a Save, Add and Delete method in both of the objects. I'm going
      > > on the assumption that it's a good idea to allow each object to control
      > > itself. Each of these methods in the Agency class override the methods
      > > in the Customer class and the base.Save etc methods are added to each of
      > > the methods in the Agency class to fire off the assiciated method in the
      > > Customer class before performing it's own Save etc.
      > >
      > > I would like to pass in a database connection object to these classes
      > > and force the Save methods to run in a transaction as there's no point
      > > saving the Agency details without the Customer details. This is the area
      > > that is causing me problems. I am currently passing the connection and
      > > transaction objects directly into the Agency object via a constructor
      > > and I then set relevant properties in the Customer object using
      > > base.localDbCon nection so that the Customer object has a connection to
      > > work with. Bearing in mind that I am probably going to introduce
      > > additional variants of Customer later, is this a good way of managing
      > > the database connections etc ?
      > >
      > > Any advice would be appreciated.
      > >
      > > Rob
      > >
      > >
      > >
      > >
      > > *** Sent via Developersdex http://www.developersdex.com ***
      > > Don't just participate in USENET...get rewarded for it![/color][/color]


      Comment

      • Thomas Tomicek [MVP]

        #4
        Re: Advice on good OO design

        eing one of the few people that ever write a professional commercial
        persistence layer (for .NET, btw.) let me second this.

        Load, Save, query etc has NOTHING to do with the object per se - these are
        operations that belong to a manager class. Not to the object.

        BESIDES you never save the object, you save the object plus maybe subobjects
        that got created etc.

        --
        Regards

        Thomas Tomiczek
        THONA Software & Consulting Ltd.
        (Microsoft MVP C#/.NET)

        "Chris Hornberger" <chris@chornbe. com> wrote in message
        news:53ec8913.0 307310855.58c13 372@posting.goo gle.com...[color=blue]
        > I don't think you should allow your customer class (put aside any
        > inheritence issues a sec) to add, save or delete themselves at all.
        > Think about it from a Real World perspective. You don't call up John
        > the customer and ask him to drop by and delete himself from your
        > records. You have an administrator.. . or a "factory class"... handle
        > the responsibilitie s of creating, modifying and removing customers
        > (and other objects) from the data storage container.
        >
        > Rob Thomas <rob@rtcomputer systems.com> wrote in message[/color]
        news:<uwoeLY2VD HA.1984@TK2MSFT NGP11.phx.gbl>. ..[color=blue][color=green]
        > > Hi,
        > >
        > > I'm just getting started with real OO design and programming and am
        > > after a little advice.
        > >
        > > Basically, I've got a Customer class and an Agency class. The Agency
        > > class inherits all the properties and methods from Customer but I have
        > > come accross a couple of problems:
        > >
        > > I've got a Save, Add and Delete method in both of the objects. I'm going
        > > on the assumption that it's a good idea to allow each object to control
        > > itself. Each of these methods in the Agency class override the methods
        > > in the Customer class and the base.Save etc methods are added to each of
        > > the methods in the Agency class to fire off the assiciated method in the
        > > Customer class before performing it's own Save etc.
        > >
        > > I would like to pass in a database connection object to these classes
        > > and force the Save methods to run in a transaction as there's no point
        > > saving the Agency details without the Customer details. This is the area
        > > that is causing me problems. I am currently passing the connection and
        > > transaction objects directly into the Agency object via a constructor
        > > and I then set relevant properties in the Customer object using
        > > base.localDbCon nection so that the Customer object has a connection to
        > > work with. Bearing in mind that I am probably going to introduce
        > > additional variants of Customer later, is this a good way of managing
        > > the database connections etc ?
        > >
        > > Any advice would be appreciated.
        > >
        > > Rob
        > >
        > >
        > >
        > >
        > > *** Sent via Developersdex http://www.developersdex.com ***
        > > Don't just participate in USENET...get rewarded for it![/color][/color]


        Comment

        Working...