I have helper component used in multiple classes. This is typical scenario:
public class HelperComponent
{
public void HelperMethod()
{
// do something
}
}
public class MyClass
{
private static HelperComponent Helper{ get; set;}
public void Method1()
{
Helper.HelperMethod();
}
}
usage: new MyClass().Method1();
I would like to know what is advised spring.net configuration/solution for this configuration?
Initially I used this line to fetch helper component:
ContextRegistry.GetContext().GetObject("HelperComponentName")
Then I read that this isn’t preferable solution and that I should use injection in order to avoid dependency to spring and have transparent component usage.
My question is: how can I achieve this using spring configuration?
Can I inject static property into class? Or should I make Helper instance property?
If I convert Helper to instance property, do I need to define MyClass in spring configuration and use CreateObject to instantiate MyClass?
If yes, it isn’t satisfiable solution for me beacuse I would like to instantiate MyClass as written above.
Any help appreciated.
I’m not that familiar with spring, but when using an IOC container, you usually register all of your application’s dependencies (and their life cycles) and then resolve the ‘entry-point’ object at the top of the object graph.
This allows you to take advantage of features of the IOC container like constructor and property injection.
Once you are set up like this, you can manage the individual objects life cycles through the IOC containers configuration.
In this case, I would inject
HelperComponentin the constructor ofMyClassafter registering theHelperComponentas a singleton in the IOC container configuration.