Display Timecards

Now that we have our model created and saved, we can write some code to see some results. First we will generate code with AndroMDA, and then we will implement the GetAllTimecards service method. After that we will export the schema to our TimeTracker database and finally we will create a screen that lists all the timecards in the database. Please note the TimeTracker completed sample is available here:Northwind.TimeTracker.zip.

Generate Code

First we will generate code from our model. Switch to Visual Studio and you can either select the Build | Rebuild All option or you can click the Generate Generate button. This will run AndroMDA inside a Visual Studio tool window and generate the entity classes, hibernate mappings, data access objects, and service interfaces and base classes.

GeneratedFiles

When this operation is complete you will see a number of files generated. Files that you don't need to edit and are always generated are placed in the target directory. Files that require you to fill in some code will be placed in the src directory if they don't exist. If they do exist they will not be overwritten so your changes will be preserved.

Writing the Entity to VO Conversion Code

The first thing we need to do is write some code that will convert between Timecard entities and TimecardVO value objects.

  1. Open the TimecardDaoImpl.cs file.
  2. Note the generated file already has created the method stubs ready for us to implement. Fill these methods in. Your final code should look like this:
                            using Northwind.TimeTracker.VO;
    
                            public class TimecardDaoImpl : TimecardDaoBase
                            {
                                public override TimecardVO ToTimecardVO(Timecard entity)
                                {
                                    // Entity to VO conversion
                                    TimecardVO valueObject = new TimecardVO();
    
                                    valueObject.StartDate = entity.StartDate;
                                    valueObject.Comments = entity.Comments;
                                    valueObject.Id = entity.Id;
    
                                    return valueObject;
                                }
    
                                public override Timecard TimecardVOToEntity(TimecardVO timecardVO)
                                {
                                    // VO to entity conversion
                                    Timecard entity = Timecard.Factory.newInstance();
    
                                    entity.StartDate = timecardVO.StartDate;
                                    entity.Comments = timecardVO.Comments;
    
                                    return entity;
                                }
                            }
                        

Implement the Service Method

Now we can implement the method in ourTimeTrackingService.

  1. Open the TimeTrackingServiceImpl.cs file.
  2. Note the generated file already has created the method stubs ready for us to implement. In addition, the service base class already has a method calledGetAllTimecards(). When called, it will start a NHibernate transaction and then call our method: HandleGetAllTimecards(). Here is one example implementation of the GetAllTimecards method:
                            using System.Collections;
                            using Northwind.TimeTracker.VO;
    
                            public class TimeTrackingServiceImpl : TimeTrackingServiceBase
                            {
                                protected override TimecardVO[] HandleGetAllTimecards()
                                {
                                    IList timecards = this.TimecardDao.LoadAll();
                                    IList timecardVOs = this.TimecardDao.ToTimecardVOList(timecards);
                                    TimecardVO[] voarray = new TimecardVO[timecardVOs.Count];
                                    timecardVOs.CopyTo(voarray, 0);
                                    return voarray;
                                }
                            }
                        

Generate Database Schema

Now we will generate the database schema to store the timecards.

  1. Click the Export Schema Generate button. This will run the schema export console project in Visual Studio. The AndroMDA Visual Studio add-in defaults are set to pass the export option so it will write the schema to your database.
  2. After the schema is created, we need to create some test data. Run the following SQL in any tool (query analyzer, management studio, Toad, etc.) You can also run the database/testdata.sql file that is in the completed downloadable solution.
                            DELETE FROM TIMECARD
                            DELETE FROM AppUser
                            SET IDENTITY_INSERT AppUser ON
                            INSERT INTO AppUser (ID, UserName, Password, Email, IsActive, Comment, CreationDate)
                            VALUES (1, 'bob', 'n/a', 'bob@bob.net', 1, '', getdate())
                            SET IDENTITY_INSERT AppUser OFF
                            SET IDENTITY_INSERT TIMECARD ON
                            INSERT INTO TIMECARD (ID, START_DATE, COMMENTS, SUBMITTER_FK)
                            VALUES (1, getdate(), 'This is the first timecard', 1)
                            INSERT INTO TIMECARD (ID, START_DATE, COMMENTS, SUBMITTER_FK)
                            VALUES (2, getdate(), 'This is another timecard', 1)
                            SET IDENTITY_INSERT TIMECARD OFF
                            GO
                        

Create a Test Page

Now that we have our service code, database schema, and test data we can create a web page that lists all the timecards.

  1. Right click on the web project Northwind.TimeTracker.Web and select Add new item
  2. Select Web Form and accept the default name of Default.aspx by clickingAdd.
  3. Add a grid to the page by adding the following code toDefault.aspx:
                            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                            <Columns>
                            <asp:BoundField DataField="ID" HeaderText="ID"/>
                            <asp:BoundField DataField="StartDate" HeaderText="StartDate"/>
                            <asp:BoundField DataField="Comments" HeaderText="Comments"/>
                            </Columns>
                            </asp:GridView>
                        
  4. Add the code to get the timecards and bind to the grid by adding the following code toDefault.aspx.cs:
                            using Northwind.TimeTracker.Service;
                            using Northwind.TimeTracker.VO;
    
                            public partial class _Default : System.Web.UI.Page
                            {
                                protected void Page_Load(object sender, EventArgs e)
                                {
                                    if (!IsPostBack)
                                    {
                                        ITimeTrackingService service = new TimeTrackingServiceImpl();
                                        TimecardVO[] timecards = service.GetAllTimecards();
                                        GridView1.DataSource = timecards;
                                        GridView1.DataBind();
                                    }
                                }
                            }
                        
  5. Right click on the web project and select View in browser

You should see a list of 2 timecards. Congratulations, you have created your first application using AndroMDA!