Where do I start?
Over the course of learning to program one runs into design patterns, architectural choices etc. For me, I understand conceptually DI, IoC and why they are needed/good. Modularity, low coupling high cohesion - I get it.
For example, I am building a small test website with the help of the MVP pattern, where the presenter doesn't have a concrete View, but uses an interface that the View implements. It also has a reference to a service layer (which sits between the presenter and the BLL), again, nothing concrete, using an interface for that. All good stuff.
The (manual) IoC is making the instantiating of concrete objects pushed upwards, to the point that objects need to be injected somewhere, someplace. In other words, the dependency is still there, it is just higher up. Enter IoC containers. And enter frustration.
I know they are there, I know what they are used for. I opted to use ninject. Cool. So, I start looking. In my download I have bunch of files:
- LICENSE.TXT
- Ninject.dll
- Ninject.pdb
- Ninject.xml
another folder, called extensions
- CommonServiceLocator.NinjectAdapter.dll
- CommonServiceLocator.NinjectAdapter.pdb
Okay... a bunch of files. Which to use? I do not know. Where do I place them? Do I need all of them? Questions questions...
Maybe read some manual. Ninject wiki @ github. Right. I start reading - why use Ninject? Dependency Injection By Hand, Dependency Injection With Ninject - nice examples about swords and daggers and such. But I don't read anywhere how to actually do/use it. Where do I place it in Visual Studio? How to call it? How?
Also, it presents me with this code:
Bind<IWeapon>().To<Sword>();
It says that everytime IWeapon is called, an implementation of sword is given. Eh... okay, there was a dagger as well - maybe I don't want to get a sword everytime I have e.g. IWeapon as parameter in my constructor, but how to do that? It doesn't say! Am I stuck with a sword everytime I use IWeapon? If not, what is the code to change that then? And how?
It says that when you have a bunch of constructors, it will only take the one with the most parameters. Okay. What does it do with the constructors that have a little less parameter than the constructor with the most? Can't I use ninject with these constructors? Or... how does it work? Again - it doesn't say anywhere. Curses!
In the wiki, there's a link to the Most Controversial Post on SO. It is about a guy named Joel how says some things about IoC that enabled the flame war/nerdrage. But you know what? I dare to say that I kinda root for this guy. Why? Maybe the concept of IoC is simple, but by god - getting to the point of finally knowing how to use it, what to do in case of certain situations etc. is really really frustrating. Searching google mostly turns up either nothing useful, or indeed a lot to read with convoluted stuff inbetween. I find that the guy might be right in some sense - in that it is really hard to understand concepts and explain to someone who doesn't understand the things you find simple.
Why can't it be more like "Download the files - get this file out and place it somewhere handy, then go to Visual Studio, make a new map and 开发者_如何学Goreference the dll. To use it do these steps" and then explaining every step with a very good and detailed why. No snippets of code. It is frustrating.
So, one might wonder what my question is. Well, I want use Ninject. What do I need? where do I place it? How do I get it to work? Do I have to be stuck with a sword everywhere I have IWeapon? What am I doing at work when the sun is shining outside?
I have a very simple set of example code for ninject that I made for some of our developers when we introduced it to our project. It just covers basic bindings and such right now. Not so much with how it works with MVC or more advanced topics like contextual bindings. It's all done as nUnit tests so I hope it's easy to follow. What it does NOT show is how to properly work it into your project. I hope to soon do a true example MVC project.
https://github.com/ryber/Ninject-Examples
Hope that helps.
For web projects I recommend using MVC3. See https://github.com/ninject/ninject.web.mvc/wiki/MVC3 about getting it to work with Ninject.
精彩评论