Best practices question.

  • Thread starter Thread starter Sasha
  • Start date Start date
S

Sasha

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 RequirementCollection. 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 requirementCollection, 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
 
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. ExamRequirements) that would hold foreign keys to the other two tables
(ExamID, ExamVersion, RequirementVersion, RequirementModelNumber). 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 ExamRequirements
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/ExamRequirements. 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
 
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
 
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
 
Back
Top