Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top