1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
   | Imports System.Reflection
Imports System.ComponentModel
 
#Region "Custom Atributs"
<AttributeUsage(AttributeTargets.All)>
Public Class DataInfoAttribute
    Inherits System.Attribute
    ' Toutes les classes d'attribut héritent de System.Attribute
    Sub New(ByVal FieldName As String)
        Me.FieldName = FieldName
    End Sub
    Private _FielName As String
    Public Property FieldName() As String
        Get
            Return _FielName
        End Get
        Set(ByVal value As String)
            _FielName = value
        End Set
    End Property
    Private _dataType As Type
    Public Property DataType() As Type
        Get
            Return _dataType
        End Get
        Set(ByVal value As Type)
            _dataType = value
        End Set
    End Property
 
    Private _FielLenght As Integer
    Public Property FiedLenght() As Integer
        Get
            Return _FielLenght
        End Get
        Set(ByVal value As Integer)
            _FielLenght = value
        End Set
    End Property
End Class
#End Region
 
#Region "DataEventArgs"
Public Class DataEventArgs
    Public Sub New(ByVal PropertyName As String, ByVal OriginalValue As Object, ByVal ProposedValue As Object)
        Me.PropertyName = PropertyName
        Me.OriginalValue = OriginalValue
        Me.ProposedValue = ProposedValue
 
    End Sub
    Private _PropertyName As String
    Public Property PropertyName() As String
        Get
            Return _PropertyName
        End Get
        Set(ByVal value As String)
            _PropertyName = value
        End Set
    End Property
    Private _OriginaleValue As Object
    Public Property OriginalValue() As Object
        Get
            Return _OriginaleValue
        End Get
        Set(ByVal value As Object)
            _OriginaleValue = value
        End Set
    End Property
    Private _Proposedvalue As Object
    Public Property ProposedValue() As Object
        Get
            Return _Proposedvalue
        End Get
        Set(ByVal value As Object)
            _Proposedvalue = value
        End Set
    End Property
 
End Class
 
Public Class DataEventArgspropertyChanging
    Inherits DataEventArgs
    Public Sub New(ByVal PropertyName As String, ByVal OriginalValue As Object, ByVal ProposedValue As Object)
        MyBase.New(PropertyName, OriginalValue, ProposedValue)
        Me.Cancel = False
        Me.ErrorMessage = String.Empty
    End Sub
    Private _Cancel As Boolean
    Public Property Cancel() As Boolean
        Get
            Return _Cancel
        End Get
        Set(ByVal value As Boolean)
            _Cancel = value
        End Set
    End Property
    Private _ErrorMessage As String
    Public Property ErrorMessage() As String
        Get
            Return _ErrorMessage
        End Get
        Set(ByVal value As String)
            _ErrorMessage = value
        End Set
    End Property
End Class
#End Region
 
Public MustInherit Class DTOBase
    Implements INotifyPropertyChanged
 
    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged
 
 
    Event DataPropertyChanging(ByVal Sender As Object, ByVal e As DataEventArgspropertyChanging)
    Event DataPropertyChanged(ByVal Sender As Object, ByVal e As DataEventArgs)
    Dim DataEventArgsPropertyChanging As DataEventArgspropertyChanging
    'Dim _ErrorMessage As String
    Dim LockPropertyChanging As Boolean
    Protected Function NotifyPropertyChanging(ByVal PropertyName As String, ByVal OriginalValue As Object, ByVal ProposedValue As Object) As Boolean
        If _IsInizializingComponent = True Or LockPropertyChanging = True Or LockPropertyChanged = True Then
            'If _IsInizializingComponent = True Or LockPropertyChanging = True Then
            Return Nothing
            Exit Function
        End If
 
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
 
        LockPropertyChanging = True
        DataEventArgsPropertyChanging = New DataEventArgspropertyChanging(PropertyName, OriginalValue, ProposedValue)
        RaiseEvent DataPropertyChanging(Me, DataEventArgsPropertyChanging)
        LockPropertyChanging = False
        If DataEventArgsPropertyChanging.Cancel = True Then
            ' Modification Annulée
            '_ErrorMessage = DataEventArg.ErrorMessage
            'DataEventArg = Nothing
            Return True
            'Return OriginalValue
        Else
            ' Modification acceptée
            '_ErrorMessage = ""
            ' DataEventArg = Nothing
            'Dim ColName As String = GetAttributsByProp(PropertyName)
            'Save(ColName, ProposedValue)
            Return False
            'Return ProposedValue
        End If
    End Function
    Dim LockPropertyChanged As Boolean
    Protected Sub NotifyPropertyChanged(ByVal PropertyName As String, ByVal Value As Object)
        If _IsInizializingComponent = True Or LockPropertyChanging = True Or LockPropertyChanged = True Then Exit Sub
        'If _IsInizializingComponent = True Or LockPropertyChanging = True Then Exit Sub
 
        LockPropertyChanged = True
        Dim DataEventArg = New DataEventArgs(PropertyName, Value, Value)
        RaiseEvent DataPropertyChanged(Me, DataEventArg)
        LockPropertyChanging = False
        LockPropertyChanged = False
    End Sub
 
    Protected _IsInizializingComponent As Boolean
    Private _DicoAttributes As New Dictionary(Of String, DataInfoAttribute)
    Private _DicoProperties As New Dictionary(Of String, PropertyInfo)
    Private _DicoAttToProperties As New Dictionary(Of String, String)
    Private _DicoPropToAttributs As New Dictionary(Of String, String)
    Public Sub New()
        Call LoadListesAttributesAndProperties()
        _IsNew = True
    End Sub
    Public Sub New(ByVal RowView As DataRowView)
        Me.New()
        Me.RowViewSource = RowView
        Call SetProperties(RowView)
        _IsNew = False
    End Sub
 
    Private _IsNew As Boolean
    Public ReadOnly Property IsNew() As Boolean
        Get
            Return _IsNew
        End Get
        'Set(ByVal value As Boolean)
        '    _IsNew = value
        'End Set
    End Property
    Private _RowViewSource As DataRowView
    Public Property RowViewSource() As DataRowView
        Get
            Return _RowViewSource
        End Get
        Set(ByVal value As DataRowView)
            _RowViewSource = value
            _IsNew = False
        End Set
    End Property
 
    Public ReadOnly Property Attributs() As Dictionary(Of String, DataInfoAttribute)
        Get
            Return _DicoAttributes
        End Get
        'Set(ByVal value As Dictionary(Of String, DataInfoAttribute))
        '    _DicoPropToAttributs = value
        'End Set
    End Property
    'Private _DicoPropToAttributs As Dictionary(Of String, DataInfoAttribute)
    Public ReadOnly Property Properties() As Dictionary(Of String, PropertyInfo)
        Get
            Return _DicoProperties
        End Get
        'Set(ByVal value As Dictionary(Of String, DataInfoAttribute))
        '    _DicoPropToAttributs = value
        'End Set
    End Property
    Public Function GetAttributsByProp(ByVal PropertyName As String) As String
        If Not _DicoPropToAttributs.ContainsKey(PropertyName) Then
            Throw New Exception(String.Format("Le dictionnaire des Attributs ne contient pas la propriété '{0}' du DTO '{1}'", PropertyName, Me.ToString))
            Exit Function
        End If
        'Dim KeyAtt As String = _DicoPropToAttributs(PropertyName)
        'Return _DicoAttributes(KeyAtt)
        Return _DicoPropToAttributs(PropertyName)
    End Function
 
    Public Sub Save()
        If Me.IsNew = True Or Me.RowViewSource Is Nothing Then
            Throw New Exception(String.Format("le DTO '{0}' est détaché ou la propriété 'RowSource' n'a pas été assignée", Me.ToString))
            Exit Sub
        End If
 
        SaveProperties(Me.RowViewSource)
 
    End Sub
    Public Sub Save(ByVal ColChangingName As String, Optional ByVal SetRequestToDBNull As Boolean = False)
        If Me.IsNew = True Or Me.RowViewSource Is Nothing Then
            Throw New Exception(String.Format("le DTO '{0}' est détaché ou la propriété 'RowSource' n'a pas été assignée", Me.ToString))
            Exit Sub
        End If
 
        Dim propriété As PropertyInfo
 
        If Not _DicoAttToProperties.ContainsKey(ColChangingName) Then
            Throw New Exception(String.Format("Le dictionnaire des propriétés ne contient pas la clé du champ '{0}' de l'objet métier '{1}'", ColChangingName, Me.ToString))
            Exit Sub
        Else
            Dim Keyprop As String = _DicoAttToProperties(ColChangingName)
            propriété = _DicoProperties(Keyprop)
            Dim value As Object = propriété.GetValue(Me, Nothing)
 
            SaveProperty(Me.RowViewSource, ColChangingName, value, SetRequestToDBNull)
 
        End If
    End Sub
    Private Sub Save(ByVal ColChangingName As String, ByVal Value As Object, Optional ByVal SetRequestToDBNull As Boolean = False)
        If Me.IsNew = True Or Me.RowViewSource Is Nothing Then
            Throw New Exception(String.Format("le DTO '{0}' est détaché ou la propriété 'RowSource' n'a pas été assignée", Me.ToString))
            Exit Sub
        End If
 
        SaveProperty(Me.RowViewSource, ColChangingName, Value, SetRequestToDBNull)
 
    End Sub
    Private Sub LoadListesAttributesAndProperties()
        Dim attributs() As Object
        Dim att As DataInfoAttribute
        'Dim methode As System.Reflection.MethodInfo
        Dim propriété As System.Reflection.PropertyInfo
        Dim attType As Type = GetType(DataInfoAttribute)
        'Dim classType As Type = GetType(DTOTache)
        Dim classType As Type = Me.GetType
 
        'Debug.WriteLine("Ordinal = " & Col.Ordinal.ToString & " - ColumnName = " & Col.ColumnName.ToString & " - Type = " & Col.DataType.ToString & " - Value = " & Value.ToString)
        For Each propriété In classType.GetProperties
            Dim keyProp As String = propriété.Name
            attributs = Attribute.GetCustomAttributes(propriété, attType)
            If attributs.Length > 0 Then
                att = DirectCast(attributs(0), DataInfoAttribute)
                Dim keyAtt As String = att.FieldName
                If Not _DicoAttributes.ContainsKey(keyAtt) Then
                    _DicoAttributes.Add(keyAtt, att)
                    _DicoAttToProperties.Add(keyAtt, keyProp)
                    _DicoPropToAttributs.Add(keyProp, keyAtt)
                Else
                    Throw New Exception(String.Format("L'attribut '{0}' existe déja dans le DTO '{1}'", keyAtt, Me.ToString))
                    Exit Sub
                End If
                'Debug.WriteLine("Propriété = " & propriété.Name)
                'Debug.WriteLine("Field Name = " & att.FieldName)
                'Debug.WriteLine("Field Lenght = " & att.FiedLenght)
            End If
 
            If Not _DicoProperties.ContainsKey(keyProp) Then
                _DicoProperties.Add(keyProp, propriété)
            End If
        Next
    End Sub
 
    Private Sub SetProperties(ByVal RowView As DataRowView)
        _IsInizializingComponent = True
        Dim Row As DataRow = RowView.Row
 
        For Each Col As DataColumn In Row.Table.Columns
            Dim Value As Object = GetTrueValue(Row, Col)
            'Debug.WriteLine("Ordinal = " & Col.Ordinal.ToString & " - ColumnName = " & Col.ColumnName.ToString & " - Type = " & Col.DataType.ToString & " - Value = " & Value.ToString)
            Dim ColName As String = Col.ColumnName
            If _DicoAttToProperties.ContainsKey(ColName) Then
                'Dim att As DataInfoAttribute = _DicoAttributes(KeyAtt)
                'att.FiedLenght = Col.MaxLength
                _DicoAttributes(ColName).FiedLenght = Col.MaxLength
                _DicoAttributes(ColName).DataType = Col.DataType
                Dim Keyprop As String = _DicoAttToProperties(ColName)
                If _DicoProperties.ContainsKey(Keyprop) Then
                    Dim prop As PropertyInfo = _DicoProperties(Keyprop)
                    prop.SetValue(Me, Value, Nothing)
                Else
                    Throw New Exception(String.Format("Le champ '{0}' du DataRow n'a pas pu initialiser la propriété correspondante du DTO '{1}'", Col.ColumnName, Me.ToString))
                    Exit Sub
                End If
            Else
                Throw New Exception(String.Format("Le champ '{0}' du DataRow n'a pas pu initialiser l'attribut correspondant du DTO '{1}'", Col.ColumnName, Me.ToString))
                Exit Sub
            End If
        Next
        _IsInizializingComponent = False
    End Sub
    'Private IsSettingProperty As Boolean
    Public Function SetProperty(ByVal ColName As String, ByVal Value As Object) As DataEventArgspropertyChanging
        'IsSettingProperty = True
        Dim Keyprop As String = _DicoAttToProperties(ColName)
        If _DicoProperties.ContainsKey(Keyprop) Then
            Dim prop As PropertyInfo = _DicoProperties(Keyprop)
            prop.SetValue(Me, Value, Nothing)
        Else
            Throw New Exception(String.Format("Le champ '{0}' du DataRow n'a pas pu initialiser la propriété correspondante du DTO '{1}'", ColName, Me.ToString))
            Exit Function
        End If
        'IsSettingProperty = False
        Return DataEventArgsPropertyChanging
    End Function
    'Private Function GetPropertieInfoByDataInfoAttributName(ByVal DataInfoAttribut As DataInfoAttribute) As PropertyInfo
 
    '    Return GetPropertieInfoByDataInfoAttributName
    'End Function
    'Private Function GetDataAttributsByPropertyName(ByVal PropertyInfo As PropertyInfo) As DataInfoAttribute
 
    'End Function
    Private Sub SaveProperties(ByVal RowView As DataRowView)
        Dim Row As DataRow = RowView.Row
        Dim attributs() As Object
        Dim att As DataInfoAttribute
        'Dim methode As System.Reflection.MethodInfo
        Dim propriété As PropertyInfo
        Dim attType As Type = GetType(DataInfoAttribute)
        'Dim classType As Type = GetType(DTOTache)
        Dim classType As Type = Me.GetType
 
        For Each propriété In classType.GetProperties
            Dim MatchField As Boolean = False
            Dim Value As Object = propriété.GetValue(Me, Nothing)
            attributs = Attribute.GetCustomAttributes(propriété, attType)
            If attributs.Length > 0 Then
                att = DirectCast(attributs(0), DataInfoAttribute)
                For Each Col As DataColumn In Row.Table.Columns
                    If Col.ColumnName = att.FieldName Then
                        MatchField = True
                        'propriété.SetValue(Me, Value, Nothing)
                        WriteValue(Row, Col.ColumnName, Value)
                        'row(Col) = Value
                        'Debug.WriteLine("Propriété = " & propriété.Name)
                        'Debug.WriteLine("Field Name = " & att.FieldName)
                        'Debug.WriteLine("Field Lenght = " & att.FiedLenght)
                    End If
                Next
                If MatchField = False Then
                    Throw New Exception(String.Format("Une Propriété '{0}' du DTO '{1}' n'a pas trouvé le champ du DataRow correspondant", propriété.Name, Me.ToString))
                    Exit Sub
                End If
            End If
        Next
 
    End Sub
 
    Private Sub SaveProperty(ByVal RowView As DataRowView, ByVal ColChangingName As String, ByVal Value As Object, Optional ByVal SetRequestToDBNull As Boolean = False)
        Dim Row As DataRow = RowView.Row
        'Dim propriété As PropertyInfo
 
        'If Not _DicoAttToProperties.ContainsKey(ColChangingName) Then
        '    Throw New Exception(String.Format("Le dictionnaire des propriétés ne contient pas la clé du champ '{0}' de l'objet métier '{1}'", ColChangingName, Me.ToString))
        '    Exit Sub
        'Else
        'Dim Keyprop As String = _DicoAttToProperties(ColChangingName)
        'propriété = _DicoProperties(Keyprop)
        'Dim value As Object = propriété.GetValue(Me, Nothing)
        If SetRequestToDBNull = True Then
            Call WriteDBNull(Row, ColChangingName)
        Else
            Call WriteValue(Row, ColChangingName, Value)
        End If
        'row(ColChangingName) = value
        'End If
 
    End Sub
    Private Function GetTrueValue(ByVal row As DataRow, ByVal Col As DataColumn) As Object
        Dim _Type As Type = Col.DataType
        Dim _TypeCode As TypeCode = Type.GetTypeCode(_Type)
        Dim value As Object = row(Col)
 
        If value Is DBNull.Value Then
            value = Nothing
        Else
            'Value = CType(Value, T)
        End If
 
        If _TypeCode = TypeCode.String Then 'Le type String est un Type réference qui peut prendre la valeur Nothing. Il faut convertir la valeur Nothing en "".
            If value Is Nothing Then value = ""
        Else
            ' T_Defaut = Defaut
        End If
        Return value
    End Function
    Public Sub WriteDBNull(ByVal Row As DataRow, ByVal ColName As String)
        TryToWriteValue(Row, ColName, DBNull.Value)
    End Sub
    Public Sub WriteValue(ByVal Row As DataRow, ByVal ColName As String, ByVal Value As Object)
        If Value Is Nothing Then Value = DBNull.Value
 
        Dim Original As Object = Row(ColName)
        If Original Is Nothing Then
            TryToWriteValue(Row, ColName, Value)
        Else
            If Original.Equals(Value) Then
            Else
                TryToWriteValue(Row, ColName, Value)
            End If
        End If
    End Sub
    Private Sub TryToWriteValue(ByVal Row As DataRow, ByVal ColName As String, ByVal Value As Object)
        Try
            Row(ColName) = Value
            Mysource1.TraceEvent(TraceEventType.Verbose, 0, String.Format("### Ecriture ### ColName = '{0}' - Value = '{1}'", ColName, Value.ToString))
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Echec d'écriture", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
 
End Class | 
Partager