Dynamic form creation in asp.net c#

前端 未结 4 662
耶瑟儿~
耶瑟儿~ 2021-02-09 09:38

So, I need some input refactoring an asp.net (c#) application that is basically a framework for creating dynamic forms (any forms). From a high level point of view, there is a t

4条回答
  •  我寻月下人不归
    2021-02-09 10:30

    Since your application seems to have become a big ball of mud, a complete (or an almost complete rewrite) might make sense.

    You should also take into account new technologies like document-oriented databases (couchDB, MongoDB)

    Most of the form definitions could probably fit pretty well in document-oriented databases. For exemple:

    To define a customer form, you could use a document that looks like:

    {Type:"FormDefinition",
     EntityType: "Customer",
     Fields: [
       {FieldName:"CustomerName", 
        FieldType:"String", 
        Validations:[
          {ValidationType:"Required"},
          {ValidationType:"StringLength", Minimum:15, Maximum:50},
        ]},
        ...
       {FieldName:"CustomerType", 
        FieldType:"Dropdown", 
        PossibleValues: ["Standard", "Valued", "Gold"],
        DefaultValue: ["Standard"]
        Validations:[
          {ValidationType:"Required"},
          {
            ValidationType:"Custom", 
            ValidationClass:"MySystem.CustomerName.CustomValidations.CustomerStatus"
          }
        ]},
        ...
     ]
    };
    

    With this kind of document to define your forms, you could easily add forms and validations which are customer specific.

    You could easily add subforms using a fieldtype of SubForm or whatever.

    You could define FieldTypes for all common types of fields like e-mail, phone numbers, address, etc.

    namespace System.CustomerName.CustomValidations {
      class CustomerStatus: IValidator {
    
        private FormContext form;
        private List validationErrors;
    
        CustomerStatus(FormContext fc) {
          this.validationErrors = new List();
          this.form = fc;
        }
    
        public List Validate() {
          if (this.formContext.Fields["CustomerType"] == "Gold" && Int.Parse(this.form.Fields["OrderCount"]) < 10) { 
            this.validationErrors.Add(new ValidationError("A gold customer must have at least 10 orders"))
          }
    
          if (this.formContext.Fields["CustomerType"] == "Valued" && Int.Parse(this.form.Fields["OrderCount"]) < 5) { 
            this.validationErrors.Add(new ValidationError("A valued customer must have at least 5 orders"))
          }
          return this.validationErrors;          
        }
      }
    }
    

    A record of a document with that definition could look like this:

    {Type:"Record",
     EntityType: "Customer",
     Fields: [
       {FieldName:"CustomerName", Value:"ABC Corp.", 
       {FieldName:"CustomerType", Value:"Gold",
       ...
     ]
    };
    

    Sure, this solution is a lot of work, but if/when realized it could be really easy to create/update/customize forms.

提交回复
热议问题