使用 Decorator 模式进行 Ninject 依赖注入
比如说,我有这样的类层次结构:
public interface IRepository { }
public class SomeSimpleRepository : IRepository {}
现在我想用附加功能“装饰” SomeSimpleRepository
public class MoreAdvancedRespository : IRepository
{
private readonly IRepository _originalRepository;
public MoreAdvancedRespository(IRepository original)
{ }
}
过了一会儿又一个..
public class TrickyRepository : IRepository
{
private readonly IRepository _originalRepository;
public TrickyRepository (IRepository original)
{ }
}
现在,我需要完成绑定。在应用程序中,我需要 TrickyRepository 的实例,用 MoreAdvancedRespository 进行初始化。所以,我需要写一些类似的东西:
Bind<IRepository>().To<TrickyRepository>.With ??
在这里我很困惑,我需要以某种方式说,采用 MoreAdvancedRespository 但用 SomeSimpleRepository 初始化它。这是一种必须针对一个接口来解决的依赖链。
有人对此有建议吗?
Say, I have such classes hierarchy:
public interface IRepository { }
public class SomeSimpleRepository : IRepository {}
Now I want to "decorate" SomeSimpleRepository with additional functions
public class MoreAdvancedRespository : IRepository
{
private readonly IRepository _originalRepository;
public MoreAdvancedRespository(IRepository original)
{ }
}
After awhile another one..
public class TrickyRepository : IRepository
{
private readonly IRepository _originalRepository;
public TrickyRepository (IRepository original)
{ }
}
Now, I need to accomplish binding. In application I need the instance of TrickyRepository, to be initialized with MoreAdvancedRespository. So, I need to write something like:
Bind<IRepository>().To<TrickyRepository>.With ??
Here I'm confused, I need somehow to say, take MoreAdvancedRespository but initialize it with SomeSimpleRepository. This is a kind of chain of dependencies that have to be resolved against one interface.
Does any one have suggestion on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
WhenInjectedInto
:请参阅 这篇博文了解更多信息。
Use
WhenInjectedInto
:See this blog post for more info.