Ninject 绑定约束,向上搜索以查找类型

发布于 2024-12-06 06:05:55 字数 1331 浏览 0 评论 0原文

我有一个像这样的类层次结构(简化):

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 的多个实现。我想创建一个提供者/绑定:

  1. 构造一个新的 Connection 实例。
  2. GetAll 并将其应用于连接。
  3. 绑定指定要使用的 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:

  1. constructs a new instance of Connection.
  2. GetAll and applies that to the Connection.
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2024-12-13 06:05:55

通过调用kernel.GetAll,您将创建一个新请求。它没有有关服务上下文的信息。有一个扩展允许您创建保留原始上下文的新请求 (Ninject.Extensions.ContextPreservation)

另请参阅 https://github.com/ninject/ninject.extensions.contextpreservation/wiki

context.GetContextPreservingResolutionRoot().GetAll<IConnectionConfiguration>();

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

context.GetContextPreservingResolutionRoot().GetAll<IConnectionConfiguration>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文