2021年3月8日星期一

Sharing View Model instance in different collections

Are there best practices for creating and sharing a View Model instance between different collection objects? For example, here's a scenario of School and Class classes that both have collections of Student objects:

Public Class Student      Public Property FirstName As String      Public Property LastName As String      Public Property Id As Integer  End Class    Public Class [Class]      Public Property Id As Integer      Public Property Name As String      Public Property Students As New Dictionary(Of Integer, Student)        Public Event StudentAdded(sender As Object, e As StudentEventArgs)        Public Sub AddStudent(student As Student)          If Not Students.ContainsKey(student.Id) Then              Students.Add(student.Id, student)              RaiseEvent StudentAdded(Me, New StudentEventArgs(student))          End If      End Sub    End Class    Public Class School        Public Property Classes As New Dictionary(Of Integer, [Class])      Public Property Students As New Dictionary(Of Integer, Student)        Public Event ClassAdded(sender As Object, e As EventArgs)      Public Event StudentAdded(sender As Object, e As StudentEventArgs)        Public Sub AddClass([class] As [Class])          If Not Classes.ContainsKey([class].Id) Then              Classes.Add([class].Id, [class])              RaiseEvent ClassAdded(Me, EventArgs.Empty)          End If      End Sub        Public Sub AddStudent(student As Student)          If Not Students.ContainsKey(student.Id) Then              Students.Add(student.Id, student)              RaiseEvent StudentAdded(Me, New StudentEventArgs(student))          End If      End Sub    End Class  Public Class StudentEventArgs      Inherits EventArgs        Public Property Student As Student        Public Sub New(student As Student)          Me.Student = student      End Sub    End Class  

When creating a View Model for School and Class, both could end up creating View Models for the same Student objects:

Public Class SchoolViewModel      Inherits BaseViewModel        Private WithEvents _school As School        Public Sub New(school As School)          _school = school      End Sub        Public Property Students As New ObservableCollection(Of StudentViewModel)      Public Property Classes As New ObservableCollection(Of ClassViewModel)        Private Sub _school_StudentAdded(sender As Object, e As StudentEventArgs) Handles _school.StudentAdded          ' Create a new View Model for the Student          Students.Add(New StudentViewModel(e.Student))      End Sub    End Class    Public Class ClassViewModel      Inherits BaseViewModel        Private WithEvents _class As [Class]        Public Sub New([class] As [Class])          _class = [class]      End Sub        Public Property Students As New ObservableCollection(Of StudentViewModel)        Private Sub _class_StudentAdded(sender As Object, e As StudentEventArgs) Handles _class.StudentAdded          ' Create a new View Model or try to get the one created by the SchoolViewModel?          Students.Add(New StudentViewModel(e.Student))      End Sub    End Class    Public Class StudentViewModel      Inherits BaseViewModel        Private _student As Student        Public Sub New(student As Student)          _student = student          With _student              Me.FirstName = .FirstName              Me.LastName = .LastName              Me.Id = .Id          End With      End Sub        Public Property FirstName As String      Public Property LastName As String      Public Property Id As Integer    End Class  

My question is regarding the creation of StudentViewModel instances in both SchoolViewModel and ClassViewModel StudentAdded event handlers. Let's assume that the students are first added to the School and then assigned to their classes. The StudentViewModel is pretty simple and creating duplicates might not be a problem but it nonetheless seems wasteful when a View Model for the same student has already been created in the SchoolViewModel.

If I want to share the SchoolViewModel's StudentViewModel, what's a best practices approach for that?

  1. Passing a reference to SchoolViewModel's Students ObservableCollection when creating a new ClassViewModel?
  2. Creating some GetStudentViewModelDelegate function?
  3. Creating a Shared collection of StudentViewModel?

I usually try to limit references between objects and creating new View Models would be a tradeoff for that in this example.

https://stackoverflow.com/questions/66448294/sharing-view-model-instance-in-different-collections March 03, 2021 at 06:50AM

没有评论:

发表评论