Code Abstraction

3

I am creating a question and answer project, more to consolidate my knowledge and acquire others. But in developing it I came up with an issue that I am thinking of which of the options would be best to create a model and consequently a table clean and easy to understand.

What I have is the following question: Which of the options (among which I thought would be good to do) is the best?

  • I put in my question model an attribute called Options> with all the options that the question brings (a, b, c, d, e) and another one called Answer and would put the correct answer to the question; or
  • Put four attributes ( Option1 , Option2 , Option3 , Option4 and Option5 ) and another called Answer with the correct answer.

In particular, I think the second option would be the best, so that by the time I answer the question, I can pull them out of the bank and put in radio buttons and make if 's to check which user responded and check whether that option is correct or not.

Does anyone have any other ideas that are better?

    
asked by anonymous 10.08.2014 / 03:38

2 answers

3

I would do the following:

public class Pergunta {
    [Key]
    public Guid PerguntaId { get; set; }
    [ForeignKey("OpcoesResposta")]
    public Guid RespostaCorretaId { get; set; }

    [Required]
    public String TextoPergunta { get; set; }

    public virtual OpcoesResposta RespostaCorreta { get; set; }

    public virtual ICollection<OpcoesResposta> OpcoesRespostas { get; set; }
}

public class OpcoesResposta {
    [Key]
    public Guid OpcoesRespostaId { get; set; }

    [Required]
    public String TextoResposta { get; set; }
}

Go for Alternative 1, with some nuances. You should put something like this in the View:

foreach (var opcao in OpcoesRespostas) {
    @Html.RadioGroup("RespostaEscolhida", opcao.TextoResposta, opcao.OpcoesRespostaId)
}

And no Controller:

public ActionResult Acao(String RespostaEscolhida) {
    ...
}

The value selected on screen will be filled within RespostaEscolhida .

EDIT

Assuming that I'm actually getting a questionnaire coming from View , I'd make a ViewModel where I have a set of questions in it:

public class QuestionarioViewModel {
    public ICollection<Pergunta> Perguntas { get; set; }
}

The Controller would have a Action to initialize the questions:

public ActionResult Questionario() {

    var questionario = new QuestionarioViewModel {
        Perguntas = context.Perguntas.Take(10).ToList();
    }

    return View(questionario);
}

The View would have something like this:

@model SeuProjeto.ViewModels.QuestionarioViewModel

foreach (var pergunta in Model.Perguntas) {
    ...
}

And, finally, Controller would receive:

[HttpPost]
public ActionResult Questionario(QuestionarioViewModel questionario) {
    // Coloque aqui a regra de negócio, obtendo o resultado dentro de questionario.Perguntas
}
    
10.08.2014 / 04:02
2

I'm going to contribute with an example of my own, I would basically: A relationship from 1 to N (1 for many), between Questions and QuestionsItems , where they would be registered to Question and then the Items of that question. At the end in the field of the Questions table has a PerguntaItemId field that would be the correct answer to the question, code coming from the relationship table, in this case, are processes in which the first registers the questions and their response items and in the end asks the question which answer is correct, by entering the code in the PerguntaItemId field.

Tables:

Models:

Code:

public partial class Perguntas
{
    public Perguntas()
    {
        this.PerguntasItens = new HashSet<PerguntasItens>();
    }
    public int PerguntaId { get; set; }
    public string Descricao { get; set; }
    public int PerguntaItemId { get; set; }
    public virtual ICollection<PerguntasItens> PerguntasItens { get; set; }
}    
public partial class PerguntasItens
{
    public int PerguntaItemId { get; set; }
    public int PerguntaId { get; set; }
    public string Resposta { get; set; }
    public virtual Perguntas Perguntas { get; set; }
}
public partial class DbPerguntas : DbContext
{
    public DbPerguntas()
        : base("name=DbPerguntas") { }
    public DbSet<Perguntas> Perguntas { get; set; }
    public DbSet<PerguntasItens> PerguntasItens { get; set; }
}
    
11.08.2014 / 14:38