我们有一个高度专业化的DAL,它位于我们的数据库之上。我们的应用程序需要使用此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。

我最近需要通过网络服务(我当然打算保护)通过网络公开我的DAL,并且发现虽然'Rec'类的结构在网上仍然完好无损......所有逻辑都丢失了..

如果有人要远程运行上一段代码,他们会注意到这两种情况都不会证明是正确的,因为没有客户端代码将null设置为true。

我觉得我已经把这一切都弄错了,但看不出我应该如何改进它。

建立这个的正确方法是什么?

有帮助吗?

解决方案

不确定我是否完全理解这个问题,但您可以在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>

其他提示

Web服务旨在公开操作(方法)和数据合同但不是内部实施逻辑。这是一件“好事”。在面向服务架构的世界中。您描述的方案是远程/分布式对象体系结构。 Web服务不支持您尝试执行的操作。有关详细信息,请参阅此发布

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top