2008-10-24 48 views
1

我們有一個高度專業化的DAL,它位於我們的數據庫之上。我們的應用程序需要使用此DAL才能正確操作此數據庫。如何構建WebService暴露的DAL?

生成的DAL(位於某些自定義基類)具有各種'Rec'類(Table1Rec,Table2Rec),每個類表示給定表的記錄結構。

這裏是一個示例僞類...

Public Class SomeTableRec 
    Private mField1 As String 
    Private mField1isNull As Boolean 
    Private mField2 As Integer 
    Private mField2isNull As Boolean 

    Public Sub New() 
     mField1isNull = True 
     mField2isNull = True 
    End Sub 
    Public Property Field1() As String 
     Get 
      Return mField1 
     End Get 
     Set(ByVal value As String) 
      mField1 = value 
      mField1isNull = False 
     End Set 
    End Property 
    Public ReadOnly Property Field1isNull() As Boolean 
     Get 
      Return mField1isNull 
     End Get 
    End Property 
    Public Property Field2() As Integer 
     Get 
      Return mField2 
     End Get 
     Set(ByVal value As Integer) 
      mField2 = value 
      mField2isNull = False 
     End Set 
    End Property 
    Public ReadOnly Property Field2isNull() As Boolean 
     Get 
      Return mField2isNull 
     End Get 
    End Property 
End Class 

每個類都有屬性每個字段的...... 因此我可以寫...

Dim Rec as New Table1Rec 
Table1Rec.Field1 = "SomeString" 
Table2Rec.Field2 = 500 

凡字段可以接受NULL值,還有一個額外的屬性,用於指示該值當前是否爲空。

因此....

Dim Rec as New Table1Rec 
Table1Rec.Field1 = "SomeString" 
If Table1Rec.Field1Null then 
    ' This clearly is not true 
End If 
If Table1Rec.Field2Null then 
    ' This will be true 
End If 

這工作,因爲在類的構造函數將所有NULLproperties爲True任何FieldProperty的設置將導致相當於NullProperty被設置爲false。

我最近有需要通過網絡服務(我當然打算保護它)在Web上公開我的DAL,並發現雖然'Rec'類的結構在網絡上保持不變。 。所有的邏輯都丟失了。

如果有人遠程運行上一段代碼,他們會注意到這兩個條件都不會被證明是真的,因爲沒有客戶端代碼將null設置爲true。

我覺得我已經把這個設計完全搞錯了,但是看不到我應該改進它。

什麼是建築師的正確方法?

回答

1

不知道我是否完全理解了這個問題,但是您可以在XML中具有可爲空的數據類型。

所以這...

Imports System.Web 
Imports System.Web.Services 
Imports System.Web.Services.Protocols 

<WebService(Namespace:="http://tempuri.org/")> _ 
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ 
Public Class Testing 
    Inherits System.Web.Services.WebService 

    <WebMethod()> _ 
    Public Function GetObjects() As Generic.List(Of TestObject) 
     Dim list As New Generic.List(Of TestObject) 
     list.Add(New TestObject(Nothing, "Empty ID Object")) 
     list.Add(New TestObject(1, "Full ID Object")) 
     list.Add(New TestObject(2, Nothing)) 
     Return list 
    End Function 

    Public Class TestObject 
     Public Sub New() 
      _name = String.Empty 
      _id = Nothing 
     End Sub 
     Public Sub New(ByVal id As Nullable(Of Integer), ByVal name As String) 
      _name = name 
      _id = id 
     End Sub 
     Private _name As String 
     Public Property Name() As String 
      Get 
       Return _name 
      End Get 
      Set(ByVal value As String) 
       _name = value 
      End Set 
     End Property 

     Private _id As Nullable(Of Integer) 
     Public Property ID() As Nullable(Of Integer) 
      Get 
       Return _id 
      End Get 
      Set(ByVal value As Nullable(Of Integer)) 
       _id = value 
      End Set 
     End Property 
    End Class 

End Class 

輸出這個(可空區)

<?xml version="1.0" encoding="utf-8" ?> 
<ArrayOfTestObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"> 
<TestObject> 
    <Name>Empty ID Object</Name> 
    <ID xsi:nil="true" /> 
</TestObject> 
<TestObject> 
    <Name>Full ID Object</Name> 
    <ID>1</ID> 
</TestObject> 
<TestObject> 
    <ID>2</ID> 
</TestObject> 
</ArrayOfTestObject> 
0

Web服務旨在公開操作(方法)&數據契約而不是內部實現邏輯。在面向服務的體系結構中,這是一件「好事」。你描述的場景是一個遠程/分佈式對象體系結構。 Web服務不會支持你想要做的事情。有關更多信息,請參閱此post