STEP 6:     Develop your application

Once you have compiled the ORM.NET data layer solution into a .NET assembly you can begin developing your own application using the exposed interfaces.

 

The first time ORM.NET generates a Data Layer solution it also includes a project called TestApp. This empty console based application makes it convenient to test and debug any potential problems before incorporating the ORM.NET Data Layer assembly into another solution. You can remove the TestApp project and include your own .NET based project(s) at any time. Once removed the TestApp project will not be generated again.

 

The outline of the generated TestApp template file TestApp.cs is shown below. Notice that the required assemblies - OrmLib and OleroTrainingBiz (the generated Data Layer) are automatically included. You will need to add these two references for use within your own application.

 

Refer to Integrate Data Layer run-time within an application to see which files and assemblies are required in order to use ORM.NET's Data Layer within your own application.

 

 

using System;

using System.Diagnostics;

using System.Configuration;

using System.Collections;

using System.Data.SqlTypes;

 

using OrmLib;

using OleroTrainingBiz;

 

namespace TestApp

{

       /// <summary>

       /// This test app can be used to test your new Data Layer object.

       /// Please make sure that the test app is set as the startup Project.

       /// </summary>

       class TestApp

       {

              /// <summary>

              /// The main entry point for the application.

              /// </summary>

              [STAThread]

              static void Main(string[] args)

              {

                     //

                     // TODO: Add code to start application here

                     //

                     DataManager dm = new DataManager(Config.Dsn);

 

                   // Start coding!

              }

       }

}

 

 

Visual Basic.NET

 

Imports System

Imports System.Diagnostics

Imports System.Configuration

Imports System.Collections

Imports System.Data.SqlTypes

 

Imports OrmLib

Imports OleroTrainingBiz

 

Module TestApp

    Sub Main()

 

        Dim dm As New DataManager(Config.Dsn)

 

' Start coding!

 

    End Sub

 

End Module

Sample Code using the TestApp Project

To begin writing some sample code using the TestApp you must first compile the OleroTrainingBiz data layer. You will also need to setup the Sample OleroTraining database. Refer

 

Be sure to set the TestApp project as the Start up project within the Solution.

  1. Right click the TestApp project within the VS.NET Solution Explorer

  2. Select the Select as Startup Project item from the menu

The TestApp project will now run when you debug the application.

 

Type or copy and paste the following code examples within the Main() method directly after the DataManager object is instantiated.

 

 

 

C#

 

  // EXAMPLE 1 - Find all students enrolled in Biology 100

 

 

  dm.QueryCriteria.And(JoinPath.Student.Enrollment.Schedule.Course.Columns.ClassName,"Biology 100");

                                                

  // Return this information as a Collection of Student objects with related Enrollment, Schedule and         

  //Course information for each Student

  StudentCollection students = dm.GetStudentCollection(FetchPath.Student.Enrollment.Schedule.Course);

 

  // iterate through the collection of students

  foreach (Student s in students)

       Console.WriteLine("Students in Biology: " + s.LastName);

 

 

 

 // EXAMPLE 2 - Insert a new Teacher with Contact information

                   

 Teacher teacher = dm.NewTeacher();

 teacher.FirstName = "Edward";

 teacher.LastName = "Blake";

                 

 teacher.Contact = dm.NewContact();

 

 teacher.Contact.Address1 = "123 Main Street";

 teacher.Contact.City = "Boulder";

 teacher.Contact.State = "Colorado";

 teacher.Contact.Country = "US";

 

 // Peform SQL operation(s)

 dm.CommitAll();

 

 

                   

 //EXAMPLE 3 - Delete the New Teacher and related Contact info

 

 dm.QueryCriteria.Clear();  // clear any previous queries

 dm.QueryCriteria.And(JoinPath.Teacher.Columns.FirstName,"Edward",MatchType.Exact);

 dm.QueryCriteria.And(JoinPath.Teacher.Columns.LastName,"Blake",MatchType.Exact);

                    

 // Now retrieve Teacher and Contact record from the query above

 teacher = dm.GetTeacher(FetchPath.Teacher.Contact);

 

 teacher.Contact.Delete();   // mark the Parent Teacher Contact for deletion

 teacher.Delete();           // then mark Teacher for deletion

                           

                           

 // Peform SQL operation(s)

 dm.CommitAll();

 

 

 

Visual Basic.NET

 

 

 

  'EXAMPLE 1 - Find all students enrolled in Biology 100

 

 

  dm.QueryCriteria.And(JoinPath.Student.Enrollment.Schedule.Course.Columns.ClassName,"Biology 100")

                                                

  // Return this information as a Collection of Student objects with related Enrollment, Schedule and         

  //Course information for each Student

 

  Dim students As StudentCollection = dm.GetStudentCollection(FetchPath.Student.Enrollment.Schedule.Course)

 

  // iterate through the collection of students

 

Dim s As Student

For Each s in students

       Console.WriteLine("Students in Biology: " + s.LastName)

 

Next

 

 

 ' EXAMPLE 2 - Insert a new Teacher with Contact information

                   

 Dim teacher As Teacher = dm.NewTeacher()

 teacher.FirstName = "Edward";

 teacher.LastName = "Blake";

                 

 teacher.Contact = dm.NewContact()

 

 teacher.Contact.Address1 = "123 Main Street"

 teacher.Contact.City = "Boulder"

 teacher.Contact.State = "Colorado"

 teacher.Contact.Country = "US"

 

 ' Peform SQL operation(s) - Will update both Teacher and Contact changes in round-trip to db

 dm.CommitAll()

 

 

                   

 'EXAMPLE 3 - Delete the New Teacher and related Contact info

 

 dm.QueryCriteria.Clear();  ' clear any previous queries

 dm.QueryCriteria.And(JoinPath.Teacher.Columns.FirstName,"Edward",MatchType.Exact)

 dm.QueryCriteria.And(JoinPath.Teacher.Columns.LastName,"Blake",MatchType.Exact)

                    

 'Now retrieve Teacher and Contact record from the query above

 teacher = dm.GetTeacher(FetchPath.Teacher.Contact)

 

 teacher.Contact.Delete()   ' mark the Parent Teacher Contact for deletion

 teacher.Delete()           ' then mark Teacher for deletion

                           

                           

 ' Peform SQL operation(s)

 dm.CommitAll()

 

 

Step through these statements within Visual Studio.NET to get a better understanding of how to access , modify and delete data using the Data Layer object model interfaces.

 

Refer to the Code Examples section for a more thorough explanation of how to use the ORM.NET Data Layer objects and methods.