Tuesday, January 03, 2006

ECO Adventures: Master-Detail

I finally have the time to play with ECO and learn about what it is.
In this episode of 'The ECO adventures' I create a Master-Detail model.

In VCL it is very easy to create a master-detail relationship between two datasets (TDataSets) using the mastersource property. In ADO.NET you can make such relations within the .net dataset, but implementing them is a bit different compared with the TDataSet.
In ECO, it is again relative easy to implement.

Making the model
Take this model, where we have Customers and Projects. This two objects has a relation, where a customer has zero or more projects, and a project belongs, of course, to one customer.
In ECO we can model it like this:



Take an extra look at the /Project attribuut. This is a derived attribuut, in this case showing the projectnumber (an integer) and the description in one field.
This can be done by OCL. The expression for this one is:

self.Number.asString + '-' + self.description

You can easily create this with the OCL expression editor.

I have created this model in a ECO Package, so that it can be used in multiple client applications. (Winform and ASP.NET)

Making the Winform Application
Just add a ECO Winform Application to your projectgroup with the ECO package and make a project reference to the ECO dll. In this case it is the MasterDetail.dll.
After setting the reference, you must select the package in the ECO space of the Winform application, to let the application know that it can use the package.

After that you must first recompile for further designtime support.

Now we can create the GUI. The form has standard a few ECO components, with among others the rhRoot, wich connects the form to the ECO space. (The reference to the model)
Select the ECO space in the ECOSpaceType property of the rhRoot.

Connecting to the object instances of our classes (Customers and Projects) we use ExpressionHandles, which are connected to the model through the roothandle (rhRoot).

To get the project details, we have to use a CurrencyManagerHandle, which has the grid (in general a GUI control) with Customers as the BindingContext. Through this handle we connect to projects of a specific Customer. (This is like the CurrencyManager in ADO.NET)

So let's make this GUI:





Create a new expressionhandler and call this ehCustomers.
Set properties as below:
  • Roothandle -> rhRoot
  • Expression -> Customer.AllInstances (Use the OCL editor for this by invoking it with the ... button)

Set the property DataSource of the customer grid to ehCustomers. Notice that the grid shows the column name already!
ECO has some strong listactions build in to invoke operations directly on your objects. Set the property ECOListAction of the button right from the grid to 'Add'. And the Roothandle to ehCustomers.

Run the application.

OK this works. So far pretty easy, isn't it? Well now we want to see the projects of a specific customer in the other grid.
Here we need the CurrencyManagerHandle. Drop a CurrencyManagerhandle on the form and set the properties as follows:

  • Name -> cmCustomer
  • RootHandle -> ehCustomers
  • BindingContext -> CustomerDataGrid

The currencymanagerhandle will select a single instance of a customer (iow the selected customer in the grid).

To get the projects we need to drop another Expressionhandle, with the following properties set:

  • Name -> ehProjects
  • RootHandle -> cmCustomer (yes the CurrencyManagerHandler...)
  • Expression -> self.Projects (To select in the OCL editor through the Roles node)

Connect the projectgrid to ehProjects and assign the button for the Add list action.

Run the application.

Well this was pretty much it. Set the property Autoform on the grids to true and you already have a complete working application.

Conclusion:
ECO is a new way of programming (to me it is) .The way to use it is a little different compared to the database-driven approach although the main concept is the same. Keeping the business rules (like the relation) in the model is a strong benefit for the ECO approach. This was 'my first ECO application', and it was not all that hard. One thing to remember is to Compile your application often, this is needed for the designtime support I guess, otherwise you will sometime s look for property values you need which will not be there.

This ECO adventure will be continued...

No comments: