用VB.NET創(chuàng)建一個(gè)三層應(yīng)用程序的例子……
2024-07-10 13:02:24
供稿:網(wǎng)友
1. 概論:
本文將介紹如何創(chuàng)建一個(gè)三層應(yīng)用程序,并且將介紹如何創(chuàng)建一個(gè)web service服務(wù)。
ado.net創(chuàng)建windows三層結(jié)構(gòu)應(yīng)用程序的體系架構(gòu)如下圖所示:
該結(jié)構(gòu)分三個(gè)層次:表示層、業(yè)務(wù)層、數(shù)據(jù)層。
數(shù)據(jù)層:代表物理數(shù)據(jù)庫(kù)。
業(yè)務(wù)層:負(fù)責(zé)數(shù)據(jù)層與表示層之間的數(shù)據(jù)傳輸。
表示層:應(yīng)用程序的客戶端,它通過(guò)業(yè)務(wù)層來(lái)訪問(wèn)數(shù)據(jù)庫(kù)。
表示層所操作的是駐留在內(nèi)存中的本地?cái)?shù)據(jù),當(dāng)需要更新數(shù)據(jù)庫(kù)數(shù)據(jù)時(shí),要通過(guò)業(yè)務(wù)層提供的更新方法實(shí)現(xiàn)。這樣可以大大提高應(yīng)用程序的性能,而且,什么時(shí)候更新數(shù)據(jù)完全由你決定,提高了編程的靈活性。
2.實(shí)例:
這里我們具體做一個(gè)實(shí)例來(lái)看看如何用vb.net創(chuàng)建三層結(jié)構(gòu)的應(yīng)用程序。
數(shù)據(jù)庫(kù):我們選擇sql server 的northwind數(shù)據(jù)庫(kù)。
業(yè)務(wù)層:我們創(chuàng)建一個(gè)webservice作為中間層。(需要安裝iis服務(wù))
表示層:我們寫(xiě)一個(gè)windows form
第一步:創(chuàng)建webservice。
具體步驟如下:
1. 新建一個(gè)項(xiàng)目,選擇asp.net web服務(wù),命名為:”webservice for 業(yè)務(wù)層”。
2. 添加兩個(gè)sql dataadapter,一個(gè)為customer_da,它指向northwind數(shù)據(jù)庫(kù)的customers表,另一個(gè)為order_da,指向northwind數(shù)據(jù)庫(kù)的orders表。
3. 然后生成一個(gè)typed dataset(選擇“數(shù)據(jù)”菜單的“生成數(shù)據(jù)集”),命名為:super_ds.
4. 數(shù)據(jù)庫(kù)連接已經(jīng)完成,下一步我們將考慮它與表示層之間的通信,這里我們定義兩個(gè)方法。一個(gè)為:get_dataset,它返回一個(gè)super_ds類型的數(shù)據(jù)集,另一個(gè)為:update_dataset,它負(fù)責(zé)更新數(shù)據(jù)庫(kù)數(shù)據(jù), 方法代碼如下:
<webmethod()> public function get_dataset() as super_ds
customer_da.fill(super_ds1.customers)
order_da.fill(super_ds1.orders)
return super_ds1
end function
<webmethod()> public sub update_dataset()
super_ds1.acceptchanges()
end sub
你可以運(yùn)行測(cè)試一下你建立的這個(gè)webservice。它將提供兩個(gè)方法。返回的dataset是以xml表示的。
業(yè)務(wù)層的完整代碼如下:
imports system.web.services
public class service1
inherits system.web.services.webservice
‘web services designer generated code…….
<webmethod()> public function get_dataset() as super_ds
customer_da.fill(super_ds1.customers)
order_da.fill(super_ds1.orders)
return super_ds1
end function
<webmethod()> public sub update_dataset()
super_ds1.acceptchanges()
end sub
' web service example
' the helloworld() example service returns the string hello world.
' to build, uncomment the following lines then save and build the project.
' to test this web service, ensure that the .asmx file is the start page
' and press f5.
'
'<webmethod()> public function helloworld() as string
' helloworld = "hello world"
' end function
end class
第二步:創(chuàng)建表示層
具體步驟如下:
1. 新建一個(gè)windows應(yīng)用程序,命名為:“windows form for 表示層”。
2. 在窗體上添加一個(gè)datagrid,一個(gè)button,button1的text為“l(fā)oad”,作用是:從業(yè)務(wù)層讀取數(shù)據(jù)。
3. 在解決方案窗體中添加web 引用,將我們自己建立的web service for 業(yè)務(wù)層引入到當(dāng)前項(xiàng)目中。
4. 向button1的click事件添加如下代碼:
dim customer_ds as new localhost.super_ds()
dim ser1 as new localhost.service1()
customer_ds.merge(ser1.get_dataset)
datagrid1.datasource = customer_ds
這里我們調(diào)用了web service的get_dataset函數(shù),update_dataset方法的調(diào)用與此完全相同。
表示層的完整代碼如下:
imports data_access_表示層
public class form1
inherits system.windows.forms.form
#region " windows form designer generated code "
public sub new()
mybase.new()
'this call is required by the windows form designer.
initializecomponent()
'add any initialization after the initializecomponent() call
end sub
'form overrides dispose to clean up the component list.
protected overloads overrides sub dispose(byval disposing as boolean)
if disposing then
if not (components is nothing) then
components.dispose()
end if
end if
mybase.dispose(disposing)
end sub
friend withevents button1 as system.windows.forms.button
friend withevents button2 as system.windows.forms.button
friend withevents button3 as system.windows.forms.button
friend withevents client_dataset as data_access_表示層.localhost.super_ds
friend withevents datagrid1 as system.windows.forms.datagrid
'required by the windows form designer
private components as system.componentmodel.container
'note: the following procedure is required by the windows form designer
'it can be modified using the windows form designer.
'do not modify it using the code editor.
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
me.button1 = new system.windows.forms.button()
me.button2 = new system.windows.forms.button()
me.button3 = new system.windows.forms.button()
me.client_dataset = new data_access_表示層.localhost.super_ds()
me.datagrid1 = new system.windows.forms.datagrid()
ctype(me.client_dataset, system.componentmodel.isupportinitialize).begininit()
ctype(me.datagrid1, system.componentmodel.isupportinitialize).begininit()
me.suspendlayout()
'
'button1
'
me.button1.location = new system.drawing.point(88, 360)
me.button1.name = "button1"
me.button1.tabindex = 0
me.button1.text = "load"
'
'button2
'
me.button2.location = new system.drawing.point(232, 360)
me.button2.name = "button2"
me.button2.tabindex = 1
me.button2.text = "update"
'
'button3
'
me.button3.location = new system.drawing.point(376, 360)
me.button3.name = "button3"
me.button3.tabindex = 2
me.button3.text = "clear"
'
'client_dataset
'
me.client_dataset.datasetname = "client_dataset"
me.client_dataset.locale = new system.globalization.cultureinfo("zh-cn")
me.client_dataset.namespace = "http://www.tempuri.org/customerds.xsd"
'
'datagrid1
'
me.datagrid1.datamember = ""
me.datagrid1.location = new system.drawing.point(40, 56)
me.datagrid1.name = "datagrid1"
me.datagrid1.size = new system.drawing.size(480, 264)
me.datagrid1.tabindex = 3
'
'form1
'
me.autoscalebasesize = new system.drawing.size(6, 14)
me.clientsize = new system.drawing.size(568, 429)
me.controls.addrange(new system.windows.forms.control() {me.datagrid1, me.button3, me.button2, me.button1})
me.name = "form1"
me.text = "form1"
ctype(me.client_dataset, system.componentmodel.isupportinitialize).endinit()
ctype(me.datagrid1, system.componentmodel.isupportinitialize).endinit()
me.resumelayout(false)
end sub
#end region
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
dim customer_ds as new localhost.super_ds()
dim ser1 as new localhost.service1()
customer_ds.merge(ser1.get_dataset)
datagrid1.datasource = customer_ds
end sub
end class
總結(jié):可見(jiàn),表示層窗體上完全沒(méi)有數(shù)據(jù)庫(kù)連接控件,它與數(shù)據(jù)庫(kù)的連接任務(wù)是通過(guò)業(yè)務(wù)層來(lái)完成的,這樣,程序的結(jié)構(gòu)更加清晰,當(dāng)然業(yè)務(wù)層的實(shí)現(xiàn)也可以用其他方法,比如:寫(xiě)一個(gè)自己的類來(lái)完成與數(shù)據(jù)庫(kù)的數(shù)據(jù)傳輸。