BMBM
BMBM

Reputation: 16013

Can I use Spring.NET to inject dependencies in a created instance?

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.

Upvotes: 2

Views: 1073

Answers (2)

BennyM
BennyM

Reputation: 2856

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.

Upvotes: 4

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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

Upvotes: 1

Related Questions