Search code examples
delegatesinversion-of-controlspring.netfactory-pattern

Factory configured with Spring returning new Instances for calls at Runtime


Is there an easy way to use Spring IoC for a factory which returns a new instance at every call during runtime (after configuration and context creation)?

I would prefer to avoid a strong dependency to Spring and also avoid calling GetObject("myFacobj") everytime... I am looking for something like a method pointer injection instead (as I would do when programming in C instead of C#).

Maybe it is possible to inject a delegate to a factory call using Spring?

Some inspiration might by the post How to inject Predicate and Func in Spring.net about injection of delegates (Func<>, ...) but I would prefer an "Spring-Built-In-Solution", if possible.


Solution

  • Spring.NET supports method injection, which could be a good fit here.

    I've posted an example in this other answer of mine on how you might use method injection as an alternative to using context.GetObject(...), so that you don't have to take a dependency the di container.