I have a MVC application that references half a dozen assemblies. Each assembly has dependent components for the web site. And in some case these assemblies have dependencies on one another. Here is some prototype
Assembly1
---------
interface ILogger{...}
Assembly2
--------
class MyLogger : ILogger {...}
Assembly3
--------
interface IRepository {.....}
Assembly4
---------
interface MyResository : IRepository
{
[Inject]
public ILogger Logger{get;set;}
... other methods...
}
MVCApp
------
public HomeController : Controller
{
[Inject]
public ILogger Logger{get;set;}
[Inject]
public IRepository Repository{get;set;}
}
I have put all Ninject related code for Di resolution in a separate assembly and that assembly references all other projects.
Here is what happens. When HomeController is instantiated, I have valid instances of Logger and Repository properties. Logger has no dependency, it is perfect. But when I look inside instance of Repository, Logger instance has not been instantiated.
How do you set up DI using Ninject in such situations?
Thanks
I resolved this issue by implementing provider for IRepository. In CreateInstance, I instantiated Logger property by resolving it with Kernel.
CreateInstance(context)
{
var repo = new MyRepository();
repo.Logger = context.Kernel.Get<ILogger>();
}
Works like a charm!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With