Ninject and configuration

坚强是说给别人听的谎言 提交于 2019-12-02 21:17:22

问题


I used to use Castle as an IoC but I had a problem using Nhibernate/Castle(IoC) in the same project so i moved to Ninject. Now to get to the question, I have this class:

class CustomModule :  NinjectModule
{
   public override void Load()
   {
       Bind<Interfaces.ICafe>().To <Concrete.Tea>();
   }
}

Concrete is a separate project and Interfaces.ICafe is a different project. With Castle I used to give a developer the interfaces DLL and ask him to implement a new concrete implementing that interface and then configure that with the app.config, so no matter what class name he implements it still works because he has to write that in the app.config, so if he made it like Concrete.Coffee it would still work.

But with Ninject he has to make a concrete with the same class name "Tea" in order to make it work otherwise it wouldn't work because it is hard coded.

I'm new to Ninject and i know there is probably something I'm missing ?


回答1:


There's been a general trend (among the folks I know or follow, anyway) to moving IoC bindings into code and out of XML. Mostly b/c you gain intellisense and runtime feedback of screw-ups. So, yes, you have to have a reference to Concrete.Tea if you're going to do things that way.




回答2:


If you want to avoid referencing the concrete implementation, you can use the conventions extension to load the implementation at runtime.

-Ian




回答3:


I haven't used it but there are release candidates for Ninject.Extensions.Xml which allows you to setup your mappings in xml. However I have to agree with Paul and I would generally shy away from doing it this way. There's also Ninject.Extensions.Conventions which might be what you're after.



来源:https://stackoverflow.com/questions/4639689/ninject-and-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!