What .NET data structure should I use?

Here is the data structure that my object will have to expose (the data is NOT stored in XML, it was just the easiest way to illustrate the layout):

<Department id="Accounting">
  <Employee id="1234">Joe Jones</Employee>
  <Employee id="5678">Steve Smith</Employee>
</Department>
<Department id="Marketing">
  <Employee id="3223">Kate Connors</Employee>
  <Employee id="3218">Noble Washington</Employee>
  <Employee id="3233">James Thomas</Employee>
</Department>

When I de-serialize data, how can I expose it in terms of properties on my object? If it were just the Department and EmployeeID, I think I would use a dictionary. But I also need to bind the name EmployeeName.

+3
source share
4 answers
Class Department
   Public Id As Integer
   Public Employees As List(Of Employee)
End Class

Class Employee
   Public Id As Integer
   Public Name As String
End Class

Something like this (can't remember my VB syntax). Be sure to use the Properties and Public properties ...

+7
source

( ), Employee ( ).

+6
Public Class Employee

    Private _id As Integer
    Public Property EmployeeID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    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


End Class

Public Class Department

    Private _department As String
    Public Property Department() As String
        Get
            Return _department
        End Get
        Set(ByVal value As String)
            _department = value
        End Set
    End Property

    Private _employees As List(Of Employee)
    Public Property Employees() As List(Of Employee)
        Get
            Return _employees
        End Get
        Set(ByVal value As List(Of Employee))
            _employees = value
        End Set
    End Property

End Class
+4
  • Department Object
  • Employee object
  • Object employeeCollection. (optional, you can just use List (of Employee))

Mark them as serializable, and then you can (de) serialize them in any format.

+2
source

Source: https://habr.com/ru/post/1702426/


All Articles