Ninject 绑定约束,向上搜索以查找类型
我有一个像这样的类层次结构(简化):
class Connection
{
}
interface IService<T>
{
}
class ServiceImplementation : IService<int>
{
public ServiceImplementation(Connection)
{
}
}
interface IConnectionConfiguration
{
public void Configure(Connection c)
}
class ConnectionConfiguration : IConnectionConfiguration
{
public void Configure(Connection c)
}
其中我有 IConnectionConfiguration 和 IService 的多个实现。我想创建一个提供者/绑定:
- 构造一个新的 Connection 实例。
- GetAll 并将其应用于连接。
- 绑定指定要使用的 IConnectionConfiguration 实现,基于 关于要构造的 IService 类型
目前我有一个像这样的提供程序实现:
public Connection CreateInstance(IContext context)
{
var configurations = context.Kernel.GetAll<IConnectionConfiguration>()
var connection = new Connection();
foreach(var config in configurations)
{
config.Configure(connection);
}
return connection;
}
但是当我尝试为 IConnectionConfiguration 进行上下文绑定时,它没有父请求或父上下文...
Bind<IConnectionConfiguration>().To<ConcreteConfiguration>().When(ctx => {
// loop through parent contexts and see if the Service == typeof(IService<int>);
// EXCEPT: The ParentRequest and ParentContext properties are null.
});
我在这里做错了什么?我可以用ninject 做到这一点吗?
I've got a class hierarchy like this (simplified):
class Connection
{
}
interface IService<T>
{
}
class ServiceImplementation : IService<int>
{
public ServiceImplementation(Connection)
{
}
}
interface IConnectionConfiguration
{
public void Configure(Connection c)
}
class ConnectionConfiguration : IConnectionConfiguration
{
public void Configure(Connection c)
}
Where I have multiple implementations of IConnectionConfiguration and IService. I am wanting to create a provider/bindings which:
- constructs a new instance of Connection.
- GetAll and applies that to the Connection.
- Bindings specify which IConnectionConfiguration implementations to be used, based on
on the type of IService to be constructed
Currently I have a provider implementation like this:
public Connection CreateInstance(IContext context)
{
var configurations = context.Kernel.GetAll<IConnectionConfiguration>()
var connection = new Connection();
foreach(var config in configurations)
{
config.Configure(connection);
}
return connection;
}
But when I try to make the contextual binding for IConnectionConfiguration it doesn't have a parent request or parent context...
Bind<IConnectionConfiguration>().To<ConcreteConfiguration>().When(ctx => {
// loop through parent contexts and see if the Service == typeof(IService<int>);
// EXCEPT: The ParentRequest and ParentContext properties are null.
});
What am I doing wrong here? Can I do this with ninject?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过调用
kernel.GetAll
,您将创建一个新请求。它没有有关服务上下文的信息。有一个扩展允许您创建保留原始上下文的新请求 (Ninject.Extensions.ContextPreservation)另请参阅 https://github.com/ninject/ninject.extensions.contextpreservation/wiki
By calling
kernel.GetAll
you are creating a new request. It has no information about the service context. There is an extension that allows you to create new requests that preserve the original context (Ninject.Extensions.ContextPreservation)See also https://github.com/ninject/ninject.extensions.contextpreservation/wiki