如何使用Ninject的公约扩展绑定? [英] How to bind using Ninject Conventions Extension?

查看:154
本文介绍了如何使用Ninject的公约扩展绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢娄代码绑定Ninject自动绑定。是否有可能同时使用mannual&放大器;在单个项目中自动绑定?让; S带波纹管手工绑定,我想实现自动绑定。请告诉我如何实现这一目标。




  1. kernel.Bind<的TestContext>()ToSelf(。 ).InRequestScope();


  2. kernel.Bind< IUnitOfWork<的TestContext>>()。至<的UnitOfWork<的TestContext>>();




娄从继承的接口底座接口: IRepository<型号>



3。 kernel.Bind< IUserRepository方式>()为< UserRepository>();



4。 kernel.Bind< IAccountRepository方式>()为< AccountRepository>();



5。 kernel.Bind< IMessageRepository方式>()为< MessageRepository方式>()WithConstructorArgument(apikey,AppSettingsManager.GetSmsApiKey)



其他



我是否需要写 .Exclude< MessageRepository>()多类如果我要做到这一点需要如



.Exclude< ARepository>()
.Exclude< BRepository>()
.Exclude< CRepository>?()



和1&安培; 2需要单独的手工绑定?或1可以用 BindToSelf()和 .Configure(B => b.InRequestScope())来完成`


解决方案

是的,它可以使用惯例绑定和单一绑定在同一个项目中,甚至在同一个模块。

  IBindingRoot.Bind(X => X 
.FromThisAssembly()
.IncludingNonePublicTypes()
.SelectAllClasses()
.InheritedFrom(typeof运算(IRepository<>))
.BindDefaultInterface()
.Configure(Y => y.InRequestScope()));



然而,你将不能够构造函数的参数传递给特定的类。 。因此,我建议更换与包装了访问配置(这是一个不错的设计反正)的接口构造函数的参数



另外,您也可以做到这一点:

  IBindingRoot.Bind(X => X 
.FromThisAssembly()
.IncludingNonePublicTypes()
.SelectAllClasses()
.InheritedFrom(typeof运算(IRepository<>))
.Exclude< MessageRepository>()
.BindDefaultInterface()
.Configure(Y => Y .InRequestScope()));

IBindingRoot.Bind&所述; IMessageRepository方式>()到< MessageRepository>)
.WithConstructorArgument(apikey,AppSettingsManager.GetSmsApiKey)
.InRequestScope();



- >你可以做一 .Exclude< TRepository>()对于每个库,其中该惯例结合是不充分的。对于每一个exlucded结合,你将不得不自己specifiy之一。如上:;>作为实施 IRepository<所有类条件结合除了的类 MessageRepository , 。它获取自己的绑定



也看看这个:
https://github.com/ninject/ninject.extensions.conventions/wiki/Projecting-Services-to-Bind

  

> IBindingRoot.Bind(X => X
.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom(typeof运算(的IFoo))
.BindDefaultInterface()
.Configure(γ= GT; y.InRequestScope()));


IBindingRoot.Bind(X => X
.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom(typeof运算(IBAR))
.BindToSelf()
.Configure(Y => y.InRequestScope()));



是完全确定。


I like to bind bellow code with Ninject auto binding. Is it possible to use both mannual & auto binding within a single project? Let;s take bellow manual binding, I want to achieve with auto binding. Please tell me how to achieve this.

  1. kernel.Bind<TestContext>().ToSelf().InRequestScope();

  2. kernel.Bind<IUnitOfWork<TestContext>>().To<UnitOfWork<TestContext>>();

Bellow all interface inherited from base Interface : IRepository< Model >

3 . kernel.Bind<IUserRepository>().To<UserRepository>();

4 . kernel.Bind<IAccountRepository>().To<AccountRepository>();

5 . kernel.Bind<IMessageRepository>().To<MessageRepository>().WithConstructorArgument("apikey", AppSettingsManager.GetSmsApiKey)

Additional

Do I need to write .Exclude<MessageRepository>() for multiple classes If I required to do it such as

.Exclude<ARepository>() .Exclude<BRepository>() .Exclude<CRepository>() ?

and for 1 & 2 is required separate manual binding ? or 1 can be done using BindToSelf()' and.Configure(b => b.InRequestScope())` ?

解决方案

Yes, it is possible to use convention binding and single binding in the same project, even in the same module.

IBindingRoot.Bind(x => x
    .FromThisAssembly()
    .IncludingNonePublicTypes()
    .SelectAllClasses()
    .InheritedFrom(typeof(IRepository<>))
    .BindDefaultInterface()
    .Configure(y => y.InRequestScope()));

However you won't be able to pass a constructor argument to a specific class. So i suggest replacing the constructor argument with an interface which wraps the access to the configuration (that's a nice design anyway).

Alternatively you can also do this:

IBindingRoot.Bind(x => x
    .FromThisAssembly()
    .IncludingNonePublicTypes()
    .SelectAllClasses()
    .InheritedFrom(typeof(IRepository<>))
    .Exclude<MessageRepository>()
    .BindDefaultInterface()
    .Configure(y => y.InRequestScope()));

IBindingRoot.Bind<IMessageRepository>().To<MessageRepository>)
    .WithConstructorArgument("apikey", AppSettingsManager.GetSmsApiKey)
    .InRequestScope();

--> You can do one .Exclude<TRepository>() for every repository, where the convention binding is not sufficient. For every exlucded binding you will have to specifiy one yourself. As above: conditional binding for all classes implementing IRepository<> except class MessageRepository, which gets its own binding.

Also have a look at this: https://github.com/ninject/ninject.extensions.conventions/wiki/Projecting-Services-to-Bind

Addendum: Note that you can specify multiple conventional bindings, for example:

IBindingRoot.Bind(x => x
    .FromThisAssembly()
    .SelectAllClasses()
    .InheritedFrom(typeof(IFoo))
    .BindDefaultInterface()
    .Configure(y => y.InRequestScope()));


IBindingRoot.Bind(x => x
    .FromThisAssembly()
    .SelectAllClasses()
    .InheritedFrom(typeof(IBar))
    .BindToSelf()
    .Configure(y => y.InRequestScope()));

Is totally ok.

这篇关于如何使用Ninject的公约扩展绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆