SlideShare a Scribd company logo
8

Programming in C++.NET
Programmer’s Guide for .NET

Chapter 8 – Programming in C++.NET

Chapter 8 - Programming in C++ .NET
DB Visual ARCHITECT (DB-VA) can generate C#.NET source code so you can implement your application by C#
programming language directly but you can also choose another language (VB.NET or C++) based on your taste in the .NET
Framework. DB-VA generates DLL file and persistent libraries that can be referenced by .NET projects of language other than
C#.
In this chapter:
•
•
•
•
•
•

Introduction
Generating DLL file
Creating C++ Project
Adding Referenced Project
Working with the Generating Code and Persistent Library
Running the Application

Introduction
C++ is an Object-Oriented Programming (OOP) language that is viewed by many as the best language for creating large-scale
applications. The .NET Framework contains a virtual machine called Common Intermediate Language (CIL). Simply put,
programs are compiled to produce CIL and the CIL is distributed to user to run on a virtual machine. C++, VB.NET, C#
compilers are available from Microsoft for creating CIL. In DB-VA you can generate C# persistent source code and DLL file,
so you can reference the DLL file and persistent library in Visual Studio .NET 2003 and develop the C++ application.
In the following section, you will develop a C++ application. The application is exactly same as the one in Chapter 4 –
Developing Standalone .NET Application sample, but this time you use C++ instead of C# for development. You need to
download the Chapter 4 sample application because it contains DLL file and persistent libraries for your C++ project.

Generating DLL File
1.

From the menu bar, select Tools > Object-Relational Mapping (ORM) > Generate Database… to open the
Database Code Generate dialog box.

2.

Check the Compile to DLL option to generate the DLL file.
• Compile to DLL
By checking this option, DB-VA will generate DLL files which can be referenced by .NET projects
of language other than C#. DB-VA generates batch file for the DLL file at the same time. You can
modify the configuration file (hibernate.cfg.xml) manually and use the batch file to recompile and
build an up-to-date DLL file for referenced project.

8-2
Programmer’s Guide for .NET

Chapter 8 – Programming in C++.NET

Creating C++ Project
1.
2.

Open Microsoft Visual Studio .NET 2003.
Select File > New > Project … from the menu.

3.

Select Project Types as Visual C++ Projects and Templates as Windows Form Application (.NET) and Location for
the new application.

4.

The School System Project is created.

5.

Right click Standalone School System cpp Project, select Add > Add New Item… from the popup menu.

8-3
Programmer’s Guide for .NET

Chapter 8 – Programming in C++.NET

6.

Select Category as Visual C++, Template Windows Form (.NET) and enter the name for the form called
“SchoolSystemForm”. This is the start point for the application.

7.

Append the following content to the SchoolSystemForm.cpp file (Source files/SchoolSystemForm.cpp). This is the
main method for C++ Application to execute.
#include <windows.h>
using namespace StandaloneSchoolSystemcpp;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
Application::Run(new SchoolSystemForm());
return 0;
}

8.

Remove the Form1.cpp and Form1.h files.

Adding Referenced Project
1.

8-4

Right click References under the Standalone School System C++ project and select Add Reference…. Reference the
C# example DDL file and persistent libraries for developing the C++ application.
Programmer’s Guide for .NET

2.

Chapter 8 – Programming in C++.NET

Click Browse… on the Add Reference dialog box to select the folder of the downloaded C# standalone application
sample. Select C# sample folder/bin/SchoolSystem.dll and all libraries in C# sample folder/lib.

Working with the Generating Code and Persistent Library
C# and C++ are both languages that built on the .NET framework and they both accomplish the same thing, just using different
language syntax. In this section, you will learn how to work with Generate Code and Persistent Library with C++ language.
•

Creating Object and Save to Database
1. From the menu bar, select File > Register Student to open the Add Student dialog box.

2.

Fill in the student information. Click OK to create the new Student record in School System.

8-5
Programmer’s Guide for .NET

3.

Chapter 8 – Programming in C++.NET

After that, the system creates the new Student Persistent object.
private: System::Void okButton_Click(System::Object *sender,
System::EventArgs *e)
{
if(loginIDTextBox->Text->Length == 0 || passwordTextBox->Text>Length == 0){
MessageBox::Show("Login id or password missing");
return;
}
PersistentTransaction *t =
SchoolSystemPersistentManager::Instance()->GetSession()>BeginTransaction();
try{       
if(userType == CREATE_TEACHER){
createdUser = TeacherFactory::CreateTeacher();
}else{
createdUser = StudentFactory::CreateStudent();
}
Source File : Standalone School System cpp RegisterDialog.h

4.

Set the student information from the text fields value to the Student Object
createdUser->set_Name(userNameTextBox->Text);
createdUser->set_Password(passwordTextBox->Text);
createdUser->set_LoginID(loginIDTextBox->Text);
if(dynamic_cast<Teacher*>(createdUser)){
dynamic_cast<Teacher*>(createdUser)->set_Email(emailTextBox->Text);
}
Source File : Standalone School System cpp RegisterDialog.h

5.

Call Save() method of Student Persistent Object and Commit() method of PersistentTransaction, then the
new Student object will be saved in database. If there are any errors occurred during the transaction, you can
call the Rollback() method to cancels the proposed changes in a pending database transaction
createdUser->Save();
this->set_CreatedUser(createdUser);
DialogResult = DialogResult::OK;
t->Commit();
Close();
}catch(Exception *ex){
Console::WriteLine(ex->InnerException->Message);
DialogResult = DialogResult::Cancel;
t->RollBack();
}
Source File : Standalone School System cpp RegisterDialog.h

8-6
Programmer’s Guide for .NET

•

Chapter 8 – Programming in C++.NET

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, the system shows all the available courses. The student can select and register
the course. If the user is teacher, the system shows the courses that are created by that teacher. The teacher can
update or delete the course information in system.
Student Login:

Teacher Login:

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.
void updateTreeView(void){
Course *courses[];
if(dynamic_cast<Student*>(currentUser)){
courses = CourseFactory::ListCourseByQuery(NULL, NULL);
}else{
courses = dynamic_cast<Teacher*>(currentUser)->courses>ToArray();
}
...
}
Source File : Standalone School System cpp SchoolSystemForm.h

•

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.

8-7
Programmer’s Guide for .NET

Chapter 8 – Programming in C++.NET

1.

From the menu bar, select User > Modify User Information to open the Modify User Information dialog
box.

2.

Enter new user information and click OK to update the User record.

3.

Update the information for the User object includes password, name and email address.
private: System::Void okButton_Click(System::Object *sender,
System::EventArgs *e)
{
if(nameTextBox->Text->Length == 0 || passwordTextBox->Text->Length ==
0){
MessageBox::Show("Missing name or password");
return;
}else{
PersistentTransaction *t =
SchoolSystemPersistentManager::Instance()->GetSession()>BeginTransaction();
try{
user->set_Name(nameTextBox->Text);
user->set_Password(passwordTextBox->Text);
if(dynamic_cast<Teacher*>(user)){
(dynamic_cast<Teacher*>(user))->set_Email(emailTextBox>Text);
}
user->Save();
DialogResult = DialogResult::OK;
t->Commit();
}catch(Exception *ex){
DialogResult = DialogResult::Cancel;
t->RollBack();
}
}
}
Source File : Standalone School System cppModifyUserDialog.h

8-8
Programmer’s Guide for .NET

•

Chapter 8 – Programming in C++.NET

Deleting Object in Database
Teacher can create courses for students to register and they can cancel the course in the system. They only need to
click delete button of the course then the course information will be deleted in the database and all its
relationships with register students will be removed.
1.

Teacher can create the course by clicking the Add Course button, fill in Course name and Description.

2.

Student can register the course by clicking the Register button.

3.

The teacher can view how many students registered his course, and he can delete the course in the system.

4.

Click Delete of a Course then it will trigger the deleteButton_Click() method.
private: System::Void deleteButton_Click(System::Object *sender,
System::EventArgs *e)
{
if(MessageBox::Show("Delete", "Delete", MessageBoxButtons::OKCancel)
== DialogResult::OK){
Source File : Standalone School System cppSchoolSystemform.h

8-9
Programmer’s Guide for .NET

5.

Chapter 8 – Programming in C++.NET

Call deleteAndDissociate() method to delete the course object and remove the relationships of student and
teacher with it.
PersistentTransaction *t =
SchoolSystemPersistentManager::Instance()->GetSession()>BeginTransaction();
try{
selectedNode->get_Course()->DeleteAndDissociate();
selectedNode->Remove();
t->Commit();
}catch(Exception *ex){
Console::WriteLine(ex->InnerException->Message);
t->RollBack();
}
}
}
Source File : Standalone School System cppSchoolSystemForm.h

Running the Application
To execute the C++ application, select Debug > Start (F5) on the menu bar Visual Studio .NET 2003.

8-10

More Related Content

What's hot

chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
It Academy
 
ASP.NET Session 3
ASP.NET Session 3ASP.NET Session 3
ASP.NET Session 3
Sisir Ghosh
 

What's hot (11)

Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
 
Intro.net
Intro.netIntro.net
Intro.net
 
Working in Visual Studio.Net
Working in Visual Studio.NetWorking in Visual Studio.Net
Working in Visual Studio.Net
 
Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4
 
Object Oriented Programming With C++
Object Oriented Programming With C++Object Oriented Programming With C++
Object Oriented Programming With C++
 
Wiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programmingWiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programming
 
ASP.NET Session 3
ASP.NET Session 3ASP.NET Session 3
ASP.NET Session 3
 
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
 
C# Tutorial MSM_Murach chapter-03-slides
C# Tutorial MSM_Murach chapter-03-slidesC# Tutorial MSM_Murach chapter-03-slides
C# Tutorial MSM_Murach chapter-03-slides
 

Viewers also liked

Viewers also liked (9)

Meeting minutes 9
Meeting minutes 9Meeting minutes 9
Meeting minutes 9
 
Seerat e rasoolearabipbuhpart1noorbushkhtawakali
Seerat e rasoolearabipbuhpart1noorbushkhtawakaliSeerat e rasoolearabipbuhpart1noorbushkhtawakali
Seerat e rasoolearabipbuhpart1noorbushkhtawakali
 
P.a
P.aP.a
P.a
 
Chapter 3f
Chapter 3fChapter 3f
Chapter 3f
 
Chapter 8f
Chapter 8fChapter 8f
Chapter 8f
 
Ahsan danish
Ahsan danishAhsan danish
Ahsan danish
 
Akhtarul iman
Akhtarul imanAkhtarul iman
Akhtarul iman
 
Cambridge english grammar_in_u
Cambridge english grammar_in_uCambridge english grammar_in_u
Cambridge english grammar_in_u
 
Akhtar shirani
Akhtar shiraniAkhtar shirani
Akhtar shirani
 

Similar to Dbva dotnet programmer_guide_chapter8

Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
meharikiros2
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
mwillmer
 
1. Introduction to C++ and brief history
1. Introduction to C++ and brief history1. Introduction to C++ and brief history
1. Introduction to C++ and brief history
Ahmad177077
 
Aae oop xp_02
Aae oop xp_02Aae oop xp_02
Aae oop xp_02
Niit Care
 
PT1420 File Access and Visual Basic .docx
PT1420 File Access and Visual Basic                      .docxPT1420 File Access and Visual Basic                      .docx
PT1420 File Access and Visual Basic .docx
amrit47
 

Similar to Dbva dotnet programmer_guide_chapter8 (20)

C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 
Creating simple component
Creating simple componentCreating simple component
Creating simple component
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
 
Bt0082 visual basic
Bt0082 visual basicBt0082 visual basic
Bt0082 visual basic
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
 
2310 b 03
2310 b 032310 b 03
2310 b 03
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
 
Dot net guide for beginner
Dot net guide for beginner Dot net guide for beginner
Dot net guide for beginner
 
Understanding C# in .NET
Understanding C# in .NETUnderstanding C# in .NET
Understanding C# in .NET
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
T2
T2T2
T2
 
The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 5 of 84The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 5 of 84
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
 
1. Introduction to C++ and brief history
1. Introduction to C++ and brief history1. Introduction to C++ and brief history
1. Introduction to C++ and brief history
 
Visual programming
Visual programmingVisual programming
Visual programming
 
Aae oop xp_02
Aae oop xp_02Aae oop xp_02
Aae oop xp_02
 
PT1420 File Access and Visual Basic .docx
PT1420 File Access and Visual Basic                      .docxPT1420 File Access and Visual Basic                      .docx
PT1420 File Access and Visual Basic .docx
 

More from Shakeel Mujahid

Seasons and months pictionary poster worksheet
Seasons and months pictionary poster worksheetSeasons and months pictionary poster worksheet
Seasons and months pictionary poster worksheet
Shakeel Mujahid
 
Ibtidai deeni nisab 2013
Ibtidai deeni nisab 2013Ibtidai deeni nisab 2013
Ibtidai deeni nisab 2013
Shakeel Mujahid
 
Seerat e rasoolearabipbuhpart1noorbushkhtawakali
Seerat e rasoolearabipbuhpart1noorbushkhtawakaliSeerat e rasoolearabipbuhpart1noorbushkhtawakali
Seerat e rasoolearabipbuhpart1noorbushkhtawakali
Shakeel Mujahid
 
Cambridge english grammar_in_u
Cambridge english grammar_in_uCambridge english grammar_in_u
Cambridge english grammar_in_u
Shakeel Mujahid
 

More from Shakeel Mujahid (20)

Ch 16 Al-Quran +923074302552
Ch 16                    Al-Quran +923074302552Ch 16                    Al-Quran +923074302552
Ch 16 Al-Quran +923074302552
 
Ch 2 Al-Quran +923074302552
Ch 2                   Al-Quran +923074302552Ch 2                   Al-Quran +923074302552
Ch 2 Al-Quran +923074302552
 
Ch 11 Al-Quran +923074302552
Ch 11               Al-Quran +923074302552Ch 11               Al-Quran +923074302552
Ch 11 Al-Quran +923074302552
 
Ch 5 Al-Quran +923074302552
Ch 5               Al-Quran +923074302552Ch 5               Al-Quran +923074302552
Ch 5 Al-Quran +923074302552
 
Ch 3 Al-Quran +923074302552
Ch 3               Al-Quran +923074302552Ch 3               Al-Quran +923074302552
Ch 3 Al-Quran +923074302552
 
The mouse-that-knew
The mouse-that-knewThe mouse-that-knew
The mouse-that-knew
 
Seasons and months pictionary poster worksheet
Seasons and months pictionary poster worksheetSeasons and months pictionary poster worksheet
Seasons and months pictionary poster worksheet
 
Tot
TotTot
Tot
 
Unit 1
Unit 1Unit 1
Unit 1
 
Salam machli shahri
Salam machli shahriSalam machli shahri
Salam machli shahri
 
Ibtidai deeni nisab 2013
Ibtidai deeni nisab 2013Ibtidai deeni nisab 2013
Ibtidai deeni nisab 2013
 
Sameer
SameerSameer
Sameer
 
Proverbs123456
Proverbs123456Proverbs123456
Proverbs123456
 
Proverbs123
Proverbs123Proverbs123
Proverbs123
 
Proverbs
ProverbsProverbs
Proverbs
 
Shazaib
ShazaibShazaib
Shazaib
 
Unit 1
Unit 1Unit 1
Unit 1
 
Seerat e rasoolearabipbuhpart1noorbushkhtawakali
Seerat e rasoolearabipbuhpart1noorbushkhtawakaliSeerat e rasoolearabipbuhpart1noorbushkhtawakali
Seerat e rasoolearabipbuhpart1noorbushkhtawakali
 
Salam machli shahri
Salam machli shahriSalam machli shahri
Salam machli shahri
 
Cambridge english grammar_in_u
Cambridge english grammar_in_uCambridge english grammar_in_u
Cambridge english grammar_in_u
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

Dbva dotnet programmer_guide_chapter8

  • 2. Programmer’s Guide for .NET Chapter 8 – Programming in C++.NET Chapter 8 - Programming in C++ .NET DB Visual ARCHITECT (DB-VA) can generate C#.NET source code so you can implement your application by C# programming language directly but you can also choose another language (VB.NET or C++) based on your taste in the .NET Framework. DB-VA generates DLL file and persistent libraries that can be referenced by .NET projects of language other than C#. In this chapter: • • • • • • Introduction Generating DLL file Creating C++ Project Adding Referenced Project Working with the Generating Code and Persistent Library Running the Application Introduction C++ is an Object-Oriented Programming (OOP) language that is viewed by many as the best language for creating large-scale applications. The .NET Framework contains a virtual machine called Common Intermediate Language (CIL). Simply put, programs are compiled to produce CIL and the CIL is distributed to user to run on a virtual machine. C++, VB.NET, C# compilers are available from Microsoft for creating CIL. In DB-VA you can generate C# persistent source code and DLL file, so you can reference the DLL file and persistent library in Visual Studio .NET 2003 and develop the C++ application. In the following section, you will develop a C++ application. The application is exactly same as the one in Chapter 4 – Developing Standalone .NET Application sample, but this time you use C++ instead of C# for development. You need to download the Chapter 4 sample application because it contains DLL file and persistent libraries for your C++ project. Generating DLL File 1. From the menu bar, select Tools > Object-Relational Mapping (ORM) > Generate Database… to open the Database Code Generate dialog box. 2. Check the Compile to DLL option to generate the DLL file. • Compile to DLL By checking this option, DB-VA will generate DLL files which can be referenced by .NET projects of language other than C#. DB-VA generates batch file for the DLL file at the same time. You can modify the configuration file (hibernate.cfg.xml) manually and use the batch file to recompile and build an up-to-date DLL file for referenced project. 8-2
  • 3. Programmer’s Guide for .NET Chapter 8 – Programming in C++.NET Creating C++ Project 1. 2. Open Microsoft Visual Studio .NET 2003. Select File > New > Project … from the menu. 3. Select Project Types as Visual C++ Projects and Templates as Windows Form Application (.NET) and Location for the new application. 4. The School System Project is created. 5. Right click Standalone School System cpp Project, select Add > Add New Item… from the popup menu. 8-3
  • 4. Programmer’s Guide for .NET Chapter 8 – Programming in C++.NET 6. Select Category as Visual C++, Template Windows Form (.NET) and enter the name for the form called “SchoolSystemForm”. This is the start point for the application. 7. Append the following content to the SchoolSystemForm.cpp file (Source files/SchoolSystemForm.cpp). This is the main method for C++ Application to execute. #include <windows.h> using namespace StandaloneSchoolSystemcpp; int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA; Application::Run(new SchoolSystemForm()); return 0; } 8. Remove the Form1.cpp and Form1.h files. Adding Referenced Project 1. 8-4 Right click References under the Standalone School System C++ project and select Add Reference…. Reference the C# example DDL file and persistent libraries for developing the C++ application.
  • 5. Programmer’s Guide for .NET 2. Chapter 8 – Programming in C++.NET Click Browse… on the Add Reference dialog box to select the folder of the downloaded C# standalone application sample. Select C# sample folder/bin/SchoolSystem.dll and all libraries in C# sample folder/lib. Working with the Generating Code and Persistent Library C# and C++ are both languages that built on the .NET framework and they both accomplish the same thing, just using different language syntax. In this section, you will learn how to work with Generate Code and Persistent Library with C++ language. • Creating Object and Save to Database 1. From the menu bar, select File > Register Student to open the Add Student dialog box. 2. Fill in the student information. Click OK to create the new Student record in School System. 8-5
  • 6. Programmer’s Guide for .NET 3. Chapter 8 – Programming in C++.NET After that, the system creates the new Student Persistent object. private: System::Void okButton_Click(System::Object *sender, System::EventArgs *e) { if(loginIDTextBox->Text->Length == 0 || passwordTextBox->Text>Length == 0){ MessageBox::Show("Login id or password missing"); return; } PersistentTransaction *t = SchoolSystemPersistentManager::Instance()->GetSession()>BeginTransaction(); try{        if(userType == CREATE_TEACHER){ createdUser = TeacherFactory::CreateTeacher(); }else{ createdUser = StudentFactory::CreateStudent(); } Source File : Standalone School System cpp RegisterDialog.h 4. Set the student information from the text fields value to the Student Object createdUser->set_Name(userNameTextBox->Text); createdUser->set_Password(passwordTextBox->Text); createdUser->set_LoginID(loginIDTextBox->Text); if(dynamic_cast<Teacher*>(createdUser)){ dynamic_cast<Teacher*>(createdUser)->set_Email(emailTextBox->Text); } Source File : Standalone School System cpp RegisterDialog.h 5. Call Save() method of Student Persistent Object and Commit() method of PersistentTransaction, then the new Student object will be saved in database. If there are any errors occurred during the transaction, you can call the Rollback() method to cancels the proposed changes in a pending database transaction createdUser->Save(); this->set_CreatedUser(createdUser); DialogResult = DialogResult::OK; t->Commit(); Close(); }catch(Exception *ex){ Console::WriteLine(ex->InnerException->Message); DialogResult = DialogResult::Cancel; t->RollBack(); } Source File : Standalone School System cpp RegisterDialog.h 8-6
  • 7. Programmer’s Guide for .NET • Chapter 8 – Programming in C++.NET 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, the system shows all the available courses. The student can select and register the course. If the user is teacher, the system shows the courses that are created by that teacher. The teacher can update or delete the course information in system. Student Login: Teacher Login: 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. void updateTreeView(void){ Course *courses[]; if(dynamic_cast<Student*>(currentUser)){ courses = CourseFactory::ListCourseByQuery(NULL, NULL); }else{ courses = dynamic_cast<Teacher*>(currentUser)->courses>ToArray(); } ... } Source File : Standalone School System cpp SchoolSystemForm.h • 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. 8-7
  • 8. Programmer’s Guide for .NET Chapter 8 – Programming in C++.NET 1. From the menu bar, select User > Modify User Information to open the Modify User Information dialog box. 2. Enter new user information and click OK to update the User record. 3. Update the information for the User object includes password, name and email address. private: System::Void okButton_Click(System::Object *sender, System::EventArgs *e) { if(nameTextBox->Text->Length == 0 || passwordTextBox->Text->Length == 0){ MessageBox::Show("Missing name or password"); return; }else{ PersistentTransaction *t = SchoolSystemPersistentManager::Instance()->GetSession()>BeginTransaction(); try{ user->set_Name(nameTextBox->Text); user->set_Password(passwordTextBox->Text); if(dynamic_cast<Teacher*>(user)){ (dynamic_cast<Teacher*>(user))->set_Email(emailTextBox>Text); } user->Save(); DialogResult = DialogResult::OK; t->Commit(); }catch(Exception *ex){ DialogResult = DialogResult::Cancel; t->RollBack(); } } } Source File : Standalone School System cppModifyUserDialog.h 8-8
  • 9. Programmer’s Guide for .NET • Chapter 8 – Programming in C++.NET Deleting Object in Database Teacher can create courses for students to register and they can cancel the course in the system. They only need to click delete button of the course then the course information will be deleted in the database and all its relationships with register students will be removed. 1. Teacher can create the course by clicking the Add Course button, fill in Course name and Description. 2. Student can register the course by clicking the Register button. 3. The teacher can view how many students registered his course, and he can delete the course in the system. 4. Click Delete of a Course then it will trigger the deleteButton_Click() method. private: System::Void deleteButton_Click(System::Object *sender, System::EventArgs *e) { if(MessageBox::Show("Delete", "Delete", MessageBoxButtons::OKCancel) == DialogResult::OK){ Source File : Standalone School System cppSchoolSystemform.h 8-9
  • 10. Programmer’s Guide for .NET 5. Chapter 8 – Programming in C++.NET Call deleteAndDissociate() method to delete the course object and remove the relationships of student and teacher with it. PersistentTransaction *t = SchoolSystemPersistentManager::Instance()->GetSession()>BeginTransaction(); try{ selectedNode->get_Course()->DeleteAndDissociate(); selectedNode->Remove(); t->Commit(); }catch(Exception *ex){ Console::WriteLine(ex->InnerException->Message); t->RollBack(); } } } Source File : Standalone School System cppSchoolSystemForm.h Running the Application To execute the C++ application, select Debug > Start (F5) on the menu bar Visual Studio .NET 2003. 8-10