基于 Ninject 约定的绑定

发布于 2024-10-15 15:22:52 字数 635 浏览 5 评论 0原文

我再次认为我可能错过了这里显而易见的事情。我希望按照约定进行自动绑定。我一直在研究 Ninject.extension.conventions 项目和程序集扫描器。

我有很多看起来像下面这样的行,我想自动绑定:

Bind<ICommandHandler<MyCommand>>().To<MyCommandHandler>();
Bind<ICommandHandler<MyOtherCommand>>().To<MyOtherCommandHander>();

我已经尝试了几种变体:

Kernal.Scan(x => {
    x.FromAssemblyContaining<MyCommand>();
    x.WhereTypeInheritsFrom(typeof(ICommandHander<>));
    x.BindWith(new DefaultBindingGenerator());
});

但是在以下情况下没有返回实例:

kernel.Get<ICommandHandler<T>>(); 

Once again I think I might be missing the obvious here. I'm looking to do auto binding by conventions. I've been looking at the Ninject.extension.conventions project and the assembly scanner.

What I have is a lot lines that look like the following, that I would like to auto bind:

Bind<ICommandHandler<MyCommand>>().To<MyCommandHandler>();
Bind<ICommandHandler<MyOtherCommand>>().To<MyOtherCommandHander>();

I've tried several variations of:

Kernal.Scan(x => {
    x.FromAssemblyContaining<MyCommand>();
    x.WhereTypeInheritsFrom(typeof(ICommandHander<>));
    x.BindWith(new DefaultBindingGenerator());
});

But there are no instances returned when:

kernel.Get<ICommandHandler<T>>(); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

楠木可依 2024-10-22 15:22:52

尝试查看 GenericBindingGenerator 而不是 DefaultBindingGenerator。

try looking at GenericBindingGenerator instead of DefaultBindingGenerator.

白况 2024-10-22 15:22:52
// use Ninject.Extensions.Conventions for convention-based binding
kernel.Scan(scanner =>
    {
        // look for types in this assembly
        scanner.FromCallingAssembly();

        // make ISomeType bind to SomeType by default (remove the 'I'!)
        scanner.BindWith<DefaultBindingGenerator>();
    });
// use Ninject.Extensions.Conventions for convention-based binding
kernel.Scan(scanner =>
    {
        // look for types in this assembly
        scanner.FromCallingAssembly();

        // make ISomeType bind to SomeType by default (remove the 'I'!)
        scanner.BindWith<DefaultBindingGenerator>();
    });
┊风居住的梦幻卍 2024-10-22 15:22:52

解决办法:

Kernel.Scan(x => {
    x.FromAssemblyContaining<CoreModule>();
     x.BindingGenerators.Add(new GenericBindingGenerator(typeof(IHandleQuery<,>)));
      x.InSingletonScope();
});

The solution:

Kernel.Scan(x => {
    x.FromAssemblyContaining<CoreModule>();
     x.BindingGenerators.Add(new GenericBindingGenerator(typeof(IHandleQuery<,>)));
      x.InSingletonScope();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文