Jefferson
Jefferson

Reputation: 67

Dynamically add controls in ASP.NET: factory pattern design pattern?

I am trying to understand how I can get this JSON data into a factory pattern design pattern. The JSON data below have two MeetingPollingQuestionType one for LongAnswerText and one for MultipleChoice. From that would I need two class ConcreteStrategy LongAnswerText and MultipleChoice? The MeetingPollingPartsValues are different two. LongAnswerText just has a label control but MultipleChoice has radio controls

Any help would be great, The Goal is to create a MVC form with MVC controls like labels textbox and radio list.

[
    {
        "MeetingPollingQuestionId": 2,
        "MeetingPollingQuestionType": "LongAnswerText",
        "MeetingPollingId": 3,
        "SequenceOrder": 1,
        "MeetingPollingParts": [
            {
                "MeetingPollingPartsId": 2,
                "Type": "Question",
                "MeetingPollingQuestionId": 2,
                "MeetingPollingPartsValues": [
                    {
                        "Type": "label",
                        "QuestionValue": "This is a long question",
                        "FileManagerId": 0,
                        "FileName": null,
                        "FileData": null,
                        "FileType": null
                    }
                ]
            }
        ]
    },
    {
        "MeetingPollingQuestionId": 3,
        "MeetingPollingQuestionType": "MultipleChoice",
        "MeetingPollingId": 3,
        "SequenceOrder": 2,
        "MeetingPollingParts": [
            {
                "MeetingPollingPartsId": 3,
                "Type": "Question",
                "MeetingPollingQuestionId": 3,
                "MeetingPollingPartsValues": [
                    {
                        "Type": "label",
                        "QuestionValue": "this is a multiple choice question",
                        "FileManagerId": 0,
                        "FileName": null,
                        "FileData": null,
                        "FileType": null
                    }
                ]
            },
            {
                "MeetingPollingPartsId": 4,
                "Type": "Image",
                "MeetingPollingQuestionId": 3,
                "MeetingPollingPartsValues": [
                    {
                        "Type": "Image",
                        "QuestionValue": null,
                        "FileManagerId": 14552,
                        "FileName": null,
                        "FileData": null,
                        "FileType": null
                    }
                ]
            },
            {
                "MeetingPollingPartsId": 5,
                "Type": "Answers",
                "MeetingPollingQuestionId": 3,
                "MeetingPollingPartsValues": [
                    {
                        "Type": "radio",
                        "QuestionValue": "Yes",
                        "FileManagerId": 0,
                        "FileName": null,
                        "FileData": null,
                        "FileType": null
                    },
                    {
                        "Type": "radio",
                        "QuestionValue": "No",
                        "FileManagerId": 0,
                        "FileName": null,
                        "FileData": null,
                        "FileType": null
                    },
                    {
                        "Type": "radio",
                        "QuestionValue": "Abstain",
                        "FileManagerId": 0,
                        "FileName": null,
                        "FileData": null,
                        "FileType": null
                    }
                ]
            }
        ]
    }
]

Goal MVC form view

this would be the base for the LongAnswerText.

<div class="form-group">
    @Html.LabelFor(c => c.LongAnswerText)
    @Html.TextBoxFor(c => c.LongAnswerText, new { @class = "form-control" })
</div>

this would be the base for the MultipleChoice

 <div class="form-group">
        @Html.LabelFor(c => c.MultipleChoice)
        @Html.RadioButtonFor(model => model.QuestionValue, item.Value, false)
</div>

App

static void Main(string[] args)
    {
        LongAnswerText LongAnswerTextParts = new LongAnswerText();
        var control = LongAnswerTextParts ()

    }

    interface MeetingQuestionInterface
    {
        string Label(string target, string text);
    }
    
    public class LongAnswerText : MeetingQuestionInterface
    {
        public static string Label(string target, string text)
        {
            return String.Format("<label for='{0}'>{1}</label>", target, text);
        }
    
        public static string TextBox(string target, string text)
        {
            return String.Format("<input  for='{0}'>{1}</input>", target, text);
        }
    
    }

Upvotes: 1

Views: 295

Answers (1)

Luis Lavieri
Luis Lavieri

Reputation: 4129

If I understood your question correctly, you are trying to parse the data into their corresponding classes, so you can use the values accordingly in your MVC application in some sort of OOP way.

Well, here's a start. If you have the json of the data, you can use Newtonsoft.JsonConverter to deserialize it into some classes. A custom JsonConverter will be needed since Newtonsoft does not have enough information to deserialize to an interface:

void Main()
{
    var obj = JsonConvert.DeserializeObject<IMeetingPollingQuestion[]>(
        "JSONSTRING", 
        new MeetingPollingQuestionConverter());
    
    Console.WriteLine(obj);
}

public class MeetingPollingQuestionConverter : JsonConverter
{
  public override bool CanConvert(Type objectType) => objectType == typeof(IMeetingPollingQuestion);

  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  {
        var obj = JObject.Load(reader);

        var type = obj["MeetingPollingQuestionType"].ToObject<MeetingPollingQuestionType>();

        return type == MeetingPollingQuestionType.LongAnswerText ?  
        obj.ToObject<LongAnswerText>() : (object)obj.ToObject<MultipleChoice>();
  }

  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  {
    serializer.Serialize(writer, value);
  }
}

public abstract class IMeetingPollingQuestion {
    public int MeetingPollingQuestionId {get; set;}
    public MeetingPollingQuestionType MeetingPollingQuestionType {get;set;}
    public int MeetingPollingId {get; set;}
    public int SequenceOrder {get; set;}
    public MeetingPollingPart[] MeetingPollingParts {get; set;}
    public virtual string Label(string target, string text) => $"<label for='{target}'>{text}</label>";
    public abstract string Control(string target, string text);
}

public class LongAnswerText : IMeetingPollingQuestion {
    public override string Control(string target, string text) => $"<input  for='{target}'>{text}</input>";
}

public class MultipleChoice : IMeetingPollingQuestion {
    public override string Control(string target, string text) => "...";
}

public class MeetingPollingPart {
    public int MeetingPollingPartsId {get; set;}
    public MeetingPollingPartsType Type {get; set; }
    public int MeetingPollingQuestionId {get; set;} 
    public MeetingPollingPartsValue[] MeetingPollingPartsValues {get; set;}
}

public class MeetingPollingPartsValue {
    public MeetingPollingPartsValueType Type {get; set;}
    public string QuestionValue {get; set;}
    public int FileManagerId {get; set;}
    public string FileName {get; set;}
    public string FileData {get; set;}
    public string FileType {get; set;}
}

public enum MeetingPollingQuestionType {
    LongAnswerText, MultipleChoice
}

public enum MeetingPollingPartsType {
    Question, Image, Answers
}

public enum MeetingPollingPartsValueType {
    label, Image, radio
}

Here is the running example: https://dotnetfiddle.net/ylIOQJ

Upvotes: 1

Related Questions