Question about design

  • Thread starter Thread starter Gabriel Lozano-Morán
  • Start date Start date
G

Gabriel Lozano-Morán

Assume that you have a Document class that you want to persist. Which
solution would be better and what are the advantages/disadvantages of each
approach?

Document doc = new Document();

1)
doc.Persist(PersistType.CsvDelimited);

2)
CsvPersister persister = new CsvPersister(Document);
persister.Persist();

I would go for solution 1 because the the Persist() operation is a
responsibility of the Document. But several of my coworkers will go for
solution 2 without arguing about it. Personally I find solution 2 a good
solution if you want to use the same persister for several different types
like the XmlSerializer class.

If you go for solution 2 where do you draw the line? I mean you could then
write a class for each public operation of the document?

I appreciate any feedback

Gabriel
 
Also how about the following approach:

Document doc = new Document();
doc.Persist(new CsvPersister())

and in the Persist of the Document:

public void Persist(IPersister persister)
{
persister.Persist(this);
}
 
Hi Gabriel

I think this crops up in many similar scenarios, for example users and
roles. Does a User object maintain a list of roles to which it belongs, or
does a Role object maintain a list of Users who have that role, or both.

In your scenario, I would say that if your Document object implements some
common (to your application) interface, such as IPersistable, then solution
2 would work well. Your CsvPersister would not need to understand how to
persist a document, only how to persist an object that implements
IPersistable. It also implies that you have a common format that you would
use for a variety of persistable objects.

On the other hand, if you only have one or two objects that will be
persisted, and each in a different way, then I would promote solution 1.

HTH

Charles
 
Back
Top