Products
 

Creating your first ECO application

By Anthony Richardson

Abstract - This article demonstrates building a simple application using the Borland® Enterprise Core Objects (ECO)™ technology in Borland® Delphi™ 2005 Architect.

Introduction

Enterprise Core Objects (ECO)™ is a development and runtime framework for Model Driven Development. The traditional process of transforming requirements to a computer understandable view of the problem may involve the design of databases, applications, and interfaces each with its own way of representing the business logic. ECO applies Object Oriented flexibility and design to the business and persistence layers of your application making design and development easier. It is worth spending time on this upfront because this is a huge part of the productivity enhancement you will experience when using the ECO architecture.

What is modeling?

I have mentioned Model Driven Development, but what is a model? A model is a description or view of something. Like a model aircraft is a miniature representation of a real aircraft, a software model is a representation of the problem domain in which the software will run. It is possible to generate many different models for a particular software project. Popular software engineering methodologies describe models and activities at various stages of the software development lifecycle. Models also simplify communication between developers, users and other stakeholders. By creating the various types of models you can describe a complex software system in a series of progressively more detailed set of views into the problem domain.

The problem with using models, in this way, is that, by the time you have increasingly refined a model to the point that it is completely unambiguous and executable, it no longer is recognizable as a description of the original problem. By the time we have converted a description of the problem progressively from text description to source code and database schemas, only a developer (and the compiler) can understand it. The problem with this is the transformation from business understanding to compiler understanding has to happen each time we modify the code. And when we look at code written by others we need to convert it the other way, from source code to business logic.

ECO provides a way to remove the need to manually perform a lot of the transformation. ECO allows the developer to model a problem in a way that more closely represents the business logic, and is still unambiguous and executable. This is often referred to as raising the level of abstraction. By doing this, the ability to change and understand complex software is made easier and less expensive.

When we create models in ECO the classes we model are often referred to as Business Classes or Domain Classes. At runtime we refer to these as Business Objects or Domain Objects. The reason we use these names is because the classes directly represent entities in the problem domain of the business. For example, in a University enrolment application we may model the business classes Student, Teacher and Subject.

The Example Application

This tutorial will build a simple address book application that saves its data to an XML file. The basic needs for our application are:

  • Store contact information for people.
  • Store contact information for companies.

We are going to implement the following Unified Modeling Language™ model for our address book application:



If you are not familiar with UML, the diagram shows three classes, represented by the rectangles. The class 'Contact' although not marked in the above diagram, is an abstract class. The lines with the arrowheads indicate that both the 'Company' and 'person' classes descend from the 'Contact' class. The model shows the following business rules:

  • A Contact has a name, address and phone number.
  • A Contact must be either a person or a Company.

For this example I could have implemented the model using one class and a simple Boolean flag to indicate if the contact was a person. I chose the above model because in the next tutorial we will extend the model and I also wanted this tutorial to demonstrate the basic generalization relationship.

Building the Application

  • Select "Other" from the "File|New" menu.
  • From the "New Items" dialog, select "Delphi for .Net Projects" and then "ECO Windows Forms Application". Press the "OK" button.
  • The New Application Dialog is displayed. Enter the name of the application. For this example use the name "ContactManager" press the "OK" button.




Borland Delphi creates a bare ECO application. The "project Manager" (View|project Manager) shows the automatically generated files. There are two files that are different to a normal Delphi Application. These are ContactManagerEcoSpace.pas and CoreClassesUnit.pas.




CoreClassesUnit.pas

This file contains an empty "UML package". This is where the classes for our ECO application will be created by default. It is possible to create multiple packages, however, this tutorial will only use the default package.

ContactManagerEcoSpace.pas

This contains the "EcoSpace" for the your application. An EcoSpace contains the objects of the application at runtime. It provides the link between the objects and the persistence mechanism (XML or RDBMS). The EcoSpace can be queried using OCL to retrieve objects from the EcoSpace.

From the Borland Delphi help file:

"An ECO Space is a container of objects. At runtime, the ECO Space contains actual instances of the classes in your model. It is helpful to think of the ECO Space as an actual instance of a model, much like an object is an instance of a class. The objects contained in the ECO Space retain the domain properties (attributes and operations) and relationships defined in the model.

As a container of objects, an ECO Space is both a cache, and a transactional context. Within the ECO Space, another component called a persistence mapper is used as the conduit between the persistence layer (either an RDBMS or XML file), and the instances of the classes defined in the model. When you configure an ECO Space using the IDE, you will select those UML packages in your model that you wish to persist in the ECO Space."

Building an ECO Model

ECO uses a subset of the Unified Modeling Language (UML) as its modeling language. The specific parts are the Class Diagram and Object Constraint Language (OCL).

Creating the model

From the "Model View" double-click the "CoreClasses" diagram item. This will open the modeling surface editor. When this opens, the "Tool palette" changes to contain the tools for generating our model.



Begin by creating a new class. You can do this one of three ways.

  • Select the "Class" tool in the "Tool palette" and click the modeling surface.
  • Right click the modeling surface and select Add|Class from the pop-up menu.
  • Right click the modeling surface and select "Add New Item..." from the pop-up menu. Select the Class Template and press OK.

Once the class is created, ensure it is selected so the properties of the class are visible in the "Object Inspector" Select the name property to "Contact" and the Abstract property to "True" a corresponding abstract class called Contact will be created.



Right-click the class and select "Add|Attribute". Select the attribute in the class with the mouse and the "Object Inspector" will display the properties for the attribute. Set this attributes name to "fullName" and type to "String".



Add the string attribute's address and phoneNumber to the Contact class. Add the classes person and Company to the model. Leave the abstract property false as these will be the concrete classes we create instances of.

We now need to create a generalization relationship between the person class and the Contact class. A generalization relationship specifies that person is a type of Contact, and inherits all the attributes and operations of Contact.

To create this relationship select the "Generalization/Implementation" tool and select the person class and drag to the Contact class.



Create the same generalization relationship for the Company Class. The completed model is below.



It is interesting to take a look at the "Model View" now that we have added some classes. The Model view shows the two models in the ECO Application. The Domain Model is the model we have just created, the Implementation Model is auto generated by ECO. The Implementation Model is created as Delphi classes.



The code generated by ECO shows the large amount of implementation detail that ECO takes care of for the developer. This code allows for managing lists of each domain class, enumerator classes, adapter and all the management code for maintaining the objects within a running application. Normally the developer is responsible for this code and it quickly turns a readable model from being a clear representation of the problem, into a mass of complex structures and architectural elements unrelated to the real problem.

Adding a User Interface

Now that a model has been created, it is necessary to add a simple GUI to explore the runtime behaviour of ECO.

NOTE: At this point in the exercise it is important that you compile your application.

ECO needs to have the model compiled so the GUI designer can display the model information. The runtime system of ECO relies on the interrogation of the classes using .Net Reflection. For Reflection to work you must compile the application.

Using the "Tool palette" add the follow controls to the WinForm.pas form.

  • Category: WindowsR Forms add 2 Buttons.
  • Category: Data Controls add 3 DataGrids.
  • Category: Enterprise Core Objects add 3 ExpressionHandles.

Set the following properties:

Type: Button
  Name: btnAddPerson
  Text: Add Person
  
Type: Button
  Name: btnAddCompany
  Text: Add Company
  
Type: ExpressionHandle
  Name: ehContacts
  Root: rhRoot
  Expression: Contact.allinstances
  
Type: ExpressionHandle
  Name: ehPersons
  Root: rhRoot
  Expression: person.allinstances
  
Type: ExpressionHandle
  Name: ehCompanies
  Root: rhRoot
  Expression: Company.allinstances
  
Type: DataGrid
  Name: dgContacts
  DataSource: ehContacts
  
Type: DataGrid
  Name: dgPersons
  DataSource: ehpersons
  
Type: DataGrid
  Name: dgCompanies
  DataSource: ehCompany




Enter the following code for the buttons OnClick event code:

procedure TWinForm.btnAddPerson_Click(sender: System.Object; 
  e: System.EventArgs);
begin
  Person.Create(EcoSpace);
end;

The above code is all it takes to create an object instance at runtime using Delphi code. However, an alternative to using code is to use ECO List actions. ECO list actions are available for a wide variety of common functions you may want to perform.

To use ECO List actions to create company instances, simply set the following properties:

Type: Button
  Name: btnAddCompany
  BindingContext: dgCompanies
  EcoListAction: Add
  RootHandle: ehCompanies

The 'Add Company' button can now create new Company objects without any code being written.

The ReferenceHandle component, which was placed on the form by the ECO Application wizard, provides the link between the EcoSpace and the Winform. A ReferencetHandle can be set to reference a specific object in an ECO Space or the entire ECO Space. This is how the ReferenceHandle is being used in this example. It is important to set the reference handle to use the ECO Space we have just defined in our model, set the following parameter for the rhRoot component.

 EcoSpaceType = ContactManagerEcoSpace.TContactManagerEcoSpace

The ExpressionHandle is linked to another handle, either an ExpressionHandle or a Referencehandle, and evaluates an OCL expression to provide a resulting object, collection or value. It is possible to daisy chain ExpressionHandles where each handle evaluates its result from the results of the previous handle.

Running the Application

Run the application and create some persons and Companies.



The grid set to show "person.allinstances" shows only the persons created. The grid set to show "Company.allinstances" show only the Companies created. The grid set to show "Contact.allinstances" shows both the Persons and the Companies.

Persisting the data

An application like in this example would generally be required to save the entered data between each invocation. With ECO there are two basic persistence methods supported. Saving to a XML file or saving to a RDBMS. For this example an XML file will be used, as it's simpler to configure.

From the "project Manager" double click ContactManagerEcoSpace.pas to call up its Design Editor. From the "Tool palette" select the 'persistenceMapperXML" tool and drop it on the designer. Set the following properties:

Type: persistenceMapperXML
  Name: persistenceMapperXML1
  FileName: data.xml

(The IDE will most like populate the following property for you automatically)

Type: EcoSpace
  persistenceMapper: persistenceMapperXML1

To set the EcoSpace properties, click anywhere on the design surface that isn't a component and the EcoSpace properties will be available in the Object Inspector.



On the WinForm.pas form add a new button. Name it "btnSave" and set the text to "Save". Set the following properties to use an ECO action to save all changes to the XML file.

Type: Button
  Name: btnSave
  EcoAction: UpdateDatabase

Alternative to using ECO actions you could use the following OnClick event code for the button.

procedure TWinForm.btnSave_Click(sender: System.Object;
  e: System.EventArgs);
begin
  EcoSpace.UpdateDatabase;
end;

Now when you run the application the data you enter will be saved to the data.xml file each time you press the "Save" button.

It's worth noting that there was no requirement within the model to specify information relating to how the data was going to be saved. However, this is possible if required.

Auto Forms

ECO supports the automatic generation of forms for editing your ECO Objects at runtime. The default ECO enabled WinForm includes a ECOAutoForms component. This component extends the data aware controls on your form to support double click activation of the objects auto form. Set the EcoAutoForm property of all three grids to true.



At runtime you can activate the auto form by double clicking the fixed column of any grid.



Conclusion

ECO offers a way to develop applications where the developer spends more time coding the business logic and less time on architectural coding. This allows for the code to be clearer and reduces the cost of change. ECO provides a distinct separation between presentation, logic and persistence. This reduces the effect changes have on an application.

About the Author

Anthony Richardson is the director of Viewpoint (SA) Pty Ltd. Anthony has many years of experience working with Borland MDA technologies. Anthony's contact details are available at http://www.viewpointsa.com/