结合使用/中保(性爱巴士)到ninject [英] Binding mediator (shortbus) with/to ninject
问题描述
我想使用与性爱巴士( https://github.com/mhinze/ShortBus的调解模式一>)。一切顺利,除了结合它ninject大。还有像这样一个例子structuremap
I am trying to use the mediator pattern with shortbus(https://github.com/mhinze/ShortBus). Everything goes great except binding it to ninject. There is a structuremap example like so
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.
有一个NinjectDependencyResolver,这应该与ninject工作。我只知道ninject不佳得到它平直。
There is a NinjectDependencyResolver and this should work with ninject. I just know ninject to poorly to get it straight.
我用Ninject MVC与NinjectWebCommon。
和上面的code被认为能为structuremap工作,所以我只需要为Ninject的等价物。
I use Ninject MVC with NinjectWebCommon. And the above code is supposed to work for structuremap so i simply need the equivalent for Ninject.
推荐答案
Ninject工作稍有不同。
对于 IRequestHandler&LT;,&GT;
和 INotificationHandler&LT;&GT;
键入绑定你应该使用的 ninject.extensions.conventions 并做类似:
Ninject works a bit differently.
For the IRequestHandler<,>
and INotificationHandler<>
type bindings you should use ninject.extensions.conventions and do something like:
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));
您可能需要调整如下:
-
FromThisAssembly()
- >这意味着只有类型,你写的这条线将被绑定的组装。您可以使用另一种机制,其中您可以指定哪些程序集寻找你的ICommandHandler&LT;&GT;
和IQueryHandler&LT;,&GT;
类型。 -
BindDefaultInterfaces()
:见的这里的解释和替代。
FromThisAssembly()
--> this means only types of the assembly where you write that line will be bound. You can use another mechanism where you specify in which assemblies to look for yourICommandHandler<>
andIQueryHandler<,>
types.BindDefaultInterfaces()
: See here for an explanation and alternatives.
另外请注意,我的例子code是根据 SHORTBUS。 Ninject 3.0.48-β。最新SHORTBUS的稳定的版本被引用StructureMap。
Also note that my example code is based upon ShortBus.Ninject 3.0.48-Beta. The most current ShortBus stable version is referencing StructureMap.
编辑:我看到你标记你的问题 asp.net
。 StructureMap.Ninject
,它是不是使用的 NinjectDependencyResolver
你最好还是不要使用的 Ninject.Web.Common (确保它是最新版本!)和 NinjectDependencyResolver
Ninject.web.mvc 。
I see that you tagged your question 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.
这篇关于结合使用/中保(性爱巴士)到ninject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!