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.

1 Answer 1


I would make a DSL describing your questions like this:

Question #11:
  Caption: just question
  Type: free text
  Next question: <<C#CODE
    return #12;

Each of the questions can be stored in an FS like a file or in a database like a text string.

Your software could determine the first questions in a group, read it from DB or FS, present it to user depending on Type and other parameters, then execute a script, located in a Next question field to understand where to go next.

DSL should be extensible. Type could be an arbitrary string and your software may (or may not) convert it into enum or whatever to pass to a question asker factory, that will create appropriate question presenter and other objects, needed to serve the question.

Script is written here in a pseudocode, you could design it like a function, for example, which is passed some domain objects to perform high-level tasks (complex computations, dialog showing functions etc). It should be kept simple.

Answers could be also kept in a table like (question_id, user_id, answer_data).

Will it suit you?

  • Almost. For the part with storing our question tree to disk, I guess we would be fine to store possible answers as id reference and next questions as key-value-entries (I'm a little uncomfortable about loading and executing C# code from disk)? I like the idea of creating a DSL here. We will try this eventually ;) May 29, 2016 at 18:20
  • @FranzWimmer That's really scarry to embed C# code into your system. But you can try to embed JS sandbox instead: stackoverflow.com/questions/172753/… May 31, 2016 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.