Intro
 FAQ
 Features
 Limitations
 The basics

 XSD vs Dingo
 Class Diagram
 NUnit Tests
 Java code
 SourceForge

 Plugins
 Custom Builders
 Tutorial
 quicktips

 Samples
   conversion
   dashSchema
   contact
   foobar
   geneology

 Links
  NUnit

Custom Builders

The primary interface that defines a code generator is Builder. The interface is fairly simple and encapsulates the basic functionalities from an outside perspective.

Builder interface
using System;

namespace woolfel.dingo.builder
{
  /// <summary>
  /// Builder need to support some basic functions.
  /// <list type="ol">
  /// <item>return a string representing the type</item>
  /// <item>save the string representing the type</item>
  /// <item>append the generated source to an existing file</item>
  /// </list>
  /// </summary>
  public interface Builder
  {
    /// <summary>
    /// Method returns the source of the class
    /// </summary>
    /// <returns></returns>
    string ToSourceString();
    /// <summary>
    /// Save the generated source to the given file
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    bool SaveSource(string path);
    /// <summary>
    /// Generate the source for an instance of a TypeInfo using a new 
    /// StringBuilder
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    bool GenerateSource(object type);
    /// <summary>
    /// Generate the source for an instance of a TypeInfo with a 
    /// specific StringBuilder
    /// </summary>
    /// <param name="type"></param>
    /// <param name="builder"></param>
    /// <returns></returns>
    bool GenerateSource(object type, System.Text.StringBuilder builder);
    /// <summary>
    /// Method takes one of the info type objects and returns true, if the
    /// builder can handle the type.
    /// </summary>
    /// <param name="infotype"></param>
    /// <returns></returns>
    bool SupportsType(object infotype);
  }
}
					

The basic tasks of a builder is:

  • generate source code
  • save the source to a file
The interface doesn't contain any notion of programming languages or platform. For the first release I've implemented support for C#. Currently, I have no plans of implementing support for VB. Once you've written the custom builder, you can plug it into Dingo. Dingo's configuraiton file contains an entry for the builder <add key="defaultBuilder" value="woolfel.schema.builder.TypeBuilder"/>. If you wish to use your own bulder, simply add your assembly to the configuration file and change the defaultBuilder to the fully qualified name of your custom builder. There are a couple of different options for writing your own builder.

  • Extend TypeBuilder and override the format methods
  • Write a completely new class that implements Builder interface
  • Extend AbstractBuilder and reuse the basic functionality it provides

For more information about implementing a builder, I would recommend reading the source for TypeBuilder. There's quite a bit of comments throughout most of the classes. Here's an example of how TypeBuilder generates the "using" statements for classes.

Builder interface
    /// <summary>
    /// Generate the using declarations to make sure the necessary
    /// namespaces are included in the class.
    /// </summary>
    public void GenerateUsingDeclaration()
    {
      if(this.TYPE.HasUsing())
      {
        IEnumerator en = this.TYPE.GetUsing().GetEnumerator();
        while(en.MoveNext())
        {
          this.BUILDER.Append(Constants.USING + " " + 
            (string)en.Current + 
            Constants.SEMICOLON + Constants.CRLF);
        }
      }
      // now add the namespace of the interfaces
      if(this.TYPE.GetInterfaces().Count > 0)
      {
        IEnumerator en = this.TYPE.GetInterfaces().GetEnumerator();
        while (en.MoveNext())
        {
          TypeInfo ti = (TypeInfo)en.Current;
          this.BUILDER.Append(Constants.USING + " " + 
            ti.GetNamespace() +
            Constants.SEMICOLON + Constants.CRLF);
        }
      }
      this.BUILDER.Append(Constants.CRLF);
    }
					

As a general tip, the Constants class contains most of the reserved key words one would need to generate a class. If there's a specific reserved word that you need, please feel free to send me an email and I will gladly add it.

Updated 9-14-2004 Peter

SourceForge.net Logo
Copyright 2004 Peter Lin