- 最關鍵的,UI層只能做爲一個外殼,不能包含任何業務邏輯(BizLogic)的處理過程。只有少許(或者沒有)SQL語句或者存儲過程的調用,而且這些語句保證不會修改數據。
- 設計時應該從BLL出發,而不是UI出發。BLL層在API上應該實現全部BizLogic(以面向對象的方式)。若是把UILayer拿掉,項目還能在Interface/API的層次上提供全部功能)。
- 無論數據層是一個簡單的SqlHelper也好,仍是帶有Mapping過的Classes也好,應該在必定的抽象程度上作到系統無關(DAL能夠移植到其餘相似環境的項目)。
- 無論使用COM+(Enterprise Service),仍是Remoting,仍是WebService之類的遠程對象技術,無論部署的時候是否是真的分別部署到不一樣的服務器上,最起碼在設計的時候要作這樣的考慮,更遠的,還得考慮多臺服務器經過負載均衡做集羣(三個模塊,能夠分別運行於不一樣的服務器)。
1,結構清晰、耦合度低,html
2,可維護性高,可擴展性高;程序員
3,利於開發任務同步進行;容易適應需求變化數據庫
一、有時會致使級聯的修改。這種修改尤爲體如今自上而下的方向。若是在表示層中須要增長一個功能,爲保證其設計符合分層式結構,可能須要在相應的業務邏輯層和數據訪問層中都增長相應的代碼編程
二、增長了代碼量,增長了工做量,增長了複雜度。服務器
MVC則是表現層的框架模式。用於解除業務邏輯和視圖之間的耦合。從而易於擴展,便於測試。架構
在MVC中Model應該包含2部分功能,一部分是處理業務邏輯,一部分是提供View顯示的數據。app
控制器變得依賴信息數據中心或數據庫,對象將間接地經過控制器的action耦合在一塊兒負載均衡
MVC最先的定義畢竟是79年提出的,到如今GUI編程環境,業務複雜程度都有了很大改變。當採用MVC模式設計UI應用時,通常會根據開發框架的特色來對Model,View和Contoller設置一個明確的界限,同時爲它們之間的交互制定一個更加嚴格的規則。
public
class
Employee
{
public
string
Id {
get
;
private
set
; }
public
string
Name {
get
;
private
set
; }
public
string
Gender {
get
;
private
set
; }
public
DateTime BirthDate {
get
;
private
set
; }
public
string
Department {
get
;
private
set
; }
public
Employee(
string
id,
string
name,
string
gender,DateTime birthDate,
string
department)
{
Id = id;
Name = name;
Gender = gender;
BirthDate = birthDate;
Department = department;
}
}
public
class
EmployeeRespository
{
private
static
IList<Employee> employees;
static
EmployeeRespository()
{
employees =
new
List<Employee>()
{
new
Employee(
"001"
,
"張三"
,
"男"
,
new
DateTime(1981,8,24),
"銷售部"
),
new
Employee(
"002"
,
"李四"
,
"男"
,
new
DateTime(1981,8,24),
"人事部"
),
new
Employee(
"003"
,
"王五"
,
"女"
,
new
DateTime(1981,8,24),
"人事部"
)
};
}
public
IEnumerable<Employee> GetEmployees(
string
department =
""
)
{
if
(
string
.IsNullOrEmpty(department))
{
return
employees;
}
return
employees.Where(e => e.Department == department).ToArray();
}
}
public
class
EmployeePresenter
{
public
IEmployeeView View {
get
;
private
set
; }
public
EmployeeRespository Respository {
get
;
private
set
; }
public
EmployeePresenter(IEmployeeView view)
{
this
.View = view;
this
.Respository =
new
EmployeeRespository();
this
.View.DepartmentSelected += OnDepartmentSelected;
}
public
void
Initialize()
{
IEnumerable<Employee> employees =
this
.Respository.GetEmployees();
this
.View.BindEmployees(employees);
string
[] departments
=
new
string
[]{
""
,
"銷售部"
,
"採購部"
,
"人事部"
};
this
.View.BindDepartments(departments);
}
protected
void
OnDepartmentSelected(
object
sender, DepartmentSelectedEventArgs args)
{
string
department = args.Department;
var
employees =
this
.Respository.GetEmployees(department);
this
.View.BindEmployees(employees);
}
}
public
interface
IEmployeeView
{
void
BindEmployees(IEnumerable<Employee> employees);
void
BindDepartments(IEnumerable<
string
> departments);
event
EventHandler<DepartmentSelectedEventArgs> DepartmentSelected;
}
public
class
DepartmentSelectedEventArgs : EventArgs
{
public
string
Department {
get
;
private
set
; }
public
DepartmentSelectedEventArgs(
string
department)
{
this
.Department = department;
}
}
public
partial
class
Form1 : Form, IEmployeeView
{
private
EmployeePresenter Presenter{
get
;
private
set
; }
public
Form1()
{
InitializeComponent();
Presenter =
new
EmployeePresenter(
this
);
Presenter.Initialize();
}
public
event
EventHandler<DepartmentSelectedEventArgs> DepartmentSelected;
public
void
BindDepartments(IEnumerable<
string
> departments)
{
this
.comboBox1.DataSource = departments;
}
public
void
BindEmployees(IEnumerable<Employee> employees)
{
this
.listBox1.DataSource = employees;
this
.listBox1.DisplayMember =
"Name"
;
}
private
void
comboBox1_SelectedIndexChanged(
object
sender, EventArgs e)
{
string
department = (
string
)
this
.comboBox1.SelectedItem;
DepartmentSelectedEventArgs eventArgs =
new
DepartmentSelectedEventArgs(department);
DepartmentSelected?.Invoke(sender, eventArgs);
}
}
WebForm實現的UI界面
public
partial
class
Default : Page, IEmployeeView
{
public
EmployeePresenter Presenter {
get
;
private
set
; }
public
event
EventHandler<DepartmentSelectedEventArgs> DepartmentSelected;
public
Default()
{
this
.Presenter =
new
EmployeePresenter(
this
);
}
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!
this
.IsPostBack)
{
this
.Presenter.Initialize();
}
}
protected
void
ButtonSearch_Click(
object
sender, EventArgs e)
{
string
department =
this
.DropDownListDepartments.SelectedValue;
DepartmentSelectedEventArgs eventArgs =
new
DepartmentSelectedEventArgs(department);
if
(
null
!= DepartmentSelected)
{
DepartmentSelected(
this
, eventArgs);
}
}
public
void
BindEmployees(IEnumerable<Employee> employees)
{
this
.GridViewEmployees.DataSource = employees;
this
.GridViewEmployees.DataBind();
}
public
void
BindDepartments(IEnumerable<
string
> departments)
{
this
.DropDownListDepartments.DataSource = departments;
this
.DropDownListDepartments.DataBind();
}
}
在MVP裏,能夠根據User Story來首先設計和開發Presenter。在這個過程當中,View是很簡單的,可以把信息顯示清楚就能夠了。在後面,根據須要再隨便更改View, 而對Presenter沒有任何的影響了。框架
若是要實現的UI比較複雜,並且相關的顯示邏輯還跟Model有關係,能夠在View和 Presenter之間放置一個Adapter。由這個 Adapter來訪問Model和View,避免二者之間的關聯。而同時,由於Adapter實現了View的接口,從而能夠保證與Presenter之 間接口的不變。這樣就能夠保證View和Presenter之間接口的簡潔,又不失去UI的靈活性。函數
轉載自:http://www.cnblogs.com/wj033/p/5812938.html