[Zope3-dev] ZCML alternative

Jeff Kowalczyk jtk@adelphia.net
Sun, 2 Jun 2002 09:47:30 -0400


I've been wondering about ZCML myself. I think it has a place as a
component configuration mechanism, but will be cumbersome to configure
individual component slots with XML syntax in an external file. The
metadata problem was a plague on an earlier successful component
archicture, COM. Microsoft has gone in a different direction with .NET,
and solved the problem nicely. Python eschews header files, but with
ZCML or other external metadata, they return. I'd like us to consider
the .NET solution. I'll start off with a couple of excerpts:

--( A programmers Introduction to C#, Eric Gunnerson )---
"To Transform a class into a component, some additional information is
often required, such as how to persist a class to disk or how
transactions should be handled. The traditional approach is to write the
information in a separate file and then combine it with the source code
to create a component.

The problem with this approach is that information is duplicated in many
places. It's cumbersome and error prone, and it means you don't have the
whole component unless you have both files. (Anyone who has ever tried
to do COM programming without a typelib should understand the problem
with this.)

The .NET runtime supports custom attributes (known simply as attributes
in C#), which are a way to place descriptive information in the metadata
along with an object, and then retrieve the data at a later time.
Attributes provide a general mechanism for doing this, and they are used
heavily throughout the runtime to store information that modifies how
the runtime use the class. Attributes are fully extensible, and this
allows programmers to define attributes and use them."
---------------------

One of the best everyday examples is decorating the slots of a class
with XML persistence attributes, and then having single-call XML
persistence forever tailored to your class.
http://www.fawcette.com/archives/premier/mgznarch/xml/2001/09sep01/dw010
9/dw0109.asp
http://www.fawcette.com/xmlmag/2002_05/online/xml_dwahlin_05_20_02/

public class HelloWorld {
	string _hello;
	[XmlAttribute]
	public string Greeting {
		get {	return _hello; }
		set {	_hello = value;}
	}
}
The serialized structure would now look like the following:

<?xml version="1.0"?>
<HelloWorld Greeting="Hi There!" />

----------------------

Here's an excerpt from a book that goes into some detail on attributes:
http://www.oreillynet.com/pub/a/dotnet/excerpt/prog_csharp_ch18/

----------------------

I'd like to offer a pretty radical suggestion: Put the Zope component
metadata attributes in prefix slots, right in the python class source.
Have some defaults that make sense for vanilla python classes, so they
can be used securely without modification. Use ZCML for configuration
where attributes in the source are not desirable. Use handlers for the
attributes that Zope is interested in, and allow them to be overridden
or extended by component developers. Zope components will then be
optionally-annotated vanilla python classes, optional custom-attribute
handler classes, and optional multi-component configuration metadata in
ZCML.

How to approach this? Request a context-dependent delimiter suitable for
attributes be added to the python comment syntax. An example would be
[attribute(params)] specifically prefixing a class, method or variable
definition.

Until the minor (attribute parsed as comment) addition makes its way
into Python proper, use the fact that Zope is in control of its python
component compilation, place a preprocessor in there to parse and strip
metadata prior to python compilation. After the preprocessor is no
longer needed, parse the metadata post-compilation. Python itself
doesn't need to know anything about the attributes, only ignore them.

ZCML is very useful to configure the larger picture at deployment time.
ASP.NET applications have something similar called "Web.config", for
example.

I think that running Zope3 on a presumptive Python.NET hosted on Mono
(http://go-mono.org) will eventually be a great deployment target.
Python.NET would almost certainly host attributes. I'd like to see Zope3
be as ready for that as possible.

I've been using C# for 18 months now, and the declarative attributes
work, IMHO. They reduce configuration markup, code duplication and
maintenance to a minimum, at the expense of a relatively benign syntax
extension prefixing language elements. You define or override them as
necessary in the source, config file, or server configurations.