Holger's Thoughts on Delphi

Monday, January 02, 2006

ECO: Using Building Blocks

My last post introduced you to the nice and pretty "Category" class. I also mentioned that you can use it in any ECO Application you might build. What I did not mention was... "How?"

Let me at first tell you how I built the Category class to make it easy inserting it into other applications. It is not difficult to use ECO classes from multiple packages in one ECO Space. The difficult thing to do is to refer to classes during design-time. In this example I will build an application that assigns categories to a product. That means I have to create an association from a class named "Product" to the already modeled "Category" class.

Step 1: Create a new ECO Package DLL. I used the C# personality for that. That is just my personal taste, of course. I can handle namespaces easier in C# that is the reason why I generate C# code for my models.

For that, go to "File / New...", select "Other..."...

... click on the correct personality and double-click the "ECO Package DLL" item.

Step 2: Name your model library appropriatly, remember to use namespaces. From my own experience, you get name conflicts if you do not create proper namespaces.

Step 3: Switch to the model view and rename "Package_1" to something that makes more sense. I named it "CategoryPkg" as it is the package for the category logic.

Step 4: You are ready to double-click on the package to design your class model. You can have a look at the previous post, what the class looks like.

That's it. This will make sure that you can use the business logic you define in the library in any other ECO application, even during design-time.

So, how do we actually include a model in another application during design-time? Well, I tried to find it in the help of D2006 and I actually could find zero on that topic. So, I will be very verbose and will try to give you as much information as I can.

First, create a new ECO application, of course, it could also be another library that is based on the other library. But we'll create a Delphi .NET Winforms Application in this example.

Before we start to design the classes belonging to that application, we will add a reference to the library, which contains the class "Category". I always use the Project Manager, right-click the "References"-node and click on "Add Reference...".

Select the library-file using the "Browse..."-button and click on "OK" to apply the changes. it is very important that you build your project now. I said build, not compile. Just in case.

The next step is to take care of the fact that we need the category class during design-time already. So right-click the exe-filename and choose the menu item named "Referred ECO Packages... ".
You have to browse for the "ecopkg"-file in your library project. Click on "Add..." to achieve that. Exactly, that means if you deploy a ECO library with classes to a customer, you also have to deploy all files named "ecopkg" as those contain the design-time information that make modeling with the classes in your library possible.
Save your project and close it. Sadly, this is necessary for the Model View to pick up the new ECO Package. I might be wrong, but I found no other means how to do it. If you did it correctly, you will have the referred ECO Package listed in the Model View as shown below.

After re-opening your project, open "Package_1" and add a class named "Product".

Right-click on the modeling workspace and choose "Add / Shortcut...". This will make it possible for you to add the class "Category" to the model.

In order to do it, you will have to expand the "References" node like I did it in the screenshot below. Be sure to select "Category" and not, like it is selected below, "MCategory", any other namespace or package. We want to add a shortcut to a class to the model, nothing else.
Good. Now we have a shortcut (as in Windows it is shown using an arrow in the corner) to Category in our model. Simply add an association between the two classes as you do it with any other class.

That's it. Yes, it is that easy. Build your project, switch to "Winform.pas" and implement the OnLoad event, for example. You will notice if you type the following code...

that the Product has a property called "Category". Even more surprising, it is of type "Category", just as we wanted it to be.

Isn't ECO just great? You just have to love it a little bit more because of this...


Post a Comment

<< Home