Previous Next
Programmers Guide for .NET Chapter 4 - Developing Standalone .NET Application

Chapter 4 - Developing Standalone .NET Application

With DB Visual ARCHITECT (DB-VA) you can develop quality Standalone .NET Application much faster, better and cheaper. DB-VA generates code, configuration files and persistent layer library. You do not need to write SQL to insert, query, update or delete the records. All code you need to program is plain C# code (e.g. OrderDAO.save(myOrder);). In this chapter we will use a simple "School System" application to show you how to generate C# code, create standalone C# application, and create/query/update/delete objects. Again, you do not need to write a single SQL statement for all the above operations.

Figure 4.1 - The architecture of Standalone .NET Application with DB-VA Persistent Layer

In this chapter:

Introduction

You will develop a School System.

The School System provides the following functions:

Required Software:

Class Diagram of School System:

Figure 4.2 - Class Diagram

Entity Relationship Diagram of School System:

Figure 4.3 - Entity Relationship Diagram (ERD)

Using the PersistentManager

In DB-VA generated code there is a PersistentManager class. The PersistentManager can manage the database connection information and states of the persistent objects. When you create or update the persistent object, you can request the PersistentManager to get session and notify begin transaction.

Sample:

private void okButton_Click(object sender, System.EventArgs e)
{
DialogResult = DialogResult.OK;
if (titleTextBox.Text.Length > 0)
{
PersistentTransaction t = SchoolSystemPersistentManager.Instance().GetSession().BeginTransaction();
try
{
Course lCourse = CourseFactory.CreateCourse();
lCourse.Title = titleTextBox.Text;
lCourse.Description = descriptionTextBox.Text;
lCourse.Teacher = _teacher;
lCourse.Save();
t.Commit();
CreatedCourse = lCourse;
Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
t.RollBack();
}
}
else
{
MessageBox.Show("Missing Title");
}
}

Source File : Standalone School System\CreateCourseDialog.cs

Creating Object and Saving to Database

In developing C# application, you need to generate code by DB-VA and configure the source code and library. The following example is a standalone .NET application, so you need to pay attention to the following settings when configure to generate C# code.

  1. From the menu bar, select Tools > Object-Relational Mapping (ORM) > Generate Code... to open the Database Code Generation dialog box.
  2. Figure 4.4 - Select generate code
  3. Fill in code generation information.
  4. For Language, select C#.
    For Association Handling, select Smart.
    For Persistent API, select Factory Class.

    Figure 4.5 - Database Code Generation dialog

    For the other configuration details, please refer to Chapter 1 - Generating .NET Code, Database Schema (DDL) and Persistent Library and Chapter 2 - Configuring Source and Library in Visual Studio.

    The school system provides the register function for teacher and student. They need to enter login id, password etc...information to the system. The registration process is the same for both teacher and student, so we only demonstrate how to create the student and save to database.

  5. From the menu bar, select File > Student Register to open the Add Student dialog box.
  6. Figure 4.6 - School System
  7. Enter the Student information; click OK to create the new Student record in the School System.
  8. Figure 4.7 - Register dialog
  9. After click OK, the system creates the new Student Persistent object.
  10. Private void okButton_Click(object sender, System.EventArgs e)
    {
    ...
    PersistentTransaction t = SchoolSystemPersistentManager.Instance().GetSession().BeginTransaction();
    try
    {
    if (_userType == CREATE_TEACHER)
    {
    User = TeacherFactory.CreateTeacher();
    }
    else if (_userType == CREATE_STUDENT)
    {
    User = StudentFactory.CreateStudent();
    }

    Source File : Standalone School System\RegisterDialog.cs

  11. Set the student information from the text fields value to the Student Object
  12. User.Name = userNameTextBox.Text;
    User.Password = passwordTextBox.Text;
    User.LoginID = loginIDTextBox.Text;
    if (User is Teacher)
    {
    ((Teacher)User).Email = emailTextBox.Text;
    }

    Source File : Standalone School System\RegisterDialog.cs

  13. Call Save() method of Student Persistent Object and call Commit() method of PersistentTransaction., then the new Student object will be saved in database. If the transaction has error occurred during the transaction, you can call the rollback() method to cancel the proposed changes in a pending database transaction.
  14. User.Save();
    t.Commit();
    Close(); //close dialog
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.InnerException.Message);
    t.RollBack();
    }

    Source File : Standalone School System\RegisterDialog.cs

Querying Object from Database

After the user login the School System, the system queries different Course objects from the database according to user role. If the user is a student, then the system shows all the available courses. The student can select and register the course. If the user is a teacher, then the system shows courses that are created by that teacher. The teacher can update or delete the course information in the system.

Student Login:

Figure 4.8 - Student login to the system

Teacher Login:

Figure 4.9 - Teacher login to the system
  1. Query the course objects when user login. When Student login, the system will call listCourseByQuery() method in CourseFactory to get all available courses. When Teacher login, the system will call courses collection variable in Teacher object.
  2. private void UpdateTreeView()
    {
    Course[] lCourses = null;
    if (CurrentUser is Student)
    {
    lCourses = CourseFactory.ListCourseByQuery(null, null);
    }
    else if (CurrentUser is Teacher)
    {
    lCourses = ((Teacher)CurrentUser).courses.ToArray();
    }

    Source File : Standalone School System\SchoolSystemForm.cs

Updating Object and Saving to Database

You can modify the user information and update the record in database. After the user login, the User object is stored in the application, so you can set new information in the user object and update the database record.

  1. From the menu bar, select User > Modify User Information to open the Modify User Information dialog box.
  2. Figure 4.10 - To modify user information
  3. Enter new user information and click OK to update the User record.
  4. Figure 4.11 - Modify User Information dialog
  5. Update the information for the User object includes password, name and email address. This operation is just as simple as the create User object process.
  6. private void okButton_Click(object sender, System.EventArgs e)
    {
    if (nameTextBox.Text.Length == 0 || passwordTextBox.Text.Length == 0)
    {
    MessageBox.Show("Missing user name or password");
    }
    else
    {
    PersistentTransaction t = SchoolSystemPersistentManager.Instance().GetSession().BeginTransaction();
    try
    {
    _user.Name = nameTextBox.Text;
    _user.Password = passwordTextBox.Text;
    if(_user is Teacher)
    {
    ((Teacher)_user).Email = emailTextBox.Text;
    }
    _user.Save();
    DialogResult = DialogResult.OK;
    t.Commit();
    }
    catch (Exception)
    {
    DialogResult = DialogResult.Cancel;
    t.RollBack();
    }
    Close();
    }
    }

    Source File : Standalone School System\ModifyUserDialog.cs

Deleting Object in Database

Teachers can create courses for students to register and they can cancel courses in the system. They only need to click delete button of the course then the course information will be deleted in the database, and its relationships with registered students will be removed.

  1. Teacher can create the course by clicking the Add Course button, fill in Course name and Description to create Course
  2. Figure 4.12 - Create Course dialog
  3. Student can register the course by clicking the Register button.
  4. Figure 4.13 - Course are created
  5. The teacher can view how many students registered his course, and he can delete the course in system.
  6. Figure 4.14 - Delete a course
  7. Click Delete of a Course then it will trigger the delButton_Click() method .
  8. private void delButton_Click(object sender, System.EventArgs e)
    {
    if (MessageBox.Show("Delete", "Delete", MessageBoxButtons.OKCancel) == DialogResult.OK)
    {

    Source File : Standalone School System\CoursePanel.cs

    Call deleteAndDissociate() method to delete the course object and remove the relationships of student and teacher with the course.

    Try
    {
    _courseNode.Course.DeleteAndDissociate();
    _courseNode.Remove();
    t.Commit();
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.InnerException.Message);
    t.RollBack();
    }

    Source File : Standalone School System\CoursePanel.cs


Previous Next
Visual Paradigm International Limited
Website: www.visual-paradigm.com
E-mail: support@visual-paradigm.com