Domanda

Abbiamo un DAL altamente specializzato che si trova sul nostro DB. Le nostre app devono utilizzare questo DAL per operare correttamente contro questo DB.

Il DAL generato (che si trova su alcune classi di base personalizzate) ha varie classi 'Rec' (Table1Rec, Table2Rec) ognuna delle quali rappresenta la struttura record di una determinata tabella.

Ecco un esempio di pseudo-classe ...

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

Ogni classe ha proprietà per ciascuno dei campi ... Quindi posso scrivere ...

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

Dove un campo può accettare un valore NULL, c'è una proprietà aggiuntiva che indica se il valore è attualmente nullo.

Così ....

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

Funziona perché il costruttore della classe imposta tutte le proprietà NULL su True e l'impostazione di qualsiasi FieldProperty farà sì che l'equivalente NullProperty sia impostata su false.

Di recente ho avuto la necessità di esporre il mio DAL sul web attraverso un servizio web (che ovviamente intendo proteggere) e ho scoperto che mentre la struttura della classe 'Rec' rimane intatta sul web ... Tutta la logica è persa ..

Se qualcuno eseguisse il codice precedente in remoto noterebbe che nessuna delle due condizioni si rivelerebbe vera in quanto non esiste un codice lato client che imposta null su true.

Ho la sensazione di aver progettato tutto in modo sbagliato, ma non riesco a vedere come dovrei migliorarlo.

Qual è il modo corretto di progettarlo?

È stato utile?

Soluzione

Non sono sicuro di comprendere appieno la domanda, ma puoi avere tipi di dati nullable in XML.

Quindi questo ...

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

genera questo (con aree nullable)

<?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>

Altri suggerimenti

I servizi Web sono progettati per esporre operazioni (metodi) e amp; contratti di dati ma non logica di implementazione interna. Questa è una "cosa buona" nel mondo dell'architettura orientata ai servizi. Lo scenario che descrivi è un'architettura di oggetti remota / distribuita. I servizi Web non supporteranno ciò che si sta tentando di fare. Per ulteriori informazioni, consulta questo post per ulteriori informazioni.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top