3

We have hit an issue where we would like to inject a dependency into an object that has been constructed via reflection:

        Type _type = Type.GetType(className, true, true);
        ConstructorInfo _ctor = _type.GetConstructor(new[] { typeof(MyClass) });
        IReg _reg = (IReg)_ctor.Invoke(new object[] { _myClass });

The injection does not seem to be happening here using property injection. Is it not possible? How can we get around this issue? Thanks.

KirEvse
  • 305
  • 3
  • 15

3 Answers3

1

You can do property inject on the object after it's construction via reflection with the kernel.Inject(Object) method on IKernel. But this WILL be post construction and you will not get any constructor injection.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
ryber
  • 4,537
  • 2
  • 26
  • 50
  • This seems like the best way so far. But how do I get to my Kernel? Create a new one? It is instantiated at the web service application level (we are using WCF extension). Along with the bindings. And our instantiation logic executes at a business logic level (different assembly) which is unaware it is used by web services. – KirEvse Aug 17 '11 at 14:16
  • The Dependency Injection pattern is a generalization of "classical abstract class factory" + new features(wiring dependencies based on ctor/properties, objects life cycle, ...). So it should be used like a abstract class factories: in setup application (ex: *main* function) you should define "class factory" and pass to component (ex: your business logic assembly) as an abstraction. In our case the abstract class factory is IKernel. How to pass kernel to your to your assembly? pass a instance for each call to business that needs. or define a singleton for it in your business logic assembly. – Nicolae Dascalu Aug 18 '11 at 00:42
  • You will have to figure out some way to get the kernel. Check out this question for some ideas on legacy systems http://stackoverflow.com/q/6562872/154477 – ryber Aug 18 '11 at 20:22
0

To use the dependency injection framework you shouldn't instantiate object by itself using new or reflection you should call the injection framework. In your case:

//setup the application
IKernel kernel = new StandardKernel();
//add all your other bindings for property injection
...

kernel.Bind<MyClass>.ToConstant(_myClass); //if same _myClass instance is used than this can go in setup application. 
Type _type = Type.GetType(className, true, true);
var _reg = kernel.Get(_type);
kernel.Unbind<MyClass>(); //cleanup the global kernel

Any way the dependency injection framework is used to avoid using reflection for object instantiation + wiring it's dependencies, based on previously configuration (using Bind calls for Ninject).

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Nicolae Dascalu
  • 3,425
  • 2
  • 19
  • 17
  • Unfortunately, there are about 100+ classes that implement that interface. The names of those classes are retrieved dynamically and as a result also instantiated dynamically (see my example above). Which classes need to be instantiated is business logic-dependent and will be pretty difficult to refactor. – KirEvse Aug 17 '11 at 13:55
0

There's a Kernel.Get(Type), but as you want to call a specific constructor, it's no use. There is a constructor selection strategy which is customizable, which you could use to emulate what you're doing in your second line.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249