views:

101

answers:

1

I have a scenario which is not easy to explain, but I am sure it is a very common problem. I will try my best to illustrate the problem.

Lets say we have a Survey application which allows us to create surveys. Each survey has its own structure (contains questions, questions are grouped and ordered, etc.). Those surveys are managed by an administrator and referred as master survey templates. Now the user can select one of those master surveys, make some customizations and conduct the survey on some persons.

So, basically we have surveys which all share the same structure (collections, properties, etc) but the data can be different.

How do you model the DB?

My idea would be to store all in one table and create a column which separates templates from conducted ones.

tbl_Survey (id, name, conducted_on)

How do you model your classes?

My idea would be:

Survey {
  Name
  Questions
}

ConductedSurvey : Survey {
  //gets the master according to the name
  GetMaster()
}

Important: A Survey has a lot of relationships to other classes. If we decide to subclass. Should all of them be subclassed (cause we would copy the data from master for each object)?

+3  A: 

So, basically we have surveys which all share the same structure (collections, properties, etc) but the data can be different.... My idea would be to store all in one table and create a column which separates templates from conducted ones.

Okay, what you're talking about is a prototype. The "template" survey is a prototype. Clearly, if a prototype has exactly the same structure as an instance based on the prototype, if would be foolish and wasteful to create entirely different tables for the same structure -- all the more so when you realize that that means any change in that structure has to mirrored in both sets.

Would I add a column to distinguish a prototype/template from a conducted survey? No, probably not. Instead I'd add another table, with a one-to-one relation to the root survey table. In this table, I'd add the little bit of metadata that does distinguish a prototype from a non-prototype.

For three reasons: 1) in any reasonable system, the prototypes will be a small minority of total surveys. 2) I'd often want to list all prototypes, in e.g. a "create new survey wizard" that lists a choice of protoypes on which to base the new survey. And 3) to hold that little bit of extra data:

create table survey_prototype (
    id int not null primary key,
    survey_id references survey(id) -- the regular survey table
    wizard_description varchar(80)
    . . . .
);

Now, I imagine that survey also has a description, but that for a prototype, that description is something like "REPLACE ME THIS IS THE DESCRIPTION THE USER WILL SEE" but that wizard_description is something like "A prototype political poll".

Now, since any lookup of a prototype/template has no chance of returning a conducted survey (since n o conducted survey joins to survey_prototype), your getMaster looks (conceptually, presumably you use an ORM) like this:

ConductedSurvey : Survey {
  //gets the master according to the name
  GetMaster() { "select * from survey_prototype join survey..."
}

Important: A Survey has a lot of relationships to other classes. If we decide to subclass. Should all of them be subclassed (cause we would copy the data from master for each object)?

You are correct: for any prototype you retrieve through your ORM, you'll have to deep copy it to save a new survey rather than overwriting the prototype. Since you have to do the deep copy anyway, in the deep copy you can instead of coying the base class that prototype uses, make a subclass copy.

Of course you'll have to make that decision at each level of the hierarchy; it would be nice to encapsulate each transformation policy for a deep copy transform in one class. Visitor Pattern will do this, as it has one overloaded visit function per (base) class of your types: so (at least) visitSurvey, visitQuestion, visitAnswer.

Since you'll be dealing with a tree (rooted at survey, with children questions and grandchild answers,i.e, Composite Pattern), I suggest you use the Visitor Pattern in your copy/transformer. Since your classes are relatively stable, visitor will work well. And it allows you to have multiple different concrete visitors, one for each type of transformation (and when it comes time to display or score a survey, you can write a visitor for that too -- so you'll get that functionality almost "for free" once you set up Visitor Pattern).

To handle subclassing in the database, you can use any one of the common nhibernate patterns for this; that way, once you've visited and transformed, you'll have a new non-prototype survey tree that can be saved to the database 'automagically by nhibernate.

So to sum up: survey_prototype table, get a prototype, nhibernate will retrieve the whole tree when you ask for the root at survey_prototype, visit that tree with a deep copying transformer which returns the copied root, let the user write on that, save the copied root and let nhibernate recursively save every node of the tree. When the user needs to see the non-prototype survey, pull the root from survey with nhibernate, use the display visitor to display it, etc.

tpdi
wow!! This is one of the best answers I have ever get on SOF! Thanks a ton for it. I highly appreciate that you have taken so much time to answer it.It helps me a lot when I know what patterns to use, cause its easier to do research then.I havent fully understand the visitor pattern in this case, but will definitely take a deeper look.One last thing: In my case I will have a Survey which will derive from SurveyPrototype. Now all related classes are always tied to a specific survey. In that case I do not need to create a prototype for all related classes, right?
Michal
E.g. Each question will be copied for each conducted survey but the question itself references a specific survey. Do you understand what I mean :) sorry my english
Michal
I don't think survey should derive from surveyprototype (if anything it would be the other way around). I'd probably say a surveyprototype has-a survey. And right, you just have the prototype for the root of the composite, not every node.
tpdi
To or second comment: yeah, the question's parent-ref is to the copy, if you need a parent-ref at. (Whether you need a parent-ref is another question). But you'll write for each class a copy constructor, which will handle the actual copying.
tpdi
I understand, makes sense. You've been a great help! People like you make this community what it is. High quality! Greetings from bangkok
Michal