In our database backed (C# WPF / Entity Framework) application, we want to build a system to enter, store, display and search arbitrary content linked to our main entity. The challenge here is to find a pattern for that.
Data should mostly be entered as Key-Value-Pairs. Starting off with one or more pre-defined questions (e.g. "What type of product is this?"), each data input causes the next question to appear (in this case, for example "Weight of the product?").
If we allowed users to enter Keys and Values as free text, it wouldn't be searchable. So we want to offer two types of questions:
- Questions with pre-defined answers to choose from
- Questions with free text input (mostly used for numerical values)
To put that into a software pattern, we started off with the following interface:
public interface IDecision
{
string Caption { get; }
bool HasSuggestions { get; }
IEnumerable<IDecisionResult> GetSuggestions { get; }
bool HasNextDecisions { get; }
IDecision GetNextDecision(IDecisionResult previousResult);
}
Caption
would be the displayed question, GetSuggestions()
would return possible answers (if it's no free text answer) and GetNextDecision()
would return the next question based on this question's answer.
Our main problems here:
- We need to store the whole "decision tree" (questions, possible answers and their following questions) and display them in our UI eventually. We thought of a manager class - but how should we couple it to our model layer? Should we serialize instances of IDecision directly to database or use an intermediate layer? How should we handle the difference between questions with pre-defined answers and free text questions - with an enum in our interface or by creating different implementations of
IDecision
? We are not bound to the Entity Framework here - we'd be fine with a JSON file containing the tree. - We need to store all answers linked to our main entity. Would an ID column in this table be suitable to link the answer back to the question for search purposes? We thought of giving each question an unique id then.