English 中文(简体)
firsts steps with an IoC because I hit a wall, please explain the behind the scenes
原标题:

So I started this new project, and I was trying to incorporate all the new design principles I was reading about, namely trying to make things loosely coupled, testable, and following some patterns.

So I then ran into the issue of having to pass too many factories/managers into my classes constructor, which led me into Dependancy injection.

public class SomeClass
{
   public SomeClass(IDBFactory factory, IOrderManager orderManager, ....)
}

So if I use ninject, from what I understand, I would then bind a particular implementation to the class.

So what is going on behind the scenes?

NInject will, whenever I instantiate SomeClass, it will bind the implementation that I defined in the config file?

i.e.

I do:

ISomeClass sc = NInject.Get<ISomeClass>();

and ninject will do:

new SomeClassImpl(pass in all the implementaitons in the constructor)

correct?

最佳回答

I don t know NInject, but most DI Containers support Auto-Wiring, which works this way:

  1. When you request ISomeClass, it looks through its list of all registered types. Using this list, it discovers that the desired implementation of ISomClass is SomeClass.
  2. It will use SomeClass constructor to create an instance (perhaps using Activator.CreateInstance), so it uses Reflection to figure out which paramters are required.
  3. For each paramameter, it looks at the type and repeats step 1-2 for each.

Thus, the process may be recursive, but in the end, you should end up with a fully populated object graph where all dependencies are satisfied.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签