Note that there are some explanatory texts on larger screens.

plurals
  1. POViewModel many to many relationship
    primarykey
    data
    text
    <p>For our project, we would like to use ViewModels. So we decided to create ViewModels for every object we can have :</p> <p>So the ViewModel looks like this (GroupVM) :</p> <pre><code>public class GroupVM : EORTCViewModel&lt;Prisma.BO.Group&gt; { public GroupVM(Prisma.BO.Group group) : base(group) { } public string Name { get { return this._Model.Name; } } public string ShortName { get { return this._Model.ShortName; } } public int Id { get { return this._Model.Id; } } [ScriptIgnore] public ICollection&lt;StudyGroupVM&gt; StudyGroups { get { return Prisma.Web.Models.Factories.ViewModelFactory&lt;Prisma.BO.StudyGroup&gt;.ToViewModel&lt;StudyGroupVM&gt;(this._Model.StudyGroups); } } } </code></pre> <p>The StudyVM :</p> <pre><code>public class StudyVM : EORTCViewModel&lt;Prisma.BO.Study&gt; { public StudyVM(Prisma.BO.Study study) : base(study) { } public string Number { get { return this._Model.Number; } } [ScriptIgnore] public ICollection&lt;StudyGroupVM&gt; StudyGroups { get { return Prisma.Web.Models.Factories.ViewModelFactory&lt;Prisma.BO.StudyGroup&gt;.ToViewModel&lt;StudyGroupVM&gt;(this._Model.StudyGroups); } } } </code></pre> <p>and the intermediate object (StudyGroup) :</p> <pre><code>public class StudyGroupVM : EORTCViewModel&lt;Prisma.BO.StudyGroup&gt; { public StudyGroupVM(Prisma.BO.StudyGroup studyGroup) : base(studyGroup) { } public StudyVM Study { get { return Prisma.Web.Models.Factories.ViewModelFactory&lt;Prisma.BO.Study&gt;.ToViewModel&lt;StudyVM&gt;(this._Model.Study); } } public GroupVM Group { get { return Prisma.Web.Models.Factories.ViewModelFactory&lt;Prisma.BO.Group&gt;.ToViewModel&lt;GroupVM&gt;(this._Model.Group); } } } </code></pre> <p>We would like to use a factory that would create ViewModels for every navigation properties.</p> <p>So we wrote a generic ViewModel factory as follow :</p> <pre><code>public static class ViewModelFactory&lt;T&gt; where T : IEORTCObject { public static R ToViewModel&lt;R&gt;(T model) where R : EORTCViewModel&lt;T&gt; { return (R)Activator.CreateInstance(typeof(R), new object[] { model }); } public static ICollection&lt;R&gt; ToViewModel&lt;R&gt;(ICollection&lt;T&gt; models) where R : EORTCViewModel&lt;T&gt; { ICollection&lt;R&gt; viewModels = new List&lt;R&gt;(); foreach (T model in models) { viewModels.Add((R)Activator.CreateInstance(typeof(R), new object[] { model })); } return viewModels; } } </code></pre> <p>The problem is that when we try to access the StudyGroups property from one of the end object of the relation, it creates correctly the object but then want to recreate the same object because it seems it tries to access the getter for the other end of the relation.</p> <p>This cause a StackOverFlowException since it never stops (cycles).</p> <p>Does any of you have any idea how I can solve this issue ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload