Creational Wizard

Often when you add a field or property you need to add code to the class constructor or destructor to initialize or finalize the member. The Creational Wizard is used to centralize this process and define initialization / finalization code while you add the member. Optionally a parameter is added to the constructor parameter list to initialize a member. You may select an existing constructor or create a new one. Similar finalization snippets can be inserted in a new or existing destructor. Both ModelMaker 8 and ModelMaker Code Explorer 4 support this wizard.

Pascal Example: adding a field with constructor / destructor code

Assume you have a class TDocument and want to add a field FLines: TStrings to store the contents like this (ModelMaker Code Explorer Add Field Dialog):

Creational Wizard: add field dialog

...then usually you need to add a constructor with code that instantiates FLines and add a destructor with code to dispose the lines again, for example like this:

constructor TDocument.Create;
begin
  inherited Create;
  FLines := TStringList.Create;
end;

destructor TDocument.Destroy;
begin
  FreeAndNil(FLines);
  inherited Destroy;
end;

This is exactly what the wizard does! Go to the Creational Wizard Tab, and click the "Composition" preset:

Creational Wizard: Composition preset

This preset selects a constructor and destructor and suggests code snippets, based on the field type and name.

Creational Wizard: add field dialog

Refactor

after clicking OK, not only the field is inserted, but also the constructor, destructor and suggested code snippets. Exactly as we wanted it! Instead of creating a new constructor, you can add the snippets to an existing constructor.

constructor TDocument.Create;
begin
  inherited Create; Refactor Result
  FLines := TStringList.Create;
end;

destructor TDocument.Destroy;
begin
  FreeAndNil(FLines);
  inherited Destroy;
end;

C# Example: adding a readonly field with constructor parameter

Fields with a readonly modifier are typically used to store a reference to another object that is passed in the constructor parameter list. Assume you want to create a helper class DocumentReader that performs operations on the Document class above. You would then need to add a read-only field like this:

Creational Wizard: C# add field dialog

...and add a constructor with a parameter and initialize the field document like this:

public class DocumentReader {
  public DocumentReader(Document ADocument) {
    document = ADocument;
  }
  private readonly Document document;
}

Again, this is exactly what the wizard does! Go to the Creational Wizard Tab, and click the "Aggregate" preset:

Creational Wizard: Add constructor parameter

This preset selects a constructor, clicks the option "add to constructor parameter list" suggests a parameter name and code snippet.

Refactor

after clicking OK, not only the field is inserted, but also the constructor plus initialization code. Exactly as we wanted it!

public class DocumentReader {
  public DocumentReader(Document ADocument) { Refactor Result
    document = ADocument;
  }
  private readonly Document document;
}

Pascal Example: initializing multiple fields and properties at once.

If you added a more fields and properties to your Document class, for example Header, Footer and Parent that all need initialization code, you can invoke the Creational Wizard on the class itself. This class wizard is similar to the one on the field and property dialogs, except that it shows all fields and properties in a class and a preset can be applied to multiple selected members at once.

Creational Wizard: applied on class

... as you see in the above dialog, Header, Footer and Lines are initialized and destroyed, the Parent document is an aggregate reference. Clicking OK results in the following code:

constructor TDocument.Create(AParent: TDocument);
begin
  inherited Create; Refactor Result
  Parent := AParent;
  FHeader := TStringList.Create;
  FFooter := TStringList.Create;
  FLines := TStringList.Create;
end;

destructor TDocument.Destroy;
begin
  FreeAndNil(FLines);
  FreeAndNil(FFooter);
  FreeAndNil(FHeader);
  inherited Destroy;
end;

Customizing the wizard

The wizard is customizable using a type based look-up list in file CreationalWizard.txt in the shared directory, for example "C:\Program Files\ModelMakerTools\Shared". Here you define type initialization and finalization code per language.