Based on the answers in this question: Cyclic dependency with ninject and this questions: Ninject: give the parent instance to a child being resolved 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 UserService
and a GroupService
that require each other (and please don't say change the classes or use a third class, etc.!). I've got a generic EntityServiceFactory
that uses ninject like so:
public static class EntityServiceFactory
{
public static TServiceClass GetService<TServiceClass>()
where TServiceClass : class
{
IKernel kernel = new StandardKernel();
return kernel.Get<TServiceClass>();
}
}
And my services:
public class GroupService : EntityService<GroupRepository, Group, DbContext>
{
public UserService _userService { private get; set; }
public GroupService(GroupRepository repository, UserService userService) : base(repository)
{
userService._groupService = this;
}
public class UserService : EntityService<UserRepository, User, DbContext>
{
public GroupService _groupService { private get; set; }
public UserService(UserRepository repository, GroupService groupService)
: base(repository)
{
groupService._userService = this;
}
Then as per the instructions in the answers to those questions add the following to my EntityServiceFactory
:
kernel.Bind<GroupService>().ToSelf().InCallScope();
kernel.Bind<UserService>().ToSelf().InCallScope();
But I still get the Error:
A cyclical dependency was detected between the constructors of two services.
Am I doing the two way property injection correctly? How do I resolve this?