I’m creating a framework for questionnaires.
The questionnaire has several questions. My situation is looking for a class called Question, which supports any answers as you want.
I mean, some questions require just one answer, others two, anothers needs strings, ints, double or any new struct what the developer has builded (imagine that the developer is creating a math problem where uses Fraction struct as answer, for example).
In other words, I need to support any data type or quantity of answers.
So I was thinking about creating an abstract class called Question, where this will contain a Dictionary of responses.
public abstract class Question
{
protected Question(string questionText)
{
this.QuestionText = questionText;
this.Responses = new Dictionary<string, object>();
}
public string QuestionText
{
get;
set;
}
public IDictionary<string, object> Responses { get; protected set; }
}
For example, if I create a new Question, this will be the demo.
public sealed class Question1 : Question
{
public Question1(string questionText)
: base(questionText)
{
}
public int? Response1
{
get
{
int? value = null;
if (this.Responses.ContainsKey("Response1"))
value = this.Responses["Response1"] as int?;
return value;
}
set
{
this.Responses["Response1"] = value;
}
}
}
What do you think about this idea? My first doubt: is it correct that I included the responses into the class and not another independent class.
Who will be answering these questions? Let’s assume people. Given that, a person will probably be logging in or identifying themselves in some way. That makes me think that the Person model should contain Responses, with a reference to each Question.