0

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.

1
  • 1
    Doesn't seem to be related to wpf or mvvm or viewmodel, but generally to constructing graphs of objects with cycles. Please show VMPerson and VMPreference classes. Commented Jul 25, 2014 at 14:29

2 Answers 2

1

Where you say And from here it will start all over again, that is not necessarily true and it's entirely up to you how far it goes. The view models only wrap the data that you provide for them. Therefore, if you don't provide any preferences for the Person A that is actually set as the partner for Person B, then there will be no loop.

Furthermore, if you just make a rule that the Person objects set as the Partner property values don't have preferences, then you will equally avoid all loops. It's really a case of not all Person objects require all of their properties to be set.

You could just ensure that all Person objects on the main level are fully populated, but the ones referred to from the Partner properties are not and just have whichever property values that you require.

Sign up to request clarification or add additional context in comments.

Comments

0

Do you need the preference to relate to the the full person? It looks as though you might get away with having a third simpler person reference class which doesn't include the preferences collection. Unless of course you want a mass of interlinked preferences to traverse. Your current person class could even extend it.

Another idea, as found in entity framework which deals with the same issue in the form of navigation properties, is to make the collection lazy load from your data source. That way, the person property in your preference object is only assigned when a get is called.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.