IoC, Mixing injections with parameters in constructor?

I am new to Inversion of Control (IoC), so I wanted to know how best to handle the case when I want to pass data structures / parameters, and also inject objects into the class.

A simple example:

public class EmailSender
{

    public EmailSender(string toEmail, string Subject, String body,
                       ILogger emailLogger)
    {.....}
}

What is the best strategy? I think this cannot be inserted directly?

I think I need to put all the string parameters as setters instead of just being Iloggerin the constructor or vice versa?

Or am I wrong?

Ps I know that the above example, and sucks toEmailand bodyshould be transferred to a separate method call, but it was just an example.

+3
source share
1 answer

, . , , "" , :

public class EmailSender
{
    private readonly ILogger emailLogger;

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

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

, EmailSender - "" , .

: , , . , , IoC. , , , .

+3

Source: https://habr.com/ru/post/1699646/


All Articles