Ninject and configuration

2020-04-30 16:11发布

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 ?

3条回答
唯我独甜
2楼-- · 2020-04-30 16:39

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.

查看更多
地球回转人心会变
3楼-- · 2020-04-30 16:46

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

-Ian

查看更多
叛逆
4楼-- · 2020-04-30 16:59

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.

查看更多
登录 后发表回答