Code abstraction

Asked

Viewed 190 times

3

I am creating a project of questions and answers, more to consolidate my knowledge and acquire others. But when developing it I arrived at a question that I am thinking about which of the options would be better to create a model and therefore a table clean and easy to understand.

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

  • I put in my question model an attribute called Opcoesresposta with all the options that the question brings (a, b, c, d, e) and another call Answer and put in it the correct answer to the question; or
  • Put four attributes (Opcao1, Opcao2, Option 3, Opcao4 and Opcao5) and another call Answer with the correct answer.

I personally think that the second option would be the best, so that when I answer the question, I can pull them out of the seat and put in radio Buttons and make if's to verify which user responded and check whether that option is correct or not.

Does anyone have any other idea that’s better?

2 answers

3


Here’s what I’d do:

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 in Controller:

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

The selected value will be filled in RespostaEscolhida.


EDIT

Supposing I’m actually getting a questionnaire from View, I would make a ViewModel where I have in it a set of questions:

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);
}

To View would have something like this:

@model SeuProjeto.ViewModels.QuestionarioViewModel

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

And finally, the Controller would receive:

[HttpPost]
public ActionResult Questionario(QuestionarioViewModel questionario) {
    // Coloque aqui a regra de negócio, obtendo o resultado dentro de questionario.Perguntas
}
  • I also thought that way Gypsy, but I thought it would hurt the performance of the system. And only one doubt there in Opcoesresposta, that attribute Guid, I never used him. He’s like a Dictionary ? That is, an array with response options. Because it has that foreach in the view to scan what is in the model.... And another, would have to put some example of code in the controller ?

  • It does not harm so much. The Guid is a special type that generates a unique key and serves as the primary key. Feel free to use int in place if you want. I will improve the example for you.

  • I get it. but I have another question... If I put all the response options in one field, how would I sweep all that content and put in a radiogroup and more, how would I see what selected is the right answer or not ?

  • It would be much more complicated this way: you would have to invent a way to separate the information in order to assemble a RadioGroup valid and assemble the answer. There is no need to use a simpler data structure just to get efficiency.

  • But that way you put it in the answer, it feels right ? You put all the response options in a single field and then assemble the radiogrouplist right ?

  • So actually to "put in a single field" is riding the RadioGroup. What I do in the answer is simply separate the answer so that Razor can write an item from RadioButton by reply. The grouping takes place by the first argument of @Html.RadioGroup() (in the case, RespostaEscolhida).

Show 1 more comment

2

Well I will contribute with a specific example, I would do so basically: A relationship of 1 to N (1 to many), between Questions and Questionsitens, where the Question would be registered and then the Items of this question. At the end of the Questions field is a field PerguntaItemId that would be the correct answer regarding the question, code coming from the relationship table, in this case, are processes in which the first enters the questions and their response items and at the end says the question which answer is the correct one, informing the code in the field PerguntaItemId.

Tables:

inserir a descrição da imagem aqui

Models:

inserir a descrição da imagem aqui

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; }
}
  • 1

    Wow, extremely useful your answer ! Very simple. Looking at this side of you became clearer to me. I was doing a single table and in it has the question and the answer, and the answer would be a IENUMERABLE. Which might not be a good one. Because I need to be able to generate a list that I can put on a radiobuttonlist to choose from among the alternatives, the correct !

  • 1

    Dear @Érikthiago, so it would be very quiet and with Entity a Questionid already brings all the information of that question. I believe that this model will have to serve a lot.

  • And that way I could make a radiobuttonlist without any problem and do the response check ?

  • 1

    Yes!!!!!!!!!!!!!

  • Demoorooou ! Thanks ! I will check the answer by if !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.