[antlr-interest] StringTemplate/MVC design issue

Harald M. Müller harald_m_mueller at gmx.de
Sun Dec 30 11:13:56 PST 2007


> What we've done so far, is to provide additional methods on 
> our model objects. For example, PackageDefinition class has a 
> toClassName() and a
> toVariableName() method that simply handles the first letter 
> upper/lower.
> The visitor calls these methods to set attributes on 
> templates. But, I think this is the wrong way to do it. With 
> this approach, the model knows too much about the view. If we 
> convert to something other than Java in the future, the 
> uppercase/lowercase first letter, not to mention the other 
> naming conventions may not be necessary and/or may be 
> different. 

We do currently exactly the same (in a quite large code generator emitting
Java and NHibernate hbm.xmls).
I wouldn't change anything in the machinery behind the scenes, but rather do
about the following:
- Let the model not return strings, but an object of some type
"AbstractName"; the concrete object will be returned by a factory, e.g.
"JavaName" or "CSharpName" or "SQLName". These classes (which do not conform
to an interface!)then habe methods that implement the various idioms on how
to convert a generic "package definition's name" to a Java/C#/... class
name, a variable name, a parameter name etc. If you emit code in some other
language, you would have to have other 
- Whatever part of the program selects the templates to be used will also
install the factory.

Whether you then count these classes to be part of the "model" or "view" is
up to you - I would think they are an "intelligent strategy of the view
injected into the model."
(Draw an architecture picture in your documentation where you explain this
:-) !).

Regards
Harald M.

P.S. Similar questions often come up in the GUI MVC context: Models often
cannot be THAT abstract w/ regards to the view - showing a list in a listbox
or as a list of radiobuttons requires, although the representations in
principle have the same power, often a few small different
methods/behaviors. Some sort of injection/factory can help there also.




More information about the antlr-interest mailing list