如何构建 DAL 以实现 WebService 暴露?

发布于 2024-07-08 01:15:18 字数 1946 浏览 7 评论 0原文

我们有一个高度专业化的 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 值的地方,有一个附加属性指示该值当前是否为 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

这是有效的,因为类的构造函数将所有 NULL 属性设置为 True,并且任何 FieldProperty 的设置将导致等效的 NullProperty 设置为 false。

我最近需要通过 Web 服务(我当然打算保护它)在 Web 上公开我的 DAL,并且发现虽然“Rec”类的结构在 Web 上保持完整......所有逻辑都是丢失..

如果有人远程运行前一段代码,他们会注意到这两个条件都不会被证明为真,因为没有客户端代码将 null 设置为 true。

我感觉我设计的这一切都是错误的,但不知道我应该如何改进它。

设计这个的正确方法是什么?

We have a highly specialized DAL which sits over our DB. Our apps need to use this DAL to correctly operate against this DB.

The generated DAL (which sits on some custom base classes) has various 'Rec' classes (Table1Rec, Table2Rec) each of which represents the record structure of a given table.

Here is a sample Pseudo-class...

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

Each class has properties for each of the fields...
Thus I can write...

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

Where a field can accept a NULL value, there is an additional property which indicates if the value is currently null.

Thus....

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

This works because the constructor of the class sets all NULLproperties to True and the setting of any FieldProperty will cause the equivalent NullProperty to be set to false.

I have recently had the need to expose my DAL over the web through a web service (which I of course intend to secure) and have discovered that while the structure of the 'Rec' class remains intact over the web... All logic is lost..

If someone were to run the previous piece of code remotely they would notice that neither condition would prove true as there is no client side code which sets null to true.

I get the feeling I have architected this all wrong, but cannot see how I should improve it.

What is the correct way to architect this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2024-07-15 01:15:18

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

Not sure if I fully understand the question, but you can have nullable data types in XML.

So this...

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

outputs this (with nullable areas)

<?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>
软的没边 2024-07-15 01:15:18

Web 服务旨在公开操作(方法)和操作(方法)。 数据契约但不是内部实现逻辑。 这在面向服务的架构领域是一件“好事”。 您描述的场景是远程/分布式对象体系结构。 Web 服务将不支持您尝试执行的操作。 请参阅此帖子了解更多信息。

Web services are designed to expose operation(methods) & data contracts but not internal implementation logic. This is a "good thing" in the world of service-oriented architecture. The scenario you describe is a remote/distributed object architecture. Web services will not support what you are trying to do. Please see this post for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文