2008-10-24 49 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