mercredi 3 août 2022

Strategy pattern but with different initializer for classes

I am currenttly working on a messenger library for emails and want to send emails in different ways. This should be achived by initializing the needed object and then sending an email.

Because the resulting function is always the same, namely it needs a receiving E-mail address, a message text and maybe an attachment, I thought that it makes sense to use a strategy pattern in order to make the code more maintainable.

This is the interface, which will always be the same:

public interface IMailService
{
    void SendEMail(List<string> toEMails, string subject, string messageText);
    void SendEMail(List<string> toEMails, string subject, string messageText, List<Attachment> attachments);
}

The problem however is that the E-mail classes, which implement this functionality are initialized differently.

The smtp Version for example looks like this:

     public SmtpMailService(string server, int serverPort, bool enableSsl, string username, string password, string fromAddress)
    {
        _fromAddress = fromAddress;
        _mailClient = new SmtpClient(server);

        _mailClient = new SmtpClient(server, serverPort)
        {
            Credentials = new NetworkCredential(username, password),
            EnableSsl = enableSsl
        };
        _mailClient.Timeout = 1000000;
        
    }

And the exchange class is initialized this way:

    public void Initialize(string server, string username, string password, string domain, ExchangeVersion exchangeVersion, string fromAddress)
    {
        ExchangeService svc = new ExchangeService(exchangeVersion);
        svc.Credentials = new NetworkCredential(username, password, domain);
        svc.Url = new Uri(string.Format("https://{0}/ews/exchange.asmx", server));

        exchangeService = svc;
    }

It is also planned to implement other ways to send emails using different services.

How can I create a strategy pattern out of this? Or am I having the wrong idea and is a different pattern better?

Aucun commentaire:

Enregistrer un commentaire