H
havremunken
Hi there,
A project I am working on uses an XML-file to store the objects from a
session, plus some metadata and definitions for this session. A brief
explanation follows;
The main class type is the Entry. Entry objects have certain
attributes, methods and properties, and really make up the core of the
application.
There is another class, the RatingCategory, which has some settings,
and a list of Rating objects. These are used for creating user ratings
for each Entry - each Entry object can (but doesn't have to) be rated
in each of the the rating types. An Entry will typically contain a
collection of EntryRating objects, where each EntryRating has a
pointer to the corresponding Rating object, plus the score in that
particular category.
There are also other "top level classes" along with RatingCategory
etc. that are defined in the file.
Now, I want to make a "superclass" which is the DataFile (inventive
name, I know) class. This will be the in-memory representation of the
XML file containing the RatingCategory, Rating, Entry etc. objects. I
want the DataFile class to have methods and properties so I can behave
such as this:
DataFile df = DataFile.Load( string fileName );
df.Entries.Add( new Entry() );
df.Save();
And that is really no problem at all.
However, say that each Entry object will have a Rating property (get
only). This will check all the EntryRating objects, calculate their
values using RatingCategory and Rating objects, and report a 0-100
type integer. However, to be able to do this, the Entry object will
need knowledge of the list of RatingCategory objects that the DataFile
keeps; The same also holds true for other cross-relations inside the
DataFile - the different object types may need to know about each
other (mainly Entry objects needing to know of the others), however,
isn't it clumsy for each Entry object to keep a pointer to the
DataFile it belongs to?
I want to be able to unit test this properly, both on the level of the
DataFile (create a DataFile, save it, load it back, compare the
contents) and the individual objects; I also want the option to keep
several DataFile objects in memory at once (MDI interface), so static
pointers won't do any good either.
What is the correct design decision to make here? Would it be to have
a method in the DataFile/RatingCategory objects like this;
int GetEntryRating(Entry entryToRate);
Or does that violate the single responsibility thingy?
Any ideas here would be welcomed!
Thanks in advance,
Rune
A project I am working on uses an XML-file to store the objects from a
session, plus some metadata and definitions for this session. A brief
explanation follows;
The main class type is the Entry. Entry objects have certain
attributes, methods and properties, and really make up the core of the
application.
There is another class, the RatingCategory, which has some settings,
and a list of Rating objects. These are used for creating user ratings
for each Entry - each Entry object can (but doesn't have to) be rated
in each of the the rating types. An Entry will typically contain a
collection of EntryRating objects, where each EntryRating has a
pointer to the corresponding Rating object, plus the score in that
particular category.
There are also other "top level classes" along with RatingCategory
etc. that are defined in the file.
Now, I want to make a "superclass" which is the DataFile (inventive
name, I know) class. This will be the in-memory representation of the
XML file containing the RatingCategory, Rating, Entry etc. objects. I
want the DataFile class to have methods and properties so I can behave
such as this:
DataFile df = DataFile.Load( string fileName );
df.Entries.Add( new Entry() );
df.Save();
And that is really no problem at all.
However, say that each Entry object will have a Rating property (get
only). This will check all the EntryRating objects, calculate their
values using RatingCategory and Rating objects, and report a 0-100
type integer. However, to be able to do this, the Entry object will
need knowledge of the list of RatingCategory objects that the DataFile
keeps; The same also holds true for other cross-relations inside the
DataFile - the different object types may need to know about each
other (mainly Entry objects needing to know of the others), however,
isn't it clumsy for each Entry object to keep a pointer to the
DataFile it belongs to?
I want to be able to unit test this properly, both on the level of the
DataFile (create a DataFile, save it, load it back, compare the
contents) and the individual objects; I also want the option to keep
several DataFile objects in memory at once (MDI interface), so static
pointers won't do any good either.
What is the correct design decision to make here? Would it be to have
a method in the DataFile/RatingCategory objects like this;
int GetEntryRating(Entry entryToRate);
Or does that violate the single responsibility thingy?
Any ideas here would be welcomed!
Thanks in advance,
Rune