Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think subclassing Question as well as answer is a good plan - get rid of that QuestionType enum.</p> <p>You can then have a MakeAnswer(string) abstract method on Question which encapsulates a lot of logic for you, and can do type conversion etc if necessary.</p> <p>My solution to the generic problem on <a href="https://stackoverflow.com/questions/3232363/questions-with-different-types-of-answer-in-nhibernate/3235230#3235230">Rob's answer</a> would be something like this:</p> <pre><code>public interface IAnswer { Question Question { get; } void Respond(IAnswerFormatter formatter); } </code></pre> <p>Where <code>IAnswerFormatter</code> looks something like this:</p> <pre><code>public interface IAnswerFormatter { void Format(string value); void Format(DateTime value); ... } </code></pre> <p>And DateTimeAnswer would look like this:</p> <pre><code>public class DateTimeAnswer : IAnswer { public DateTimeAnswer(Question question, DateTime value) { Question = question; } public Question Question { get; protected set; } protected DateTime Response {get; set;} public virtual void Respond(IAnswerFormatter formatter) { formatter.Write(Response); } } </code></pre> <p>And DateTimeQuestion might look like:</p> <pre><code>public class DateTimeQuestion : Question { public IAnswer MakeAnswer(string value) { // Ignoring error handling here return new DateTimeAnswer(this, DateTime.Parse(value)); } } </code></pre> <p>This way your answer can hold the value in their own way, and you NH mappings can specify how it looks in the database (I'd recommend mapping using Discriminators) your Question can be responsible for creating answers from generic responses.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload