473,396 Members | 1,849 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,396 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


Nov 20 '05 #1
3 974
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

Nov 20 '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


Nov 20 '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

Nov 20 '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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.