Ninject 自动连线

发布于 2024-12-29 01:01:54 字数 112 浏览 2 评论 0原文

一些 IOC 容器具有所谓的基于约定的自动装配,例如,IProductRepository 映射到 ProductRepository,而无需您进行任何手动装配。

Ninject有这样的事吗?

Some of the IOC containers have what's called auto-wiring based on conventions, for e.g., IProductRepository maps to ProductRepository without any manual wiring on your part.

Is there such a thing with Ninject?

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

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

发布评论

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

评论(2

⒈起吃苦の倖褔 2025-01-05 01:01:54
// 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>();
    });

复制自 @Pete Montgomery 评论

// 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>();
    });

copied from @Pete Montgomery comment

泡沫很甜 2025-01-05 01:01:54

Ninject 附带了基于约定的配置的扩展。但您仍然需要配置您的约定。请参阅 https://github.com/ninject/ninject.extensions.conventions 语法有3.0.0 发生了变化,但变得更加强大。以下将为系统中的所有类添加绑定。但通常您需要针对不同类型的类使用其中一些约定(例如,服务是单例,......)

kernel.Bind(
    x => x.FromThisAssembly()
          .SelectAllClasses()
          .BindAllInterfaces());

Ninject comes with an extension for convention based configuration. But you still need to configure your convenions. See https://github.com/ninject/ninject.extensions.conventions The syntax has changed for 3.0.0 but has become much more powerful. The following would add bindings for all classes in your system. But normally you want several of these conventions for different kind of classes (e.g. services are singletons, ....)

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