It's all about Binding
In Chapter 1, Understanding Dependency Injection, we implemented DI manually in the MailService
class. You remember that we ignored the configuration of SmtpClient
to simplify the project. Now, we are going to add the configuration of SmtpClient
and implement DI using Ninject.
Let's start by creating the
MailConfig
class:
class MailServerConfig { public string SmtpServer { get { return ConfigurationManager.AppSettings["SmtpServer"]; } } public int SmtpPort { get { var port = ConfigurationManager .AppSettings["SmtpPort"]; return Convert.ToInt32(port); } } public string SenderEmail { get { return ConfigurationManager .AppSettings["SenderEmail"]; } } public string SenderPassword { get { return ConfigurationManager .AppSettings["SenderPassword"]; } } }
Now, we can update the MailService
class and incorporate MailServiceConfig
:
class MailService { private ILogger...