Skip to content

Instantly share code, notes, and snippets.

@troygoode
Created January 21, 2011 18:25
Show Gist options
  • Save troygoode/790135 to your computer and use it in GitHub Desktop.
Save troygoode/790135 to your computer and use it in GitHub Desktop.
Example of versionned survey object.
// survey definitions
public abstract class SurveyDefinition{
public string Id{ get; set; }
public string Survey{ get; set; }
public string Version{ get; set; }
public abstract Survey Generate();
}
public class MySurvey2010 : SurveyDefinition{
public MySurvey2010(){
Id = "550e8400-e29b-41d4-a716-446655440000";
Survey = "My Survey";
Version = "2010";
}
public override Survey Generate(){
var survey = new Survey(this);
survey.Pages.Add("page-1", p=> {
p.Questions.Add(new TextBoxQuestion("page-1-q-1", "How big is this building?"));
p.Questions.Add(new YesNoQuestion("page-1-q-2", "Does it have a door?"));
return p;
});
survey.Pages.Add("page-2", p=> {
p.Questions.Add(new DropDownListQuestion("page-2-q-1", "How big is the building?", new[]{ "Pretty big.", "Way huge." }));
return p;
});
return survey;
}
}
// survey structure
public class Survey{
public Survey(SurveyDefinition definition){
Definition = definition;
Pages = new List<Page>();
}
public SurveyDefinition Definition{ get; private set; }
public IList<Page> Pages{ get; private set; }
}
public class Page{
public string Id{ get; set; }
public IList<IQuestion> Questions{ get; set; }
}
// question implementations
public interface IQuestion{
string Id{ get; }
MvcHtmlString Render(HtmlHelper html); // http://msdn.microsoft.com/en-us/library/system.web.mvc.mvchtmlstring.aspx
}
public abstract class Question : IQuestion{
public Question(string id){
Id = id;
}
public string Id{ get; private set; }
public abstract MvcHtmlString Render(HtmlHelper html);
}
public class TextBoxQuestion : IQuestion{
public TextBoxQuestion(string id, string label) : base(id){
Label = label;
}
public string Label{ get; private set; }
public override MvcHtmlString Render(HtmlHelper html){
return html.TextBox(this.Id, this.Label);
}
}
// controller code
public class SurveyController : Controller{
public ViewResult Page(string pageId){
var survey = new MySurvey2010().Generate();
var page = survey.Pages.Single(p=> p.Id == pageId);
return View(page);
}
}
// view (page.spark)
<strong>Questions for page ${Model.Id}:</strong>
<ul>
<li each="var question in Model.Questions">${question.Render(Html)}</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment