|
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:
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) |
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:
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
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.
![]() |
|---|
| Figure 4.4 - Select generate code |
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.
![]() |
|---|
| Figure 4.6 - School System |
![]() |
|---|
| Figure 4.7 - Register dialog |
...
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
((Teacher)User).Email = emailTextBox.Text;}
Source File : Standalone School System\RegisterDialog.cs
User.Save();}
t.Commit();
Close(); //close dialog
Console.WriteLine(ex.InnerException.Message);}
t.RollBack();
Source File : Standalone School System\RegisterDialog.cs
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 |
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
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.
![]() |
|---|
| Figure 4.10 - To modify user information |
![]() |
|---|
| Figure 4.11 - Modify User Information dialog |
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
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.
![]() |
|---|
| Figure 4.12 - Create Course dialog |
![]() |
|---|
| Figure 4.13 - Course are created |
![]() |
|---|
| Figure 4.14 - Delete a course |
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.
_courseNode.Course.DeleteAndDissociate();}
_courseNode.Remove();
t.Commit();
Console.WriteLine(ex.InnerException.Message);}
t.RollBack();
Source File : Standalone School System\CoursePanel.cs
|
|
|||||||