简体   繁体   English

绑定介体(shortbus)与/要注入

[英]Binding mediator (shortbus) with/to ninject

I am trying to use the mediator pattern with shortbus( https://github.com/mhinze/ShortBus ). 我试图将调解器模式用于shortbus( https://github.com/mhinze/ShortBus )。 Everything goes great except binding it to ninject. 除了将其绑定到ninject之外,一切都进行得很好。 There is a structuremap example like so 有一个这样的structuremap示例

    public BasicExample()
    {
        ObjectFactory.Initialize(i =>
        {
            i.Scan(s =>
            {
                s.AssemblyContainingType<IMediator>();
                s.TheCallingAssembly();
                s.WithDefaultConventions();
                s.AddAllTypesOf((typeof(IRequestHandler<,>)));
                s.AddAllTypesOf(typeof(INotificationHandler<>));
            });

            i.For<IDependencyResolver>().Use(() => DependencyResolver.Current);
        });

        ShortBus.DependencyResolver.SetResolver(new StructureMapDependencyResolver(ObjectFactory.Container));
    }

The above is for a unit test. 以上是单元测试。 I want to be able to unit test as well but most of all I just want it to work with the whole project. 我也希望能够进行单元测试,但最重要的是,我只希望它与整个项目一起工作。

There is a NinjectDependencyResolver and this should work with ninject. 有一个NinjectDependencyResolver,它应该与ninject一起工作。 I just know ninject to poorly to get it straight. 我只是知道ninject很难做到这一点。

I use Ninject MVC with NinjectWebCommon. 我将Ninject MVC与NinjectWebCommon一起使用。 And the above code is supposed to work for structuremap so i simply need the equivalent for Ninject. 上面的代码应该适用于Structuremap,所以我只需要Ninject的等效代码即可。

Ninject works a bit differently. Ninject的工作方式略有不同。 For the IRequestHandler<,> and INotificationHandler<> type bindings you should use ninject.extensions.conventions and do something like: 对于IRequestHandler<,>INotificationHandler<>类型绑定,应使用ninject.extensions.conventions并执行以下操作:

var kernel = new StandardKernel();

kernel.Bind(x => x.FromThisAssembly()
    .SelectAllClasses()
    .InheritedFromAny(
        new[]
        {
            typeof(ICommandHandler<>), 
            typeof(IQueryHandler<,>)
        })
    .BindDefaultInterfaces());

kernel.Bind<IDependencyResolver>().ToMethod(x => DependencyResolver.Current);


ShortBus.DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));

You may need to adapt the following: 您可能需要调整以下内容:

  • FromThisAssembly() --> this means only types of the assembly where you write that line will be bound. FromThisAssembly() ->这意味着仅绑定您编写该行的程序集类型。 You can use another mechanism where you specify in which assemblies to look for your ICommandHandler<> and IQueryHandler<,> types. 您可以使用另一种机制,在其中指定在哪些程序集中查找ICommandHandler<>IQueryHandler<,>类型。
  • BindDefaultInterfaces() : See here for an explanation and alternatives. BindDefaultInterfaces() :有关说明和替代方法,请参见此处

Also note that my example code is based upon ShortBus.Ninject 3.0.48-Beta . 还要注意,我的示例代码基于ShortBus.Ninject 3.0.48-Beta The most current ShortBus stable version is referencing StructureMap. 最新的ShortBus 稳定版本正在引用StructureMap。

EDIT: I see that you tagged your question asp.net . 编辑:我看到你标记了问题asp.net Instead of using StructureMap.Ninject and it's NinjectDependencyResolver you are probably better off using Ninject.Web.Common (make sure it's the latest version!) and NinjectDependencyResolver of Ninject.web.mvc . 与其使用StructureMap.Ninject及其NinjectDependencyResolver ,不如使用Ninject.Web.Common (确保它是最新版本!)和NinjectDependencyResolverNinjectDependencyResolver更好

For completeness... don't forget to bind your IMediator 为了完整性...别忘了绑定您的IMediator

kernel.Bind<IMediator>().To<Mediator>();

Also worth noting that if you use BindDefaultInterfaces() you cannot name your handlers however you fancy. 还值得注意的是,如果使用BindDefaultInterfaces(),则无法命名处理程序,但是您很喜欢。 See link posted by BatteryBackupUnit. 请参阅BatteryBackupUnit发布的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM