2
\$\begingroup\$

I am working project where I need to build ASP.net control based on JSON data. I am using the method below GetMeetingPollingQuestion to create this model.

Any suggestions/comments are greatly appreciated to improve the code further. Thanks.

JSON

[{"MeetingPollingQuestionId":1,"MeetingPollingQuestionType":"LongAnswerText","MeetingPollingId":1,"SequenceOrder":1,"MeetingPollingParts":[{"MeetingPollingPartsId":1,"Type":"Question","MeetingPollingQuestionId":1,"MeetingPollingPartsValues":[{"MeetingPollingPartsValuesId":1,"Type":"label","QuestionValue":"Do you have additional comments or concerns with these changes to the Guidelines?","FileManagerId":0,"FileName":null,"FileData":null,"FileType":null}]}]},{"MeetingPollingQuestionId":12,"MeetingPollingQuestionType":"MultipleChoice","MeetingPollingId":1,"SequenceOrder":2,"MeetingPollingParts":[{"MeetingPollingPartsId":35,"Type":"Question","MeetingPollingQuestionId":12,"MeetingPollingPartsValues":[{"MeetingPollingPartsValuesId":63,"Type":"label","QuestionValue":"Do you approve the following statement to be added for all current rituximab indications in the Guidelines: “An FDA-approved biosimilar is an appropriate substitute for rituximab”?  ","FileManagerId":0,"FileName":null,"FileData":null,"FileType":null}]},{"MeetingPollingPartsId":36,"Type":"Image","MeetingPollingQuestionId":12,"MeetingPollingPartsValues":[{"MeetingPollingPartsValuesId":64,"Type":"FileManagerId","QuestionValue":null,"FileManagerId":14716,"FileName":"B-cell_1.2022_panel vote_Page_02 - Copy.png","FileData":"iVBORw.....","FileType":"image/png"}]},{"MeetingPollingPartsId":37,"Type":"Answers","MeetingPollingQuestionId":12,"MeetingPollingPartsValues":[{"MeetingPollingPartsValuesId":65,"Type":"Answers","QuestionValue":"Yes","FileManagerId":0,"FileName":null,"FileData":null,"FileType":null},{"MeetingPollingPartsValuesId":66,"Type":"Answers","QuestionValue":"No","FileManagerId":0,"FileName":null,"FileData":null,"FileType":null},{"MeetingPollingPartsValuesId":67,"Type":"Answers","QuestionValue":"Abstain","FileManagerId":0,"FileName":null,"FileData":null,"FileType":null}]}]}]

Method

public IEnumerable<MeetingPollingQuestionViewModel> GetMeetingPollingQuestion()
{
    List<MeetingPollingQuestionViewModel> vm = new List<MeetingPollingQuestionViewModel>();
    //JSON Data parse into class Model and set to object ListofMeetingPollingQuestion
    foreach (MeetingPollingQuestion MeetingPollingQuestion in ListofMeetingPollingQuestion)
    {
        int SequenceOrder = MeetingPollingQuestion.SequenceOrder;
        switch (MeetingPollingQuestion.MeetingPollingQuestionType)
        {
           
            case "LongAnswerText":
                MeetingPollingQuestionViewModel LongAnswerText = new MeetingPollingQuestionViewModel();
                LongAnswerText.QuestionType = "LongAnswerText";
                var MeetingPollingParts = MeetingPollingQuestion.MeetingPollingParts;

                var LongAnswerQuestion = MeetingPollingParts.FirstOrDefault(part => part.Type == "Question");
                var labelControl = LongAnswerQuestion.MeetingPollingPartsValues.FirstOrDefault(part => part.Type == "label");
                LongAnswerText.labelControl =  $"{SequenceOrder}. {labelControl.QuestionValue}'";
                LongAnswerText.textboxControl = $"textboxfor_{labelControl.MeetingPollingPartsValuesId}";
                vm.Add(LongAnswerText);
                break;

            case "MultipleChoice":
                MeetingPollingQuestionViewModel MultipleChoice = new MeetingPollingQuestionViewModel();
                MultipleChoice.QuestionType = "MultipleChoice";
                var MultipleChoiceMeetingPollingParts = MeetingPollingQuestion.MeetingPollingParts;

                var MultipleChoiceQuestion = MultipleChoiceMeetingPollingParts.FirstOrDefault(part => part.Type == "Question");
                var MultipleChoicelabelControl = MultipleChoiceQuestion.MeetingPollingPartsValues.FirstOrDefault(part => part.Type == "label");
                MultipleChoice.labelControl = $"{SequenceOrder}. {MultipleChoicelabelControl.QuestionValue}'";

                var MultipleChoiceImage = MultipleChoiceMeetingPollingParts.FirstOrDefault(part => part.Type == "Image");
                var MultipleChoiceImageControl = MultipleChoiceImage.MeetingPollingPartsValues.FirstOrDefault(part => part.Type == "FileManagerId");
                if (MultipleChoiceImageControl.FileManagerId != 0){
                    MultipleChoice.imageSRC = MultipleChoiceImageControl.FileData;
                }
                

                var MultipleChoiceAnswers = MultipleChoiceMeetingPollingParts.FirstOrDefault(part => part.Type == "Answers");
                var MultipleChoiceAnswersControl = MultipleChoiceAnswers.MeetingPollingPartsValues.ToList();

                List<CBRBControl> RadioButtonlist = new List<CBRBControl>();

                foreach (var item in MultipleChoiceAnswersControl)
                {
                    CBRBControl RadioButton = new CBRBControl();
                    RadioButton.Value = item.MeetingPollingPartsValuesId.ToString();
                    RadioButton.Label = item.QuestionValue;
                    RadioButtonlist.Add(RadioButton);
                }
                multipleChoice.RadioButtonName = $"radioList_{MultipleChoiceAnswers.MeetingPollingQuestionId}";
                multipleChoice.RadioButtonList = RadioButtonlist;
                vm.Add(MultipleChoice);
                break;
        }

    }

    return vm;



}

}

Model

public class  MeetingPollingQuestionViewModel
{
    public string QuestionType { get; set; }
    public string labelControl { get; set; }
    public string textboxControl { get; set; }
    public string imageControl { get; set; }
    public byte[] imageSRC { get; set; }
    public string RadioButtonName { get; set; }
    public List<CBRBControl> RadioButtonList { get; set; }
}


public class CBRBControl
{
    public string Label { get; set; }
    public string Value { get; set; }
}
\$\endgroup\$
3
  • \$\begingroup\$ Properties need to be PascalCased. Local variables need to be camelCased. \$\endgroup\$
    – BCdotWEB
    Commented Oct 28, 2022 at 14:08
  • \$\begingroup\$ Are you re-designing your code? I noted your previous, related question with a class LongAnswerText : IMeetingPollingQuestion This is certainly relevant to any answers here. \$\endgroup\$
    – radarbob
    Commented Oct 30, 2022 at 19:10
  • \$\begingroup\$ I changed it to use FirstOrDefault but wanted to see if there is more refactoring that can be done \$\endgroup\$
    – Jefferson
    Commented Oct 30, 2022 at 22:11

1 Answer 1

2
+50
\$\begingroup\$

Gerneral advice

look at this line

List<MeetingPollingQuestionViewModel> vm = new List<MeetingPollingQuestionViewModel>();

this variable name vm showed be plural because it's a list vms .. and you can use the simplified new expression

List<MeetingPollingQuestionViewModel> vms = new();

your foreach loop could be more readable if you use var key word and short variable name

foreach (var Question in ListofMeetingPollingQuestion)

it's more readable than

foreach (MeetingPollingQuestion MeetingPollingQuestion in ListofMeetingPollingQuestion)

Redundancy elimination

Looke at this of code for each case it's nearly identical

foreach (MeetingPollingQuestion MeetingPollingQuestion in ListofMeetingPollingQuestion)
{
    int SequenceOrder = MeetingPollingQuestion.SequenceOrder;
    switch (MeetingPollingQuestion.MeetingPollingQuestionType)
    {
        case "LongAnswerText":
            MeetingPollingQuestionViewModel LongAnswerText = new MeetingPollingQuestionViewModel();
            LongAnswerText.QuestionType = "LongAnswerText";
            var MeetingPollingParts = MeetingPollingQuestion.MeetingPollingParts;

            var LongAnswerQuestion = MeetingPollingParts.FirstOrDefault(part => part.Type == "Question");
            var labelControl = LongAnswerQuestion.MeetingPollingPartsValues.FirstOrDefault(part => part.Type == "label");
            LongAnswerText.labelControl = $"{SequenceOrder}. {labelControl.QuestionValue}'";
            //... code
            vm.Add(LongAnswerText);
            break;

        case "MultipleChoice":
            MeetingPollingQuestionViewModel MultipleChoice = new MeetingPollingQuestionViewModel();
            MultipleChoice.QuestionType = "MultipleChoice";
            var MultipleChoiceMeetingPollingParts = MeetingPollingQuestion.MeetingPollingParts;

            var MultipleChoiceQuestion = MultipleChoiceMeetingPollingParts.FirstOrDefault(part => part.Type == "Question");
            var MultipleChoicelabelControl = MultipleChoiceQuestion.MeetingPollingPartsValues.FirstOrDefault(part => part.Type == "label");
            MultipleChoice.labelControl = $"{SequenceOrder}. {MultipleChoicelabelControl.QuestionValue}'";
            //... code 
            vm.Add(MultipleChoice);
            break;
    }
}

in both cases you create a new view model and change the same property and create same variables with different names and at the end add this view model in your list

foreach (var Question in ListofMeetingPollingQuestion)
{
    int SequenceOrder = Question.SequenceOrder;

    MeetingPollingQuestionViewModel vm = new();
    vm.QuestionType = Question.MeetingPollingQuestionType;
    var MeetingPollingParts = Question.MeetingPollingParts;
    var QuestionObj = MeetingPollingParts.FirstOrDefault(part => part.Type == "Question");
    var label = QuestionObj .MeetingPollingPartsValues.FirstOrDefault(part => part.Type == "label");
    vm.labelControl = $"{SequenceOrder}. {label.QuestionValue}'";

    switch (Question.MeetingPollingQuestionType)
    {
        case "LongAnswerText":
            //... code
            break;

        case "MultipleChoice":
            //... code
            break;
    }

    vms.Add(vm);
}

Bad type

your code use string literals such as ("LongAnswerText", "MultipleChoice", "FileManagerId", "label", "Question" ,... etc) you should avoid this technique because this string's value is not checked at compile time Read more resons whe you should not use magic strings try useing Enums or constant value insted

for example define this enum

public enum QuestionType
{
    LongAnswerText,
    MultipleChoice
}

then change the type MeetingPollingQuestionType to beQuestionType then give your code the compiler check superpower as following

switch (Question.MeetingPollingQuestionType)
{
    case QuestionType.LongAnswerText:
        //... code
        break;

    case QuestionType.MultipleChoice:
        //... code
        break;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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