Injection of Delegates and Unity Configuration

May 29, 2010 at 6:47 PM
Edited Jun 1, 2010 at 2:06 PM

Hello, I have some trouble understanding how to write the Unity configuration for injecting delegates. My class is like this:


    public class MyService
        public MyService(Func<string, string> stringProcessor)
            // Save stringProcessor for later use.

        // ...


and I have another class that implements a delegate:


    public class MyStringProcessor
        string Processor(string input)
            var output = input;
            // Process output...
            return output;


How should the configuration look to inject a new MyStringProcessor().Processor into the MyService constructor?

I haven't found any example of this in the forums.


PS: I have obviously some trouble posting as well... How can I format my post???

Edit: reformatted my post

May 29, 2010 at 9:26 PM

Codeplex occasionally decides not to render the post editor - I've never found an explanation why. I'll have to ask them sometimes. When it doesn't show up just use straight html and it'll work. Giant PITA though.

Now, as for your question - you can't do what you're asking in configuration right now. You'll have to use the API. To do that sort of thing in full generality would require writing a C# parser for configuration, which is a little beyond our project's scope. That having been said, it's certainly possible to come up with a useful subset, but there's no concensus yet on what that subset would look like.


Jun 1, 2010 at 2:13 PM

Thanks Chris.

I reformatted my post and this time it worked...

So I guess that if I want to achieve a similar result via configuration, then I should define an interface, have MyStringProcessor implement it and change the service to accept an interface instead of a delegate. Unity will then be able to resolve the dependency.