Getting started with AndroMDA.NET

Understanding new tools and technologies can be a daunting task. AndroMDA is no exception. This tutorial serves as a gentle introduction to the power of AndroMDA. We will show you step-by-step how to set up your development environment and build your first .NET application. Instead of mechanically going through a series of steps, we will focus on ideas and concepts behind what we are doing. Armed with this knowledge you will be well positioned to take on real world challenges. Please set aside half a day of quality uninterrupted time to learn AndroMDA as there is lot to learn. Then brew a good cup of coffee and immerse yourself in the wonderful world of Model Driven Architecture.

Prepare the development environment

What follows is a step-by-step guide to set up your development environment for AndroMDA. We assume that you are working on a Windows platform.

Install Visual Studio

Install Visual Studio 2003 or Visual Studio 2005. Our preference, without any doubt, is Visual Studio 2005. It is a much more productive environment. So if you have any flexibility in this matter, do yourself a favor and choose this newer version! The instructions in this tutorial assume VS 2005. If you are using VS 2003, you will have to make minor adjustments to do equivalent tasks.

When installing Visual Studio 2005, use custom installation options to prevent the installation of SQL Server Express Edition. We will be installing SQL Server 2000 or 2005 in the next step. Also note that AndroMDA.NET currently supports generation of C# only, other .NET languages are not supported.

Install SQL Server

Install SQL Server 2000 or SQL Server 2005. You should be able to use other databases by configuring NHibernate appropriately. However for the purpose of this tutorial we will assume SQL Server.

Install Java

Install J2SE Development Kit 5.0 (JDK 5.0) from here. This is the preferred version of the JDK for AndroMDA.NET. We also support JDK version 1.4. If you prefer this version, you can download it from here.

Make sure that the JAVA_HOME environment variable is pointing to the directory where you installed the JDK. It is possible you have this variable set already, but just to make sure here's how to check.

  1. Go to the control panel and double-click on System . If you are a Windows XP user it's possible you have to open Performance and Maintenance before you will see the System icon.
  2. Go to the Advanced tab and click on Environment Variables
  3. Make sure you have set the JAVA_HOME variable set and it is pointing to the directory where you installed the JDK. If not, add/edit the variable and set it to the correct value, e.g. C:\Program Files\Java\jdk1.5.0. You may add the variable to the list of user variables or system variables.

Install Maven

Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. We will now install Maven and configure it to download dependencies from the AndroMDA server whenever it needs to.

  1. Download Maven 1.0.2 from this site: http://maven.apache.org/start/download.html. Note that later versions of Maven are not yet supported by AndroMDA.NET.
  2. Install the downloaded binary. (We will assume you installed it in C:\Program Files\Apache Software Foundation\Maven 1.0.2 )
  3. If the installation would complain that the JAVA_HOME environment is not set then make sure you have correctly set it and close Windows Explorer or Total Commander, whatever it is you are using and start it again.
  4. Go to your home directory. (If your login is foo this means here: C:\Documents and Settings\foo )
  5. Create a file there, called build.properties and add the following line in there. This line instructs Maven to look for artifacts at the AndroMDA site in addition to the ibiblio site.

    maven.repo.remote=http://www.ibiblio.org/maven,http://team.andromda.org/maven

Setup Environment variables

We have seen earlier how to set environment variables such as JAVA_HOME , it is strongly recommended to set the following variables before continuing. We repeat JAVA_HOME here just for completeness.

Property Value Required?
JAVA_HOME Java installation directory, e.g. C:\Program Files\Java\jdk1.5.0 Yes
MAVEN_HOME Maven installation directory, e.g. C:\Program Files\Apache Software Foundation\Maven 1.0.2 (this may have already been set by the Maven installer) Yes
MAVEN_OPTS Parameters to pass to the Java VM when running Maven. Set this value to: -XX:MaxPermSize=128m -Xmx512m (required to build large maven projects such as AndroMDA itself) No
PATH This makes sure the system will search for console commands in the directories listed here. If this property already exists you should simply add %JAVA_HOME%\bin;%MAVEN_HOME%\bin Yes

Install AndroMDA C# Application plugin

Next we will download and install the AndroMDA C# Application plugin to the MAVEN_HOME\plugins directory. This plugin allows creation of C# starter applications that use AndroMDA. This is the only AndroMDA artifact that we will install explicitly. All other artifacts, such as AndroMDA cartridges, will be automatically downloaded by the Maven scripts generated by the plugin.

To install the C# Application plugin open a Command Prompt and Execute the following command. Make sure you get a "BUILD SUCCESSFUL" message at the end of the command output.

maven plugin:download -DgroupId=andromda -DartifactId=maven-andromdacsapp-plugin -Dversion=1.0-SNAPSHOT

Notice that you will now have maven-andromdacsapp-plugin-1.0-SNAPSHOT.jar installed at MAVEN_HOME\plugins ( C:\Program Files\Apache Software Foundation\Maven 1.0.2\plugins for this example).

Install a UML tool

Install a tool that will allow you to model your applications in UML and export these models into a format that AndroMDA can understand. AndroMDA currently supports UML 1.4 and XMI 1.2. Please follow one of the links below to install the UML tool of your choice. (As of this writing only one tool is supported. We invite UML tool vendors and open-source projects to test their tools with AndroMDA and supply installation instructions for inclusion in this section.)

MagicDraw 9.5

My first AndroMDA application

Now that the development environment is ready to go, it is time to build our first AndroMDA application. We will design and implement a simple TimeTracker application that allows users to enter timecards.

The diagram below shows four key components of the TimeTracker application and their relationships. We will create a Visual Studio project for each of these components. What follows is a high-level description of each component.

  1. TimeTrackerCommon is a class library that contains classes common to multiple tiers, for example value objects and enumerations.
  2. TimeTrackerCore is a class library that packages the entities and the data access layer for the TimeTracker application. A key design principle used throughout AndroMDA recommends that entities should reside only in the middle-tier of an application, i.e. in the data-access layer and/or in the services layer. Specifically, the front-end and other applications should not have direct access to entities. Instead should be converted to value objects before transfer to/from other tiers. This approach has several advantages. First of all the data passed to other tiers can be massaged to present a view that is more useful or efficient compared to passing entities in their entirety. Sometimes you may want to limit what gets passed to other tiers based on the access rights of the requesting user. For example you may want to return an EmployeeVO object as the return value of a web service call which has the salary field stripped out. The second benefit of this approach is that it allows the middle-tier to act as a gatekeeper of data that can be merged into entities without compromising their integrity. The middle-tier can perform business validations and check for sanity of incoming data. Finally, this approach also helps in controlling what exactly gets serialized when passing information between tiers. As you know, entities can be interconnected to form complex networks. Automatic serialization of these networks could result in transfer of more then necessary set of entities across the wire. In addition, some transport layers have restrictions on how entities can be serialized. For example, in case of web services it is easier to represent data in tree structures compared to networks. Also web services do not understand C# collections. Collections have to be converted into arrays before passing them back and forth. This translation is easily facilitated by the use of value objects.
  3. TimeTrackerConsole is an administrative console application that allows creation and display of various entities used in the TimeTracker application. It demonstrates how to use the entities and data access layer provided by the TimeTrackerCore library.
  4. SchemaExport is another administrative application that generates DDL from the entities defined in the TimeTrackerCore library. This DDL can be used to create tables that will store the TimeTracker entities. SchemaExport also has the capability to directly create the tables in a database.

In this section we will perform the one-time set up that is necessary to start any AndroMDA application. Subsequent sections will describe the iterative development process that follows this initial set up. So roll up your sleeves and let's get started!

Create a Visual Studio Solution

We start by creating a blank Visual Studio solution and populating it with the projects that make up the TimeTracker application.

  1. Start Visual Studio 2005.
  2. Select File > New > Project...
  3. Under Project types (left panel) select Other Project Types > Visual Studio Solutions
  4. Under Templates (right panel) choose Blank Solution.
  5. In the Name field below, enter TimeTracker as the name of the solution.
  6. In the Location field, enter a directory where you want to create the solution. We will assume the location to be C:\.
  7. Now click OK. A blank solution will be created at C:\TimeTracker.

Follow the steps below to add a project called TimeTrackerCommon to our solution. As mentioned earlier, this project will generate a class library containing classes common to multiple tiers.

  1. In Solution Explorer, right-click on the TimeTracker solution and select Add > New Project....
  2. Under Project types (left panel) select Visual C# > Windows
  3. Under Templates (right panel) choose Class Library.
  4. In the Name field below, enter TimeTrackerCommon as the name of the project.
  5. Make sure the Location field contains the name of the solution directory. In this example, the location should be C:\TimeTracker.
  6. Now click OK. A class library project will be created at C:\TimeTracker\TimeTrackerCommon.
  7. Delete the automatically generated class file called Class1.cs.

Follow the steps below to add a project called TimeTrackerCore to our solution. This project will generate a class library containing the entities and the data access layer for the TimeTracker application.

  1. In Solution Explorer, right-click on the TimeTracker solution and select Add > New Project....
  2. Under Project types (left panel) select Visual C# > Windows
  3. Under Templates (right panel) choose Class Library.
  4. In the Name field below, enter TimeTrackerCore as the name of the project.
  5. Make sure the Location field contains the name of the solution directory. In this example, the location should be C:\TimeTracker.
  6. Now click OK. A class library project will be created at C:\TimeTracker\TimeTrackerCore.
  7. Delete the automatically generated class file called Class1.cs.

Follow the steps below to add a project called TimeTrackerConsole to our solution. This project will generate a console application that allows creation and display of various entities used by TimeTracker.

  1. In Solution Explorer, right-click on the TimeTracker solution and select Add > New Project....
  2. Under Project types (left panel) select Visual C# > Windows
  3. Under Templates (right panel) choose Console Application.
  4. In the Name field below, enter TimeTrackerConsole as the name of the project.
  5. Make sure the Location field contains the name of the solution directory. In this example, the location should be C:\TimeTracker.
  6. Now click OK. A console application project will be created at C:\TimeTracker\TimeTrackerConsole.
  7. [VS 2003 only] In the Solution Explorer, rename the automatically generated class file called Class1.cs to Program.cs.
  8. In Solution Explorer, right-click on the TimeTrackerConsole project and select Add > New Item....
  9. Select the template called Application Configuration File.
  10. Make sure the Name field contains the name App.config and click Add. A new App.config file will be added to the project.

Follow the steps below to add a project called SchemaExport to our solution. This project will generate a console application that allows DDL generation from the entities defined in the TimeTrackerCore library.

  1. In Solution Explorer, right-click on the TimeTracker solution and select Add > New Project....
  2. Under Project types (left panel) select Visual C# > Windows
  3. Under Templates (right panel) choose Console Application.
  4. In the Name field below, enter SchemaExport as the name of the project.
  5. Make sure the Location field contains the name of the solution directory. In this example, the location should be C:\TimeTracker.
  6. Now click OK. A console application project will be created at C:\TimeTracker\SchemaExport.
  7. [VS 2003 only] In the Solution Explorer, rename the automatically generated class file called Class1.cs to Program.cs.
  8. In Solution Explorer, right-click on the SchemaExport project and select Add > New Item....
  9. Select the template called Application Configuration File.
  10. Make sure the Name field contains the name App.config and click Add. A new App.config file will be added to the project.

Now save the Visual Studio Solution by selecting File > Save All. Exit Visual Studio by selecting File > Exit. At this point the preliminary set up of our Visual Studio solution is complete.

Create Maven scripts for code generation

Let us turn our attention to the one-time setup required for code generation from AndroMDA. This procedure will add Maven build scripts, an empty UML model and a Lib directory to the TimeTracker solution. The Maven scripts will be used to generate code from our UML model. So let's get started.

  1. Open a Command Prompt and change directory to the parent directory of the TimeTracker solution. In this example, that would be the C:\ directory.
  2. Now execute the following command to generate Maven scripts in the TimeTracker solution. Answer the questions exactly as shown in the output that follows. You are allowed to enter your own name in response to the first question :-).

    maven andromdacsapp:generate

                
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0.2

Please enter your first and last name (e.g. Louis Coude):
Naresh Bhatia
Please enter the name of your C# application (e.g. Time Tracker):
Time Tracker
Please enter a version for your application (e.g. 1.0-SNAPSHOT):
1.0-SNAPSHOT
Would you like to create an ASP.NET web site? (enter 'yes' or 'no'):
no
build:start:

andromdacsapp:init:

andromdacsapp:generate:
    [echo] +------------------------------------------------------------------+
    [echo] |   G E N E R A T I N G   A n d r o M D A   C#   S O L U T I O N   |
    [echo] +------------------------------------------------------------------+
andromdacsapp:init:

andromdacsapp:generate-mda-project:
    [mkdir] Created dir: C:\TimeTracker\mda\src\uml
    [copy] Copying 1 file to C:\TimeTracker\mda\src\uml
    [zip] Building zip: C:\TimeTracker\mda\src\uml\TimeTrackerModel.xml.zip
    [delete] Deleting: C:\TimeTracker\mda\src\uml\TimeTrackerModel.xml
    [mkdir] Created dir: C:\TimeTracker\mda\conf
    [copy] Copying 1 file to C:\TimeTracker\mda

andromdacsapp:init:

andromdacsapp:generate-common-project:
andromdacsapp:generate-project:

    [mkdir] Created dir: C:\TimeTracker\TimeTrackerCommon\src
    [mkdir] Created dir: C:\TimeTracker\TimeTrackerCommon\target

andromdacsapp:init:

andromdacsapp:generate-core-project:
andromdacsapp:generate-project:

    [mkdir] Created dir: C:\TimeTracker\TimeTrackerCore\src
    [mkdir] Created dir: C:\TimeTracker\TimeTrackerCore\target

andromdacsapp:init:

andromdacsapp:generate-schema-export-project:
    [copy] Copying 2 files to C:\TimeTracker\SchemaExport

andromdacsapp:init:

andromdacsapp:generate-lib-directory:
    [mkdir] Created dir: C:\TimeTracker\Lib
    [copy] Copying 11 files to C:\TimeTracker\Lib

    [echo]
    [echo] ***  New C# solution generated in 'C:\\TimeTracker'  ***
    [echo]
BUILD SUCCESSFUL
Total time: 51 seconds
Finished at: Sat Nov 12 17:06:50 EST 2005
                
            

Examine the various folders and files created by the andromdacsapp plugin. You will notice files called project.xml , maven.xml and project.properties at various levels in the TimeTracker solutions. These files represent Maven projects, scripts and properties. You will also notice a mda folder which contains an empty UML model under mda\src\uml . Finally you will see a Lib folder containing dlls to be used by the generated code. Most dlls belong to the NHibernate library which is used for object persistence. In addition you will find the following dlls:

  • AndroMDA.NHibernateSupport.dll : used to manage NHibernate sessions
  • NHibernate.Nullables2.dll : used to store .NET 2.0 Nullable types using NHibernate
  • log4net.dll : used for general purpose logging

By the way, do you remember that so far we have downloaded only one AndroMDA artifact? It was the AndroMDA C# Application plugin. Well, its time to pull in the remaining artifacts. We will do this implicitly by running the maven scripts generated above. These scripts specify all dependencies the application has on AndroMDA. So if we execute them on our blank model, all the necessary dependencies will be pulled into our Maven repository. Of course, since the model is blank, no code will be generated. However getting the dependencies now is important since we will need them very soon. Follow the steps below to download the required dependencies.

  1. Open a Command Prompt and change your directory to C:\TimeTracker .
  2. Execute the command maven . You will see messages on your screen showing artifacts that are being downloaded. Make sure you get a BUILD SUCCESSFUL message when the Maven script ends.

Create a database

Create a new database in your SQL Server instance using SQL Server Enterprise Manager and name it TimeTracker. You may use an existing database, just be sure to replace the name TimeTracker with the name of your database later in the tutorial.

Congratulations! You have now completed the one-time setup for TimeTracker. We are now ready to start modeling!

First Iteration - Implement key TimeTracker classes

In this section we will implement four key entities of the TimeTracker application - Person, Timecard, TimeAllocation and Task. In addition we will implement two supporting classes - TimecardStatus and Quantity. TimecardStatus is an enumeration and Quantity is an embedded value inside the Person entity. The diagram below shows these classes and their relationships.

Note the level of detail in this diagram. Every detail has some meaning - none of the content is "fluff". For example we use stereotypes to indicate the "flavor" of each class - Entity vs. Enumeration vs. EmbeddedValue. Stereotypes tell AndroMDA the pattern of code to generate for each class. More specifically, stereotypes determine which AndroMDA templates get triggered to generate code. Similarly the various decorations you see on associations have very specific meanings. It is this level of preciseness that enables AndroMDA to generate major portions of your application - this is truly what Model Driven Architecture is! Here are some of the advantages of this approach:

  1. The model provides a common language for business users and developers. It describe business requirements precisely and succinctly.
  2. Developers can focus on implementing higher level business logic rather than doing mundane tasks such as writing repetitive code to implement the data access layer.
  3. Developers can be more responsive to changing business needs. Relatively large changes to the business model can be handled more easily, because AndroMDA is there to help you generate major portions of these changes.
  4. The quality of your application will undoubtedly be superior because you are now generating tested and proven patterns instead of writing one-of-a-kind code. Possibility of human error is minimized. In addition, your focus will shift from writing custom code for each situation to discovering commonalities and patterns that can be coded into templates.
  5. Models always represent the current state of the system, not just an outdated piece of documentation. When you are done with your system, the model truly represents the system "as-delivered" - there is no need to go back and retrofit the "documentation" to match what was implemented. AndroMDA indeed makes the model a first-class system artifact as is source code.

Now that you are fully bought into the idea of Model Driven Architecture :-), let's get started with the TimeTracker model shown above.

Create TimeTracker Model

As part of the setup procedure performed earlier, AndroMDA had created an empty UML model in the TimeTracker solution. We will now start populating this model. Please follow one of the links below to edit the TimeTracker model with the UML tool of your choice. (There is only one choice at the time of this writing.)

MagicDraw

Generate code and build the application

Follow the steps below to generate code.

  1. Open a Command Prompt and change your directory to C:\TimeTracker .
  2. Execute the command maven -o and boom, there you have it - code for all the classes in your model is generated! Make sure you get a BUILD SUCCESSFUL message when Maven finishes execution. Note that we ran Maven with the "-o" option this time. This option runs Maven "offline", which means there is no attempt to fetch required dependencies from the Internet. It is assumed that they are all available on your local hard drive. Running Maven with this option is much faster and hence is used more often. However if Maven complains that it cannot find some dependencies locally then it is easy enough to run it without the "-o" option and get them from a remote repository.

Notice that code is generated in the following folders.

  1. C:\TimeTracker\TimeTrackerCommon\target\TimeTracker\Domain: This folder contains only one file, TimecardStatus.cs , representing the enumeration TimecardStatus .
  2. C:\TimeTracker\TimeTrackerCore\target\TimeTracker\Domain: This folder contains the majority of generated files representing entities, embedded values, data access objects and NHibernate mappings. In addition it contains a file called DaoFactory.cs which is used to get instances of the DAOs.
  3. C:\TimeTracker\TimeTrackerCore\src\TimeTracker\Domain: This folder contains several files with Impl suffix. These files are intended for modification by the developer.

Note that code produced in target folders should never be modified by hand. It will be deleted completely before regeneration in the next iteration. It is also a best practice not to check this code into your source repository. Instead let your build script generate this code on the fly. On the other hand, code generated in the src directory is intended for modification by the developer. AndroMDA will never overwrite code in these directories. It is generated one-time only.

Now that you understand the code that has been generated, it is time to compile it. In order to avoid adding each individual file to our solution we will perform a neat trick to make our job easier. We will edit the TimeTrackerCommon and TimeTrackerCore projects so they will compile all C# files in a specified folder. Follow the steps below to implement this trick. Note that this "feature" is not available in VS 2003. You will have to create the src and target directory structures yourself and add the C# files manually. Also don't forget to add the *.hbm.xml files as embedded resources.

  1. Open TimeTrackerCommon.csproj in a text editor.
  2. Search for the following ItemGroup:
    <
    ItemGroup
    > <
    Compile
    Include=
    "Properties\AssemblyInfo.cs"
    /
    > <
    /ItemGroup
    >
  3. Modify the ItemGroup as follows to compile all C# files in the target directory:
    <
    ItemGroup
    > <
    Compile
    Include=
    "Properties\AssemblyInfo.cs"
    /
    > <
    Compile
    Include=
    "target\TimeTracker\Domain\*.cs"
    /
    > <
    /ItemGroup
    >
  4. Save and close TimeTrackerCommon.csproj .
  5. Open TimeTrackerCore.csproj in a text editor.
  6. Search for the following ItemGroup:
    <
    ItemGroup
    > <
    Compile
    Include=
    "Properties\AssemblyInfo.cs"
    /
    > <
    /ItemGroup
    >
  7. Modify the ItemGroup as follows to compile all C# files in the src and target directories:
    <
    ItemGroup
    > <
    Compile
    Include=
    "Properties\AssemblyInfo.cs"
    /
    > <
    Compile
    Include=
    "src\TimeTracker\Domain\*.cs"
    /
    > <
    Compile
    Include=
    "target\TimeTracker\Domain\*.cs"
    /
    > <
    /ItemGroup
    >
  8. Add another ItemGroup right after the one above to embed the NHibernate mapping files in the project:
    <
    ItemGroup
    > <
    EmbeddedResource
    Include=
    "target\TimeTracker\Domain\*.hbm.xml"
    /
    > <
    /ItemGroup
    >
  9. Save and close TimeTrackerCore.csproj .
  10. Now open the TimeTracker solution in Visual Studio and notice that it shows the C# files and NHibernate mappings added to the two projects.

Let's implement the convenience methods added to the 3 classes in the diagram shown earlier.

  1. Open the file PersonImpl.cs in the project TimeTrackerCore . Implement AddTimecard() as follows:
                                    
    
    public
    override
    void
    AddTimecard(TimeTracker.Domain.Timecard timecard) { Timecards.Add(timecard); timecard.Owner =
    this
    ; }
  2. Open the file TimecardImpl.cs in the project TimeTrackerCore . Implement AddTimeAllocation() as follows:
                                    
    
    public
    override
    void
    AddTimeAllocation(TimeTracker.Domain.TimeAllocation timeAllocation) { Allocations.Add(timeAllocation); timeAllocation.Timecard =
    this
    ; }
  3. Open the file TaskImpl.cs in the project TimeTrackerCore . Implement AddTimeAllocation() as follows:
                                    
    
    public
    override
    void
    AddTimeAllocation(TimeTracker.Domain.TimeAllocation timeAllocation) { Allocations.Add(timeAllocation); timeAllocation.Task =
    this
    ; }

Let us now add references to the various projects to reflect their dependencies. To add a reference, simply right-click on the project in the Solution Explorer and select Add Reference. Then select the Projects tab to add a reference to an internal project or select the Browse tab to add a reference to an external DLL (for example, a DLL in the Lib directory).

  • Add the following references to the TimeTrackerCore project. Note that all the DLLs listed below are in the Lib directory of the TimeTracker solution.
    1. TimeTrackerCommon project
    2. Iesi.Collections.dll
    3. log4net.dll
    4. NHibernate.dll
    5. NHibernate.Caches.SysCache.dll
    6. NHibernate.Nullables2.dll (VS 2005 only)
    7. AndroMDA.NHibernateSupport.dll
  • Add the following references to the TimeTrackerConsole project:
    1. All references that are included above in the TimeTrackerCore project
    2. TimeTrackerCore project
  • Add the following references to the SchemaExport project:
    1. All references that are included above in the TimeTrackerCore project
    2. TimeTrackerCore project

We need to do one more thing before building the application. As part of the initial setup, AndroMDA had generated the NHibernate configuration file for the SchemaExport application. This file needs to be added to the SchemaExport project. Here are the steps to do this.

  1. Right-click on the SchemaExport project in the Solution Explorer and select Add > Existing Item.
  2. In the Add Existing dialog, change the value for the drop down "Files of type" to "All Files(*.*). Now the file called nhibernate.config will be visible in the dialog. Select it and click Add. The file will be added to the project.
  3. Select the file nhibernate.config in Solution Explorer.
  4. In the Properties window, change the value of the field "Copy to output directory" to "Copy if newer". This feature is not available in VS 2003. You will have to add the following post-build event to the SchemaExport project: copy "$(ProjectDir)nhibernate.config" "$(TargetDir)"
  5. Open the nhibernate.config file in Visual Studio and change any values if necessary. For example, if the name of your database is not TimeTracker, then change the value of the hibernate.connection.connection_string property accordingly.
  6. Save the solution by selecting File > Save All.

At this point we are ready to build the solution. Select Build > Build Solution. Visual Studio should be able to build the solution without any errors.

Create the database schema

It is now time to create the database schema for TimeTracker entities and relations. Use the SchemaExport application generated by AndroMDA to do this. Here are the steps.

  1. Open a Command Prompt and change your directory to C:\TimeTracker\SchemaExport\bin\Debug
  2. Execute the command SchemaExport t t . This will create the TimeTracker schema in the database.
The output from the execution of SchemaExport is shown below. Notice that you can copy the DDL script from the output and use it in your own build scripts.
                
C:\TimeTracker\SchemaExport\bin\Debug>SchemaExport t t

Usage:
SchemaExport [script] [export]
script=t outputs DDL to the console
export=t exports schema to the database
Example:
SchemaExport t f

Calling CreateDatabase with script = True and export = True...

alter table TIMECARD  drop constraint TIMECARD_OWNER_FKC
Unsuccessful: Cannot alter table 'TIMECARD' because this table does not exist in database 'TimeTracker'.
alter table TIME_ALLOCATION  drop constraint TIME_ALLOCATION_TASK_FKC
Unsuccessful: Cannot alter table 'TIME_ALLOCATION' because this table does not exist in database 'TimeTracker'.
alter table TIME_ALLOCATION  drop constraint TIME_ALLOCATION_TIMECARD_FKC
Unsuccessful: Cannot alter table 'TIME_ALLOCATION' because this table does not exist in database 'TimeTracker'.
drop table TIMECARD
Unsuccessful: Cannot drop the table 'TIMECARD', because it does not exist in the system catalog.
drop table TASK
Unsuccessful: Cannot drop the table 'TASK', because it does not exist in the system catalog.
drop table PERSON
Unsuccessful: Cannot drop the table 'PERSON', because it does not exist in the system catalog.
drop table TIME_ALLOCATION
Unsuccessful: Cannot drop the table 'TIME_ALLOCATION', because it does not exist in the system catalog.
create table TIMECARD (
ID NUMERIC(19,0) IDENTITY NOT NULL,
STATUS INT not null,
BEG_DATE DATETIME not null,
OWNER_FK NUMERIC(19,0) not null,
primary key (ID)
)
create table TASK (
ID NUMERIC(19,0) IDENTITY NOT NULL,
NAME VARCHAR(255) not null,
primary key (ID)
)
create table PERSON (
ID NUMERIC(19,0) IDENTITY NOT NULL,
NAME VARCHAR(255) not null,
RATE_VALUE FLOAT not null,
RATE_UNIT VARCHAR(255) not null,
primary key (ID)
)
create table TIME_ALLOCATION (
ID NUMERIC(19,0) IDENTITY NOT NULL,
BEG_TIME DATETIME not null,
END_TIME DATETIME not null,
TIMECARD_FK NUMERIC(19,0) not null,
TASK_FK NUMERIC(19,0) not null,
primary key (ID)
)
alter table TIMECARD  add constraint TIMECARD_OWNER_FKC foreign key (OWNER_FK) references PERSON
alter table TIME_ALLOCATION  add constraint TIME_ALLOCATION_TASK_FKC foreign key (TASK_FK) references TASK
alter table TIME_ALLOCATION  add constraint TIME_ALLOCATION_TIMECARD_FKC foreign key (TIMECARD_FK) references TIMECARD
                    
            

Build and test the TimeTrackerConsole application

It is now time to test all the functionality we have implemented so far using the TimeTrackerConsole application. Follow the steps below to do this.

  1. Instead of writing the entire console application from scratch copy it from here to C:\TimeTracker\TimeTrackerConsole\Program.cs.
  2. [VS 2003 only] In Program.cs, change all occurences of .Factory.newInstance() to Factory.newInstance() (i.e. remove the "." before Factory ). The reason for this is that .NET 1.1 and VS 2003 do not support static classes and hence AndroMDA generates slightly different code for VS 2003.
  3. [VS 2003 only] In Program.cs, change all occurences of .Value.Add to .Add (i.e. remove the ".Value" before .Add ). The reason for this is that .NET 1.1 and VS 2003 do not support nullable types and hence AndroMDA generates slightly different code for VS 2003.
  4. Review the new code in Program.cs and understand how the DAO classes are used to manipulate entities. Specifically look at the methods AddPerson() , AddTask() , AddTimecard() and ShowData() .
  5. Now copy the two files App.config and nhibernate.config from the directory C:\TimeTracker\SchemaExport to C:\TimeTracker\TimeTrackerConsole . This will configure the application to work with NHibernate.
  6. Right-click on the TimeTrackerConsole project in the Solution Explorer and select Add > Existing Item.
  7. In the Add Existing dialog, change the value for the drop down "Files of type" to "All Files(*.*). Now the file called nhibernate.config will be visible in the dialog. Select it and click Add. The file will be added to the project.
  8. Select the file nhibernate.config in Solution Explorer.
  9. In the Properties window, change the value of the field "Copy to output directory" to "Copy if newer". This feature is not available in VS 2003. You will have to add the following post-build event to the TimeTrackerConsole project: copy "$(ProjectDir)nhibernate.config" "$(TargetDir)"
  10. Save the solution by selecting File > Save All.
  11. Select Build > Rebuild Solution. Visual Studio should be able to build the solution without any errors.
  12. Open a Command Prompt and change your directory to C:\TimeTracker\TimeTrackerConsole\bin\Debug
  13. Execute the command TimeTrackerConsole . This will display menu.
  14. Select various menu items to add people, tasks and timecards to the database.
A sample run of TimeTrackerConsole is shown below.
                
C:\TimeTracker\TimeTrackerConsole\bin\Debug>TimeTrackerConsole

1. Create Database
2. Add Person
3. Add Task
4. Add Timecard
5. Update Timecard
6. Delete Timecard
7. Show Data

Please make a choice ([h]elp [q]uit): 2

Person Name: Naresh Bhatia
Naresh Bhatia added.

Please make a choice ([h]elp [q]uit): 2

Person Name: Louis Coude
Louis Coude added.

Please make a choice ([h]elp [q]uit): 3

Task Name: Research
Task 1 added: Research

Please make a choice ([h]elp [q]uit): 3

Task Name: Development
Task 2 added: Development

Please make a choice ([h]elp [q]uit): 4

Adding timecard...
Timecard 1 created with 1 allocations

Please make a choice ([h]elp [q]uit): 4

Adding timecard...
Timecard 2 created with 2 allocations

Please make a choice ([h]elp [q]uit): 4

Adding timecard...
Timecard 3 created with 2 allocations

Please make a choice ([h]elp [q]uit): 4

Adding timecard...
Timecard 4 created with 2 allocations

Please make a choice ([h]elp [q]uit): 7

People:
1: Naresh Bhatia, rate=10 USD/hour
2: Louis Coude, rate=10 USD/hour

Tasks:
1: Research
2: Development

Timecards:
1: Naresh Bhatia, Draft, 11/16/2005 5:58:29 PM
    1: 11/16/2005 5:58:29 PM, 11/16/2005 6:58:29 PM, Research
2: Naresh Bhatia, Draft, 11/16/2005 5:58:31 PM
    2: 11/16/2005 5:58:31 PM, 11/16/2005 6:58:31 PM, Research
    3: 11/16/2005 6:58:31 PM, 11/16/2005 7:58:31 PM, Development
3: Louis Coude, Draft, 11/16/2005 5:58:32 PM
    4: 11/16/2005 5:58:32 PM, 11/16/2005 6:58:32 PM, Research
    5: 11/16/2005 6:58:32 PM, 11/16/2005 7:58:32 PM, Research
4: Louis Coude, Draft, 11/16/2005 5:58:33 PM
    6: 11/16/2005 5:58:33 PM, 11/16/2005 6:58:33 PM, Research
    7: 11/16/2005 6:58:33 PM, 11/16/2005 7:58:33 PM, Research

Please make a choice ([h]elp [q]uit): q

C:\TimeTracker\TimeTrackerConsole\bin\Debug>
                    
            

Where to go from here ?

In this tutorial we have shown you how to set up your development environment and build your first .NET application using AndroMDA. But this is just the beginning. You can now use AndroMDA to build your own applications. Try out the various features documented under each .NET cartridge. If you don't find a feature that you need, write yourself a template or an entire cartridge. If you think your work might be useful to other developers, don't forget to contribute it to the AndroMDA project.