Auto Factories with parameters

Oct 5, 2010 at 11:04 PM

I readed a topic about this a while ago, but i can't find the topic right now. You were discussing that unity auto factories doesn't support parameters. The reason you gave was that there were many ambiguities about wich parameters should be injected and wich not. I found an scenario where having parameterized factories could be helpfull and unambiguous. Supose you are resolving to a base class instead of to an interface. Say for example the class EmailAddress.

This class might look like this:

public abstract class EmailAddress
    private string  _address;
    public EmailAddress(string address)
        _address = address;

    public abstract Send(string message, string subject, string destination);

You probably want to inject specific parameters on the concrete implementation of this class, but you'll probably wan't to specify the address manually always.

What do you think about this topic?

Oct 6, 2010 at 12:34 AM

Well, the specific example doesn't strike me as very convincing. The abstraction is poor - you've conflating the email address with the means of sending it. And I don't see how this clears up the ambiguity. Ok, you've got a derived class now:

public class Pop3EmailAddress : EmailAddress
    public Pop3EmailAddress(string address, string server)
        : base(address)
    // other stuff here

And you ask for a factory of type Func<string, EmailAddress>. Which string is it - the address or the server?

The solution here is to be explicit, and to create an explicit factory interface or delegate implementation, and register that.