I am trying to make a program which helps with combining persons to groups by matching their preferences.
But before I even come to the combining, I already have a problem with the viewmodels for this case.
Let's estimate the following classes:
public class Person
{
public string Name;
public List<Preference> PartnerPreferences;
}
public class Preference
{
public Person Partner;
public int Preference; //-1 does not want as partner / +1 does want as partner
}
I can now create the corresponding viewmodels VMPerson and VMPreference. If I do it like I usually do, then the constructor of VMPerson will have somehting like this in it:
foreach (Preference pref in _person.PartnerPreferences)
{
PartnerPreferences.Add(new VMPreference(pref));
}
Where PartnerPfreferences is an Observablecollection<VMPreferences>
And in the VMPreference:
Person = new VMPerson(_preference.Person);
But when I want to create the viewmodels from even a simple model I will run into a loop generating infinite viewmodels.
Example:
The model has 2 persons, A and B. A wants B as his partner and B wants A as his partner.
Creating viewmodel for A
-> In VMPartner constructor, preferences of person A get wrapped in viewmodels
-> In VMPreference constructor, person B gets wrapped in viewmodel
-> In VMPartner constructor, preferences of person B get wrapped in viewmodels
-> In VMPreference constructor, person A gets wrapped in viewmodels
[And from here it will start all over again]
(Since the constructor of VMPreference can not know that person A already
has a viewmodel and he could stop here, setting the already existing
viewmodel in place)
The only solution I see is to make the VMPreference a VM with only string Name and int Preference. But this would disable many things like using Preferences.Add(new VMPreference(SelectedPerson)) and would also complicate the Viewmodel to Model conversion.
I am thankful for any tips and thoughts on how to solve this.