I have this class hierarchy:
public interface ISR { }
public interface ICU { }
public interface IFI { }
public class CU : ICU { }
public class SR : ISR
{
public SR(IFI fi)
{
FI = fi;
}
public IFI FI { get; set; }
}
public class FI : IFI
{
public FI(ISR sr, ICU cu)
{
SR = sr;
CU = cu;
}
public ISR SR { get; set; }
public ICU CU { get; set; }
}
public class Module : NinjectModule
{
public override void Load()
{
Bind<ISR>().To<SR>();
Bind<ICU>().To<CU>();
Bind<IFI>().To<FI>();
}
}
class Program
{
static void Main(string[] args)
{
var kernel = new StandardKernel(new Module());
var sr = kernel.Get<ISR>();
}
}
When I run this code, I have an exception because I have a cyclic dependency. The SR class needs an instance of IFI to be injected in order to be completed, and the FI class needs an instance of ISR to be injected.
Of course, using a property injection doesn't solve this issue.
I have a particularity though: I need ISR to be constructed first, and it is this instance which must be given to FI. So ISR and IFI need to share a scope.
How would you solve that with Ninject?