Creating methods: Write constructors A constructor is a special method that used to initialize the properties of the object A constructor is invoked when the object gets instantiated
Trang 1Building multitiers program
Week 9
Trang 21. Review OOP
2. Three-tier application
3. Implementing 3-tier
Trang 3Create class
Add new class: menu Project->Add Class
Create properties for a class
Create methods for a class
Create events for class
Slide 3
Trang 4Creating properties for a class
Declare all variables in a class as private (encapsulation)
private properties must be assigned values and can be get values
through get/set
public dataType myProperty {
get { return propertyName; }
set { propertyName = value; } }
Trang 5Creating methods: Write constructors
A constructor is a special method that used to initialize the properties of the object
A constructor is invoked when the object gets instantiated
Note to write a constructor:
the same
A constructor does not return value, not even void
A class can have multiple constructors (overloaded
constructors)
public ClassName (parameterList)
{ }
Trang 6Instantiating an object
To instantiate an object, using the new keyword
Example:
Lop l = new Lop();
Lop c;
c = new Lop(“NCTH2K”, “Cao dang nghe 2K”);
ClassName object = new ClassName (…);
ClassName object;
object = new ClassName (…);
Trang 7Using properties, methods of a class
Call methods or properties of a class
Using dot operator
get: variable = object.propertyName
set: object.propertyName = variable
Example:
SinhVien sv = new SinhVien();
sv.HoTen = “Nguyen Van An”;
sv.DiemLT = 5;
sv.DiemTH = 10;
lblTB.Text = sv.DiemTB().ToString();
MessageBox.Show(sv.HoTen + “ có ĐTB = ” + sv.DiemTB());
Slide 7
Trang 81. Review OOP
2. Three-tier application
3. Implementing 3-tier
Trang 92 Three-tier application
2.1 What is a 3-tier architecture?
2.2 What is the need for dividing the code in 3-tiers?
2.3 Designing 3-tier
Slide 9
Trang 102.1 What is a 3-tier architecture?
Three-tier (layer) is a client-server
architecture in which the user
interface, business rules and data
access are developed and maintained
as independent modules
There are:
Tier 1: Presentation tier
Tier 2: Business Logic tier
Tier 3: Data Access tier
Trang 112- 11
Trang 122.2 What is the need for dividing the code in 3-tiers?
Reusability
E.g if we have a module that handles adding, updating, deleting and finding customers in the system, we can use it in any
other project that might involve maintaining customers
Transformation of the system is easy
there shouldn’t be any changes required in the business layer component and in the GUI component.
Change management of the system is easy
to update the business logic component
Trang 132.3 Designing 3-tiers:
Data Access layer
The Data Access Logic component provide methods for querying and updating data (relational databases, file
system,…)
Each Data Access Logic component typically provides
methods for inserting, deleting, updating and retrieving
operations relating to a specific business entity in the
application
When the application contains multiple Data Access Logic components, you may use a generic data access component
to manage database connections, execute commands,…
Trang 142.3 Designing 3-tiers:
Data Access layer
DACommon
- conn : OleDbConnection
+ getTable( sql: string ) : DataTable
+ Update( tableUpdate : DataTable , tableName : string ):
void
Customers
- dacommon : DACommon
+ getTable() : DataTable
+ insert (fields in database) : bool
+ delete (primary fields in database) : bool
+ update (fields in database) : bool
+ find (some fields in database) : DataTable
Products
- dacommon : DACommon + getTable() : DataTable + insert (fields in database) : bool + delete (primary fields in database) : bool + update (fields in database) : bool
+ find (some fields in database) : DataTable
Trang 152.3 Designing 3-tiers:
Business Logic layer
The Business Logic layer contains classes that handle the data
Functionality
To call Data Access Logic components to retrieve and/or update application data
Calculations
Validation to enforce business rules
Trang 162.3 Designing 3-tiers:
Presentation layer
The Presentation layer contains the components that are required to enable user interaction with the application User Interface
Functionality
Receive and send data to the business logic tier
Restrict the types of input a user can enter
information provided by the user controls to values needed
by the underlying components
Trang 171. Review OOP
2. Three-tier application
3. Implementing three-tier
Window Application
Class Library
Slide 17