Best practices question.

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

    #1

    Best practices question.

    Hi everybody,

    I would like to hear your thoughts on the following problem.

    We have the following classes.

    Class Exam
    int ID*
    int Version*
    string Name

    Class Requirement
    Int version*
    String name
    Int modelnumber*


    In every class the fields marked with a star define class' uniqueness.
    There are also collections that contain these classes, and they are strongly
    typed. ExamCollection and RequirementColl ection. The collections check the
    items for uniqueness and validity before they add them.
    Now, when user asks us for an item from the collection, we cannot really
    give him the internal class, because, the user can change the item in a way,
    that will cause duplicates in the collection or render the item invalid.
    Thus, we have to clone the item, and return the clone. After the changes to
    the cloned item were made, we have to try to add it to the collection. If
    the item with the composite key already exists, we need to update it. If it
    doesn't we need to add it to the collection. And by the way, we also have to
    make sure that the item we are about to add is valid; that is why Exam and
    Requirement have function Valid() which returns a state of validness of the
    item.

    Every exam can be linked to multiple requirements, and one requirement can
    have multiple exams. The best way to maintain the links is to create a
    LinkManager class which will take care of the problem. But, since real
    addresses of the objects are not available, we need to store the keys of the
    related objects. So, every time I want to know which requirements reference
    a specific exam, I need to get exam's key, ask LinkManager to find all the
    requirements' keys that are related to the exam's key. Then ask the
    requirement collection to return clones of all the requirements with the
    following keys.
    I could have used a Singleton that will maintain all the relations, but then
    I will allow cloned items, that do not belong to the collections create
    links, which is a logical error. (All collections contain valid and unique
    items. Everything was checked during adding.). Exams might reference
    requirements that are outside of requirementColl ection, and that is not
    acceptable.

    I described my solution to the problem. My concern is the penalty for
    cloning items and also all the iteration times (every time we iterate
    through the collection, we need to clone items that are being enumerated.
    Otherwise, foreach loop will be able to change it.)

    I would love to hear some ideas of yours on this design issue. Any
    suggestions or critique is welcome.

    Take care,
    Sasha




  • Jeff Clark

    #2
    Re: Best practices question.

    Sasha,
    This is very similar to aa common database example when you have a
    many-to-many relationship. In a database you would create a third table
    (i.e. ExamRequirement s) that would hold foreign keys to the other two tables
    (ExamID, ExamVersion, RequirementVers ion, RequirementMode lNumber). From that
    you could query (or filter with ADO) to get the list of Requirements linnked
    to an Exam, or Exams linked to a Requirement.

    However, since you are not using a database, just in-memory classes, it
    would be a little more difficult, but not impossible.
    I would still follow the database template and create an ExamRequirement s
    class (My guess is that this is the LinkManager you were refering to). This
    would be in charge of making sure that items were not illegally added.

    You could also move to an ADO model. This would be a memory hog. But most of
    the low level functionality would already be there.

    As for your question about penalty for cloning, with these small classes,
    you won't see anything causing issues (unless the name string becoomes
    huge). The main obsticle is the hash table lookups you would need in the
    LinkManager/ExamRequirement s. If the lookup is only one way (Get the
    requirements for this exam) then it won't be too bad. But is you need a two
    way lookup (or Get the exams for this requirement) then it would get a bit
    nasty.

    Hope this answers some of your questions.
    JPC


    "Sasha" <osydorukzzzz@h otmail.com> wrote in message
    news:eWVOru$gDH A.3144@tk2msftn gp13.phx.gbl...[color=blue]
    > Hi everybody,
    >
    > I would like to hear your thoughts on the following problem.
    >
    > We have the following classes.
    >
    > Class Exam
    > int ID*
    > int Version*
    > string Name
    >
    > Class Requirement
    > Int version*
    > String name
    > Int modelnumber*
    >
    >
    > In every class the fields marked with a star define class' uniqueness.
    > There are also collections that contain these classes, and they are[/color]
    strongly[color=blue]
    > typed. ExamCollection and RequirementColl ection. The collections check the
    > items for uniqueness and validity before they add them.
    > Now, when user asks us for an item from the collection, we cannot really
    > give him the internal class, because, the user can change the item in a[/color]
    way,[color=blue]
    > that will cause duplicates in the collection or render the item invalid.
    > Thus, we have to clone the item, and return the clone. After the changes[/color]
    to[color=blue]
    > the cloned item were made, we have to try to add it to the collection. If
    > the item with the composite key already exists, we need to update it. If[/color]
    it[color=blue]
    > doesn't we need to add it to the collection. And by the way, we also have[/color]
    to[color=blue]
    > make sure that the item we are about to add is valid; that is why Exam and
    > Requirement have function Valid() which returns a state of validness of[/color]
    the[color=blue]
    > item.
    >
    > Every exam can be linked to multiple requirements, and one requirement can
    > have multiple exams. The best way to maintain the links is to create a
    > LinkManager class which will take care of the problem. But, since real
    > addresses of the objects are not available, we need to store the keys of[/color]
    the[color=blue]
    > related objects. So, every time I want to know which requirements[/color]
    reference[color=blue]
    > a specific exam, I need to get exam's key, ask LinkManager to find all the
    > requirements' keys that are related to the exam's key. Then ask the
    > requirement collection to return clones of all the requirements with the
    > following keys.
    > I could have used a Singleton that will maintain all the relations, but[/color]
    then[color=blue]
    > I will allow cloned items, that do not belong to the collections create
    > links, which is a logical error. (All collections contain valid and unique
    > items. Everything was checked during adding.). Exams might reference
    > requirements that are outside of requirementColl ection, and that is not
    > acceptable.
    >
    > I described my solution to the problem. My concern is the penalty for
    > cloning items and also all the iteration times (every time we iterate
    > through the collection, we need to clone items that are being enumerated.
    > Otherwise, foreach loop will be able to change it.)
    >
    > I would love to hear some ideas of yours on this design issue. Any
    > suggestions or critique is welcome.
    >
    > Take care,
    > Sasha
    >
    >
    >
    >[/color]


    Comment

    • Mike Maddux

      #3
      Re: Best practices question.

      I don't understand why you don't just make the fields that you want unchanged
      private and either const or readonly.

      Best,

      Mike Maddux

      In article <eWVOru$gDHA.31 44@tk2msftngp13 .phx.gbl>, Sasha says...[color=blue]
      >
      >Hi everybody,
      >
      >I would like to hear your thoughts on the following problem.
      >
      >We have the following classes.
      >
      >Class Exam
      >int ID*
      >int Version*
      >string Name
      >
      >Class Requirement
      >Int version*
      >String name
      >Int modelnumber*
      >
      >
      >In every class the fields marked with a star define class' uniqueness.
      >There are also collections that contain these classes, and they are strongly
      >typed. ExamCollection and RequirementColl ection. The collections check the
      >items for uniqueness and validity before they add them.
      >Now, when user asks us for an item from the collection, we cannot really
      >give him the internal class, because, the user can change the item in a way,
      >that will cause duplicates in the collection or render the item invalid.
      >Thus, we have to clone the item, and return the clone. After the changes to
      >the cloned item were made, we have to try to add it to the collection. If
      >the item with the composite key already exists, we need to update it. If it
      >doesn't we need to add it to the collection. And by the way, we also have to
      >make sure that the item we are about to add is valid; that is why Exam and
      >Requirement have function Valid() which returns a state of validness of the
      >item.
      >
      >Every exam can be linked to multiple requirements, and one requirement can
      >have multiple exams. The best way to maintain the links is to create a
      >LinkManager class which will take care of the problem. But, since real
      >addresses of the objects are not available, we need to store the keys of the
      >related objects. So, every time I want to know which requirements reference
      >a specific exam, I need to get exam's key, ask LinkManager to find all the
      >requirements ' keys that are related to the exam's key. Then ask the
      >requirement collection to return clones of all the requirements with the
      >following keys.
      >I could have used a Singleton that will maintain all the relations, but then
      >I will allow cloned items, that do not belong to the collections create
      >links, which is a logical error. (All collections contain valid and unique
      >items. Everything was checked during adding.). Exams might reference
      >requirements that are outside of requirementColl ection, and that is not
      >acceptable.
      >
      >I described my solution to the problem. My concern is the penalty for
      >cloning items and also all the iteration times (every time we iterate
      >through the collection, we need to clone items that are being enumerated.
      >Otherwise, foreach loop will be able to change it.)
      >
      >I would love to hear some ideas of yours on this design issue. Any
      >suggestions or critique is welcome.
      >
      >Take care,
      >Sasha
      >
      >
      >
      >[/color]

      Comment

      • Sasha

        #4
        Re: Best practices question.

        My concern was that if I expose the data in the collection to the client,
        he/she will be able to change it in illegal ways (For example: assign Name
        field an empty string. Or change the Id and Version combination to the one
        that is already in the collection.).

        But now, after your suggestion... If I make exam without a default
        constructor, in other words, exam has to be given his id and version in the
        constructor, and these fields will be read-only, that might eliminate the
        issue of violating uniqueness of the data in the collection. As far as
        assigning illegal values to other properties, I can throw exceptions from
        the get part of the properties if something is wrong with value.

        Is that what you mean?

        Sasha



        "Mike Maddux" <mike_maddux@ro cketmail.com> wrote in message
        news:bl1gf10122 6@drn.newsguy.c om...[color=blue]
        > I don't understand why you don't just make the fields that you want[/color]
        unchanged[color=blue]
        > private and either const or readonly.
        >
        > Best,
        >
        > Mike Maddux
        >
        > In article <eWVOru$gDHA.31 44@tk2msftngp13 .phx.gbl>, Sasha says...[color=green]
        > >
        > >Hi everybody,
        > >
        > >I would like to hear your thoughts on the following problem.
        > >
        > >We have the following classes.
        > >
        > >Class Exam
        > >int ID*
        > >int Version*
        > >string Name
        > >
        > >Class Requirement
        > >Int version*
        > >String name
        > >Int modelnumber*
        > >
        > >
        > >In every class the fields marked with a star define class' uniqueness.
        > >There are also collections that contain these classes, and they are[/color][/color]
        strongly[color=blue][color=green]
        > >typed. ExamCollection and RequirementColl ection. The collections check[/color][/color]
        the[color=blue][color=green]
        > >items for uniqueness and validity before they add them.
        > >Now, when user asks us for an item from the collection, we cannot really
        > >give him the internal class, because, the user can change the item in a[/color][/color]
        way,[color=blue][color=green]
        > >that will cause duplicates in the collection or render the item invalid.
        > >Thus, we have to clone the item, and return the clone. After the changes[/color][/color]
        to[color=blue][color=green]
        > >the cloned item were made, we have to try to add it to the collection. If
        > >the item with the composite key already exists, we need to update it. If[/color][/color]
        it[color=blue][color=green]
        > >doesn't we need to add it to the collection. And by the way, we also have[/color][/color]
        to[color=blue][color=green]
        > >make sure that the item we are about to add is valid; that is why Exam[/color][/color]
        and[color=blue][color=green]
        > >Requirement have function Valid() which returns a state of validness of[/color][/color]
        the[color=blue][color=green]
        > >item.
        > >
        > >Every exam can be linked to multiple requirements, and one requirement[/color][/color]
        can[color=blue][color=green]
        > >have multiple exams. The best way to maintain the links is to create a
        > >LinkManager class which will take care of the problem. But, since real
        > >addresses of the objects are not available, we need to store the keys of[/color][/color]
        the[color=blue][color=green]
        > >related objects. So, every time I want to know which requirements[/color][/color]
        reference[color=blue][color=green]
        > >a specific exam, I need to get exam's key, ask LinkManager to find all[/color][/color]
        the[color=blue][color=green]
        > >requirements ' keys that are related to the exam's key. Then ask the
        > >requirement collection to return clones of all the requirements with the
        > >following keys.
        > >I could have used a Singleton that will maintain all the relations, but[/color][/color]
        then[color=blue][color=green]
        > >I will allow cloned items, that do not belong to the collections create
        > >links, which is a logical error. (All collections contain valid and[/color][/color]
        unique[color=blue][color=green]
        > >items. Everything was checked during adding.). Exams might reference
        > >requirements that are outside of requirementColl ection, and that is not
        > >acceptable.
        > >
        > >I described my solution to the problem. My concern is the penalty for
        > >cloning items and also all the iteration times (every time we iterate
        > >through the collection, we need to clone items that are being enumerated.
        > >Otherwise, foreach loop will be able to change it.)
        > >
        > >I would love to hear some ideas of yours on this design issue. Any
        > >suggestions or critique is welcome.
        > >
        > >Take care,
        > >Sasha
        > >
        > >
        > >
        > >[/color]
        >[/color]


        Comment

        Working...