开发者

How to use Strategy and others for email sending

开发者 https://www.devze.com 2023-04-05 02:18 出处:网络
I want to use various email providers like GMail, mailgun, mailchimp etc and create a library to use. Those providers allow to send emails using SMTP, HTTP Post,

I want to use various email providers like GMail, mailgun, mailchimp etc and create a library to use. Those providers allow to send emails using SMTP, HTTP Post,

REST based APIs etc. Currently I have defined the following interfaces

interface IEmailSendStrategy // how to send email like SMTP , REST , HTTP Post etc ??
{
   void Send(IEmailSender sender);
}
interface IEmailSender      // provider like GMail , mailchimp, mailgun
{
    void SendEmail(ISendStrategy strategy, System.Net.Mail.Message message)
}

and some classes implementing these interfaces

class SMTPStrategy : IEmailSendStrategy
{
    void Send(IEmailSender sender){ // code to send }
}
class GMailSender : IEmailSender
{
    void SendMail(ISendStrategy strategy, System.net.Mail.Message message){ // code }
}

Now my question is

1) Is this a good approach to develop a EmailLib, any pitfalls or wrong design here ?

2) How can I put more data into these classes in a DesignPattern-Way

( like the usernames, pwds, ports, SSL or not, AUthentication for REST etc etc...

I thought of IData and use concrete classes for those IData ( like SMTPData, etc.. )

but each one may have its own type of SMTPData ( gmail needs SSL, different port, may use

some MD5 authentication etc, etc )

开发者_如何学JAVA

3) Any suggestions are welcome to make this a robust library ( I will put it as open source

later on )

Thanx


For me, I don't necessarily see the point in splitting out ISendStrategy and IEmailSender. I'd suggest you consider how you're going to use the code. Having briefly thought about it, I would suggest that one interface should be sufficent. Perhaps something like this...

public interface IEmailService {
    public Send(IAuthenticationDetails details, IMessage message)
}

public class GMail : IEmailService {
    public GMaily() { //... }
    public Send(IAuthentication details, IMessage message) {
        //...
    }
}

public class AnotherClasss {
    public void AMethodToSendEmail(...) {    
       // use factory or container to get instance of strategy
       strategy.Send(details, message)
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号