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.
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 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.
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.
The first thing we need to do is write some code that will convert between Timecard entities and TimecardVO value objects.
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; } }
Now we can implement the method in ourTimeTrackingService.
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; } }
Now we will generate the database schema to store the timecards.
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
Now that we have our service code, database schema, and test data we can create a web page that lists all the timecards.
<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>
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(); } } }
You should see a list of 2 timecards. Congratulations, you have created your first application using AndroMDA!