A dynamic form without real OOP or objects?

I use a large project refactor, and I asked this question to confirm / understand the direction I should go, and I think I got the answer that I wanted to not throw away the summer cost of the code. So now the code refactoring task begins. I read the books of Martin Fowler and Martin Pers, and they have a lot of understanding, but I am looking for advice on the ultimate goal, where I want the application to be.

So, to repeat the application a bit, this is a system of dynamic forms, with a lot of validation logic and data logic between fields. The main record that is inserted is the set of form fields that are on the page. Another part of it is the “Actions” that you can do for a person. These "actions" may differ from client to client, and there are hundreds of "actions". There is also talk that we can somehow create an engine that can ultimately use other similar areas where the “person” can be something else (for example, a student or employee). So I want to build something very deactivated. We have one code base, but different databases for different clients. The set of form fields on the page is dynamic, but there is no database — it is translated into a specific database table through stored procedures. Thus, the general set of fields is sent to the stored proc and stored proc, then it decides what to do with the fields (find out which table it should go to). These tables are actually quite static, which means that they are not very dynamic, and there is a certain structure.

What I'm specifically afraid of is how to set up a good way to create a dynamic form control page. It seems that most of the logic will be in the code on the UI / aspx.cs page, because its loading is controlled on the web page. Is there a way I can do this, so it is done in a streamlined way, so the aspx.cs page does not have a length of 5000 lines? I have a "FORM" object, and one of the properties is its "FIELDS". Thus, this object is loaded into the business layer and at the data level, but now at the end of fron it should scroll through FIELDS and display the controls on the page. It may also be useful to somehow manage the placement, but not sure how to get it in this model ....

In addition, from another point of view - how can I “really” get it in an object-oriented structure? Because technically they can create forms of anything. And these form fields can represent any object. So, for example, today they can create a set of form fields that represent the “person” - tomorrow they can create a set of form fields that represent the “furniture”. How can I translate this into a person or piece of furniture (or should I even try?). And I have no control over the form fields, because they can create anything ....

Any thought process would be very helpful - thanks!

+4
source share
3 answers

How can I translate this into a person or a piece of furniture? (or should I even try?)

If you understand correctly, you probably should not try to convert these fields to specific objects, since the nature of your application is so dynamic. If stored procedures are able to determine which combination of fields belongs to which tables, then large.

If you can change the database schema, I would suggest coming up with something much more dynamic. Instead of having a separate table for each type of dynamic object, I would create the following schema:

Object { ID Name ... (clientID, etc.) ... } Property { ID ObjectID Name DBType (int, string, object-id, etc.) FormType ( textbox, checkbox, etc.) [FormValidationRegex] <== optional, could be used by field controls Value } 

If you cannot change the database schema, you can apply the following to the old system using stored procedures and fixed tables:

Then, when you read from a database in a specific object, you can scroll through each of the properties and get the type of the form and simply add the corresponding type of the general form to the page:

 foreach(Property p in Object.Properties) { switch(p.FormType) { case FormType.CheckBox: PageForm.AddField(new CheckboxFormField(p.Name, p.Value)); break; case FormType.Email: PageForm.AddField(new EmailFormField(p.Name, p.Value)); break; case FormType.etc: ... break; } } 

Of course, I threw the PageForm object, as well as the CheckboxFormField and EmailFormField objects. The PageForm object can just be a placeholder, and CheckboxFormField and EmailFormField can be UserControls or ServerControls.

I would not recommend trying to control the placement. Just list each field one at a time vertically. In any case, this is becoming increasingly popular, even with static forms that can fully control the layout. Most registration forms, for example, follow this agreement.

Hope this helps. If I misunderstood your question or if you would like further explanations, let me know.

+2
source

Not sure if I understand the question. But there are two sets of tools suitable for writing common code. These are generics, and this reflection is usually combined.

I don’t think I really understand what you are trying to do, but a method using relfection to identify all the properties of an object might look like this:

 using System.Reflection; (...) public void VisitProperties(object subject) { Type subjectType = subject.GetType(); foreach (PropertyInfo info in subjectType.GetProperties() { object value = info.GetValue(subject, null); Console.WriteLine("The name of the property is " + info.Name); Console.WriteLine("The value is " + value.ToString()); } } 

You can also view a blog post where I discuss the use of attribute attributes in conjunction with reflection. He actually discusses how this can be used to write a common user interface. Not quite what you want, but at least the same principles can be used.

http://codepatrol.wordpress.com/2011/08/19/129/

This means that you can create your own custom attributes or use those that already exist within the .NET framework to describe your types. You can use attributes to specify validation rules, field labels, and even field placement.

 public class Person { [FieldLabel("First name")] [ValidationRules(Rules.NotEmpty | Rules.OnlyCharacters)] [FormColumn(1)] [FormRow(1)] public string FirstName{get;set;} [FieldLabel("Last name")] [ValidationRules(Rules.NotEmpty | Rules.OnlyCharacters)] [FormColumn(2)] [FormRow(1)] public string LastName{get;set;} } 

Then you should use the method described on my blog to identify these attributes and take the appropriate action - for example, putting them in the right row, giving the right label, and so on. I will not suggest how to solve these things, but at least reflection is a great and simple tool for getting descriptive information about an unknown type.

0
source

I found xml invaluable for the same situation. You can plot an object graph in your code to represent the form easily enough. This graphic can be loaded / saved again from db.

You can turn your object graph into xml and use xslt to create html for display. Now you also have the opportunity to configure this conversion for clients / differnetn versions, etc. I also store xml in the database for performance and provide a publish function.

A certain code is required to process incoming data, as you will gain access to an unprocessed request. You need to check the incoming data for what you think has been shown. This prevents people from faking / interfering with your forms.

I hope everything makes sense.

0
source

All Articles