473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 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


Jul 21 '05 #1
3 1614
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
"Sasha" <os**********@hotmail.com> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
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

Jul 21 '05 #2
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 <eW**************@tk2msftngp13.phx.gbl>, Sasha says...

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


Jul 21 '05 #3
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" <mi*********@rocketmail.com> wrote in message
news:bl*********@drn.newsguy.com...
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 <eW**************@tk2msftngp13.phx.gbl>, Sasha says...

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 stronglytyped. ExamCollection and RequirementCollection. The collections check theitems 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 tothe 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 itdoesn't we need to add it to the collection. And by the way, we also have tomake sure that the item we are about to add is valid; that is why Exam andRequirement have function Valid() which returns a state of validness of theitem.

Every exam can be linked to multiple requirements, and one requirement canhave 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 therelated objects. So, every time I want to know which requirements referencea specific exam, I need to get exam's key, ask LinkManager to find all therequirements' 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 thenI will allow cloned items, that do not belong to the collections create
links, which is a logical error. (All collections contain valid and uniqueitems. 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

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
13
by: john doe | last post by:
A quick question, about so-called 'best practices', I'm interested in which of A/B of the two examples people would choose, and why. public enum MyEnum { Option1 = 0, Option2 = 1, Option3 =...
1
by: Vincent V | last post by:
Hey i am just starting a new project and from the start i want to make sure my app is as Object Orientated as possible I have a couple of questions in relation to this Question 1: Should i...
3
by: Derek Martin | last post by:
Hi list, I have been doing VB.Net for quite a while now and just now getting into the forray of ASP.Net using VS2003. I have created our development website and now we are ready to start putting...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
8
by: SStory | last post by:
When I right a class, I am wondering what are the best practices for error handling? Do I try..catch and trap the error and if so what do I do with it? Because most likely the class user will...
15
by: Andrew Brampton | last post by:
Hi, This may sound a odd question, but I wanted to know how you return a list of data from a function. These are some of the ways I know how, and I was wondering which method you normally use....
10
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
17
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.