tags:

views:

122

answers:

2

Is it possible to to something like this using Spring.NET IoC container?

MyClass instance = new MyClass();
ContextRegistry.GetContext().InjectDepenencies(instance);
// instance has the defined dependencies set

This would come in handy. Of couse I could use ContextRegistry.GetContext().Get("name") but that would create a new instance of the defined object. I would need to set the dependencies of an already created object.

A: 

ContextRegistry.GetContext().Get("name") will not create a new instance if name is defined as singleton which is the default scope in spring.net.

Darin Dimitrov
Thats true, but singleton behaviour is not an option in this case.
Max
If you use Spring to create the object instead of doing it manually it will already have the dependencies injected. So could you elaborate a little more on your scenario?
Darin Dimitrov
I would use this e. g. when the object was not created in any of my code but outside of the scope of my app, like in a third party framework.
Max
+4  A: 

There are three options available, the first one matches what you want.

  • IApplicationContext.ConfigureObject(object target, string name)

    This configures the target object using the object definition which is matched by the name argument.

  • IApplicationContext.Get(string name, object[] arguments)

    Which will either use the constructor or a static factory method which will receive the arguments as specified.

  • GenericApplicationContext.RegisterObjectDefinition(string name, IObjectDefinition definition)

    You can use it to register dependencies at runtime.

BennyM