Proper Design

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

    #1

    Proper Design

    Hi there,

    I am writing a simple program that will connect to database.
    Database has 2 tables, let's call them father and child.
    This is one to many relationship.
    I would like to create corresponding classes for each table.
    Each class would have functions Insert, Edit, Delete
    I would like to find some information how to properly design my classes.

    I used to program in php and delphi.
    So I used to create separe class that corresponded to each table in
    database.
    In Father class I used to crate an array of child.
    whenever Father was inserting new record to database, it was also inserting
    corresponding record(s) into child.

    I can easly do it, but would like to find out what is the best way to do it.

    For instance, if there are 2 tables in database (father and child)
    Father has
    Id Integer
    FatherName String

    Child has
    Id Integer (corresponds to father Id)
    ChildName String

    How to design 2 classes fot both tables?

    Thanks,
    John


  • Spam Catcher

    #2
    Re: Proper Design

    "John" <j_stus@hotmail .comwrote in
    news:uelXz0uVHH A.4632@TK2MSFTN GP04.phx.gbl:
    I can easly do it, but would like to find out what is the best way to
    do it.
    >
    For instance, if there are 2 tables in database (father and child)
    Father has
    Id Integer
    FatherName String
    >
    Child has
    Id Integer (corresponds to father Id)
    ChildName String
    >
    How to design 2 classes fot both tables?
    You can use a Typed Dataset... or take a look at an ORM mapper. Saves you
    the troble of writing your own classes. I personally use LLBLGen Pro (very
    well reviewed). There are other projects out there like NHibernate,
    CodeSmith, etc.

    Comment

    • RobinS

      #3
      Re: Proper Design


      There are a bunch of ways to do this. You might want to check out Deborah
      Kurata's new book, "Doing Objects in VB2005". It shows how to use the
      3-layer methodology, separating your business layer from your data layer
      from your UI layer. It also shows how to design your data and set up
      corresponding classes and their methods and properties. I found it hugely
      understandable and helpful.

      For this case, I would do this:

      -- set up a class for Father
      -- set up a class for Child
      -- set up a class for ChildList that is a list of children

      On the ChildList class, I would do one of two things.

      If you are ever going to want to see a list of children for more than one
      father, make one of the properties of the Child class the FatherID, and
      then every entry in the ChildList will have its associated FatherID. If you
      do this, you want to make the default constructor for ChildList load all of
      them, but you want another constructor that just loads the children for a
      specified FatherID.

      If you are only ever going to want to see a ChildList for a specific
      Father, put the FatherID property in the ChildList class, and just have
      one, versus one for each Child in the list. In this case, I would make the
      default constructor private, and add a constructor that accepts FatherID.
      This way, you can't accidentally instantiate ChildList w/o giving the
      Father.

      If you are always going to want to see the ChildList with the Father, add a
      property to the Father class that holds a ChildList. Otherwise, just
      define it wherever you're going to use it.

      Hope this helps. If you need more info, or specific code, post back.

      Robin S.
      ------------------------------
      "John" <j_stus@hotmail .comwrote in message
      news:uelXz0uVHH A.4632@TK2MSFTN GP04.phx.gbl...
      Hi there,
      >
      I am writing a simple program that will connect to database.
      Database has 2 tables, let's call them father and child.
      This is one to many relationship.
      I would like to create corresponding classes for each table.
      Each class would have functions Insert, Edit, Delete
      I would like to find some information how to properly design my classes.
      >
      I used to program in php and delphi.
      So I used to create separe class that corresponded to each table in
      database.
      In Father class I used to crate an array of child.
      whenever Father was inserting new record to database, it was also
      inserting corresponding record(s) into child.
      >
      I can easly do it, but would like to find out what is the best way to do
      it.
      >
      For instance, if there are 2 tables in database (father and child)
      Father has
      Id Integer
      FatherName String
      >
      Child has
      Id Integer (corresponds to father Id)
      ChildName String
      >
      How to design 2 classes fot both tables?
      >
      Thanks,
      John
      >

      Comment

      Working...