views:

111

answers:

2

Hi

I am new to IOC, so I wanted to know the best strategy to handle the case where I want to pass data structures/paramters as well as injected objects into a class, like

simple example:

public class EmailSender
{

    public EmailSender(string ToEmail, string Subject,String body,
                       ILogger EmailLogger)
    {.....}
}

What is the best strategy here? I guess the above is no possible to inject directly?

I guess I need to put all the string parameters as setters instead and just have the Ilogger in the constructor? or the other way around.

Or am I wrong?

//andy

PS, I know the example above sucks and ToEmail and Body should be passed in a separate method call, but it was just to make a example. I will see the other post as well.

+1  A: 

I think this is a duplicate of this question, but since that one was one I asked I'll let one of the other mods decide whether to close this.

Matt Hamilton
+2  A: 

No, you should be able to specify the strings in the constructor call. Admittedly I'd usually expect those to be more "transient" values passed in as method arguments:

public class EmailSender
{
    private readonly ILogger emailLogger;

    public EmailSender(ILogger emailLogger)
    {
         this.emailLogger = emailLogger;
    }

    public void SendEmail(string toEmail, string subject, string body)
    {
         // ...
    }
}

That way the same EmailSender can be used to send many emails - the details of the email itself "flow through" the sender rather than being part of it.

EDIT: Given the edit to the question, it's not entirely clear what remains. If you're really asking how to specify strings as constructor arguments, that will depend on the IoC framework you're using. If you could specify the framework, we could probably give you the appropriate syntax.

Jon Skeet