Note that there are some explanatory texts on larger screens.

plurals
  1. PONinject, using two way property injection to resolve cyclic dependency
    primarykey
    data
    text
    <p>Based on the answers in this question: <a href="https://stackoverflow.com/questions/3511547/cyclic-dependency-with-ninject">Cyclic dependency with ninject</a> and this questions: <a href="https://stackoverflow.com/questions/6180280/ninject-give-the-parent-instance-to-a-child-being-resolved">Ninject: give the parent instance to a child being resolved</a> both of these say you can use two way property injection to resolve cyclic dependencies as long as you change the scope to not be the default Transient Scope. So I tried doing that, I've got a <code>UserService</code> and a <code>GroupService</code> that require each other (and please don't say change the classes or use a third class, etc.!). I've got a generic <code>EntityServiceFactory</code> that uses ninject like so:</p> <pre><code>public static class EntityServiceFactory { public static TServiceClass GetService&lt;TServiceClass&gt;() where TServiceClass : class { IKernel kernel = new StandardKernel(); return kernel.Get&lt;TServiceClass&gt;(); } } </code></pre> <p>And my services:</p> <pre><code>public class GroupService : EntityService&lt;GroupRepository, Group, DbContext&gt; { public UserService _userService { private get; set; } public GroupService(GroupRepository repository, UserService userService) : base(repository) { userService._groupService = this; } public class UserService : EntityService&lt;UserRepository, User, DbContext&gt; { public GroupService _groupService { private get; set; } public UserService(UserRepository repository, GroupService groupService) : base(repository) { groupService._userService = this; } </code></pre> <p>Then as per the instructions in the answers to those questions add the following to my <code>EntityServiceFactory</code>:</p> <pre><code>kernel.Bind&lt;GroupService&gt;().ToSelf().InCallScope(); kernel.Bind&lt;UserService&gt;().ToSelf().InCallScope(); </code></pre> <p>But I still get the Error:</p> <blockquote> <p>A cyclical dependency was detected between the constructors of two services.</p> </blockquote> <p>Am I doing the two way property injection correctly? How do I resolve this?</p>
    singulars
    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.
 

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