4

I am writing XSD parser , which will be used to generate asp.net controls on a form according to the parsed XSD .

The XSD is loaded from some location ( for example from DB) to a XsdSchema object , and then using .NET classes (Schema Object Model) to read the elements of that schema to produce list of controls to be rendered on a form .

Which patterns you think are the best to be used in this scenario ?

(Currently I've created classes to represent different kinds of controls -text , date , list and etc. , and my xsd parser class has a method that returns a list of those classes according to the parsed XSD .

Those "Ui Element" classes were created to somehow not to tie the parser to asp.net layer )

I want to write the parser in some smart manner according to some design patterns for a simpler changes in the future .

Thanks.

Alex
  • 417
  • 5
  • 13

1 Answers1

4

TL;DR: Visitor pattern (double dispatch) and Intepreter pattern (recursive function) can be used to translate trees (Composite pattern), in your case, the form elements tree into the UI controls tree. Here is a smart article on that.

When parsing, it can be useful to think in terms of trees, rather then lists (your list of controls will most likely have a parent control, so it's in fact a tree). In a nutshell, a tree is a recursive data structure -- a node with scalar id and a list of children, which point to other nodes, whose children also point to other nodes, and so on.

XML can be seen as a tree serialization (graph, in fact, but tree is a common special case) and so can XSD. So, let's say XSD contains a tree of form elements, which needs to be translated to a tree of UI elements.

This can be done by traversing the form tree -- enumerating the nodes in some order, creating a UI control for each node and building up the tree of UI controls.

Two patterns, which come to mind here, are Visitor (double dispatch) and Intepreter (recursive function). Here is some tutorial -- it's in Java, which is translatable to C#.

Update: A good description of Visitor pattern as applies to parse trees:

How to write the Visitor Pattern for Abstract Syntax Tree in Python?

Each node in your AST would need to implement an accept() method (NOT a visit() method). The method takes, as an argument, a visitor object. In the implementation of this accept() method, you call a visit() method of the visitor object (there will be one for each AST node type; in Java, you’ll use parameter overloading, in Python I suppose you can use different visit_*() methods).

Community
  • 1
  • 1
rns
  • 771
  • 4
  • 9