I want to create an object with a constructor containing predicate and func objects in the xml config using spring. The Predicate and the Func arguments should point to a method of another configured object. How is this possible using Spring.net? I was not able to find a solution or hint in the documentation...
A sample constructor would be:
MyClass(Predicate<TInput> condition, Func<TInput, TOutput> result)
I would say you have to extract the predicate and the function and put them behind an interface. Then you can use this interface in your constructor. If your using constructor injection most of the times you specify the dependencies as interfaces or types. The constructor in your example uses 2 instance variables (in this case pointing to a method).
You could create an interface like this:
And use this interface as constructor param, which gives you the exact same result as you can have your 'other configured object' implement this interface.
I solved the problem using an intermediate "factory" inspired by the suggestion of thekip but leaving the object requiring the predicate and func in the constructor (MyClass in the example above) untouched. This solution keeps the "problem" of handling delegates in spring outside of the implementation of MyClass (in opposite of the suggestion of thekip, but thanks for your answer).
Here is my way to configure such a situation (setting delegates in Spring.Net, using object based factory pattern).
The MyDelegator class a sample implementation for predicate/func to use (object here, but could be anything else fitting to the predicate/func params):
... then you need a container for the object with the predicate and or func (could be in seperate classes, too) ...
... and this can be configured in Spring.Net xml this way
And now you can use this anywhere in your config for objects with predicate/func (no need to change the class requiring the predicate/func)
That's it. Any suggestions to improve this solution? Maybe, Spring.net already has a generic solution for this...
It is also possible to use the DelegateFactoryObject within Spring.net to create delegates (Action, Func, Predicate are only special delegates):
So you're not forced to create a construct such the MySpringConfigurationDelegateObjectContainer mentioned above to forward the delegates through factory methods anymore.