1. LAYERED ARCH.
2. MVC
3. รู้จักกับ SOA
What is a layered architecture ?
A multilayered (software) architecture is using different layers for allocating the responsibilities of an application.
Well, this concept is not new, and it applies also to most real world organizations. Most of them work more or less the same way: they divide the tasks that are required to come up with a finished product (or service).
As an example, think about the way a restaurant works. The main actors are :
- the customer
- the waiter
- the Chef
They all have different responsibilities that can be briefly described as below:
The customer:
- decides what he’d like to eat
- eats
- asks for the bill
- pays
The waiter:
- takes the order
- forward it to the Chef
- serves the meal
- brings the bill
- clean the table
The Chef:
- cooks (or gets the food out of the freezer)
It makes sense to have the waiter taking the customer’s order and asking the Chef to cook the desired meal. You wouldn’t let the customer go into the kitchen and take whatever he feels like having at anytime
Why a layered architecture ?
Now that you know what a layered architecture is, the reasons why it is a good idea to build your site / application following those principles must be pretty obvious.
- clear separation of responsabilities — each layer being only responsible for itself
- exposed workflow — as opposed to the spaghetti code we’ve all see way too many times
- ability to replace one or several layers implementation with minimum effort and side effects.
Anatomy of a layered architecture
In practice, you need to know what the 3 main layers are:
- the Data Access layer
- the Business Logic layer
- the Graphical User Interface layer
Data Access Layer — DAL
The Data Access Layer — DAL — is as its name implies the layer at which the data is processed. It’s typical CRUD operations. Create, Retrieve, Update and Delete.
The DAL is composed of one or many Data Access Object (DAO)
The DAL is the lowest layer of our application. On top of it, is — in this simple example — the Business Logic Layer.
Business Logic Layer — BLL
The Business Logic Layer — BLL — is obviously where all the Business Logic is implemented.
Business logic is a non-technical term generally used to describe the functional algorithms that handle information exchange between a database and a user interface.
For small applications, the BL is pretty basic if / else clauses that determine which functions should be called. It might look something like this:
if user.firstname.lower()[:1] == "j":
#do something with user
else:
#do something else
Most of the time, we’ll have the basic CRUD operations. The Usermanager class should look like this:
class UserManager:
def create(self,user):
pass
def retrieve(self,user):
pass
def update(self,user):
pass
def delete(self,user):
pass
#insert additional methods here
In order to talk to the DAL, the UserManager class has to instantiate a new UserDAO class.
ตัวอย่าง Business logic (UserManager class) เรียกใช้ UserDAO
class UserManager:
def __init__(self):
self.dao = UserDAO()
def retrieve(self,user)
#perform some business logic here if needed
userRetrieved = self.dao.retrieve(user)
#perform additional business logic here if needed
return userRetrieved
This is how the BLL and DAL are linked together. The UserManager calls the appropriate UserDAO method after having performed its Business Logic.
Graphical User Interface — GUI
The Graphical User Interface — GUI — is the only visible part of the application. It may have several representations:
- html / css (+ javascript)
- flash / flex
- Silverlight / WPF
- Cocoa / Cocoa touch
- …
In short, the goal of the GUI is to collect the input data and pass it along to the Business Logic Layer and wait for the enriched data or success/failure message to come back, in order to provide a visual feedback that the action has been processed (flashing message, redirection to another page …).
How does that happen ? First, the GUI has to create a new BO with the input data:
userFromGUI = User("Jon","Doe","john.doe@example.com")
Then it passes it along to the appropriate method of the manager, in our case, the UserManager from the BLL userManager = UserManager()
(เรียกใช้ BLL และใน BLL ค่อยไปเรียกใช้ DAL อีกที เพื่อทำ CRUD กับ Data)
#in this case we expect a User to be returned
userFromDB = userManager.retrieve(userFromGUI)
Then we process the data we’ve received: (แสดงผลผ่าน GUI)
if userFromDB is not None:
print "Welcome back %s" % (userFromDB.firstname)
else:
pint "Looks like we've no information about you."
In its simplest aspect, this is all the GUI has to do. Set the data, pass it along, and behave according to the data that has been returned.
__________________________________________________________________________________________________
Presentation, Business, and Data Layers
At the highest and most abstract level, the logical architecture view of any system can be considered as a set of cooperating components grouped into layers. The Figure shows a simplified, high level representation of these layers and their relationships with users.
MVC (Movel/View/Controller) vs 3-Tier
MVC ที่กำลังฮิตกันอยู่ช่วงนี้ มันเป็นอย่างเดียวกันกับ 3-Tier หรือเปล่า…?
ตอบ 3-Tier กับ MVC เป็นคนละเรื่องกัน
สถาปัตยกรรมแบบ 3-Tier
เป็นการออกแบบสถาปัตยกรรมของระบบ (System Architecture) โดยมีคอนเซปต์พื้นฐานคือการแบ่งแยกหน้าที่ความรับผิดชอบของแต่ละ tier ให้เด็ดขาดจากกัน ไม่ว่าจะเป็น…
- Presentation Tier รับผิดชอบในการแสดงผลด้าน UI
- Business Logic Tier รับผิดชอบในการประมวลผลด้าน business logic และ
- Data Tier ดูแลในส่วนการจัดการฐานข้อมูล (ในกรณีของ SOA จะมี Service Tier เพิ่มเข้ามาเพื่อดูแลในส่วนการติดต่อสื่อสารกับ service อื่นๆ ซึ่งมีได้มากมายในระบบ)
MVC
เป็น design pattern สำหรับ Presentation Tier โดยสถาปัตยกรรมของ MVC คือ การแบ่งหน้าที่ความรับผิดชอบของ Presentation Tier โดยแยกส่วนระหว่าง View กับ Data อย่างชัดเจน
- Model คือชั้นข้อมูลที่ได้รับการส่งต่อจาก Business Logic Tier
- Controller คือชั้นควบคุม ที่เป็นตัวกลางระหว่าง User, View และ Model จะทำการควบคุมการทำงานของ Presentation Tier โดยการรับ Model และส่งไปที่หน่วยแสดงผล (View) ที่เหมาะสม รวมถึงรับ activity จาก User ด้วย
- View คือชั้นการแสดงผล ที่ไม่ได้สนใจโครงสร้างข้อมูล มีไว้เพื่อแสดงผลตามเงื่อนไขของ Controller เท่านั้น
ทำไมต้องทำ Software Architecture
What's SOA ?
SOA's concept
https://www.youtube.com/watch?v=jL1oVENiYT8
Understanding SOA - Part 1 - Architecture
https://www.youtube.com/watch?v=0hyXOuvyq2Q
Understanding SOA - Part 2 - Technologies
https://www.youtube.com/watch?v=zNvyCUO0dyw