mardi 21 avril 2020

best practice for using decorator for send notification in asp core

i need to send notification by sms or by email and i write code for this by Decorator Pattern .

but i dont know i write currect or not becuse when i need to use mutiple way (email and sms ) it not work good .

see the code :

public abstract class Notification
{
    public abstract Task<OperationResult<string>> Send(string from, string subject, string content, string userName, string password, MailboxAddress to, int port, string smtpServer);
    public abstract Task<OperationResult<string>> Send(string lineNumber, string userApiKey, string phoneNumber, string message, string secrectKey);
}

i have tow method Send becuse the are have diffrent argument .

i implement Notification in this class :

 public class SendNotification : Notification
{
    public SendNotification()
    {
    }
    /// <summary>
    /// Send Email Notifiaction
    /// </summary>
    /// <param name="from"></param>
    /// <param name="subject"></param>
    /// <param name="content"></param>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    /// <param name="to"></param>
    /// <param name="port"></param>
    /// <param name="smtpServer"></param>
    /// <returns></returns>
    public override async Task<OperationResult<string>> Send(string from, string subject, string content, string userName, string password, MailboxAddress to, int port, string smtpServer)
    {
        using (var client = new SmtpClient())
        {
            var emailMessage = new MimeMessage();
            try
            {
                client.Connect(smtpServer, port, true);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                client.Authenticate(userName, password);
                emailMessage.From.Add(new MailboxAddress(from));
                emailMessage.To.Add(to);
                emailMessage.Subject = subject;
                emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Text) { Text = content };
            }
            catch (Exception ex)
            {
                return OperationResult<string>.BuildFailure(ex);
            }
            finally
            {
                await client.DisconnectAsync(true);
                client.Dispose();
            }
        }
        return OperationResult<string>.BuildSuccessResult("Success Send Email");
    }


    /// <summary>
    /// Send Sms Function
    /// </summary>
    /// <param name="lineNumber"></param>
    /// <param name="userApiKey"></param>
    /// <param name="phoneNumber"></param>
    /// <param name="message"></param>
    /// <param name="secrectKey"></param>
    /// <returns></returns>
    public override async Task<OperationResult<string>> Send(string lineNumber, string userApiKey, string phoneNumber, string message, string secrectKey)
    {
        var token = new Token().GetToken(userApiKey, secrectKey);

        var restVerificationCode = new RestVerificationCode()
        {
            Code = message,
            MobileNumber = phoneNumber
        };

        var restVerificationCodeRespone = new VerificationCode().Send(token, restVerificationCode);
        if (restVerificationCodeRespone.IsSuccessful)
        {
            return OperationResult<string>.BuildSuccessResult(restVerificationCodeRespone.Message);
        }
        return OperationResult<string>.BuildFailure(restVerificationCodeRespone.Message);
    }
}

now i create class SendSms and class Send Email :

Email :

 public class NotificationEmail : SendNotification
{
    private readonly Notification notification;

    public NotificationEmail(Notification notification) 
    {
        this.notification = notification;
    }

    public override Task<OperationResult<string>> Send(string from, string subject, string content, string userName, string password, MailboxAddress to, int port, string smtpServer)
    {
        return base.Send(from, subject, content, userName, password, to, port, smtpServer);
    }
}

SMS :

 public class NotificationSms : SendNotification
{
    private readonly Notification notification;

    public NotificationSms(Notification notification) 
    {
        this.notification = notification;
    }

    public override Task<OperationResult<string>> Send(string lineNumber, string userApiKey, string phoneNumber, string message, string secrectKey)
    {
        return base.Send(lineNumber, userApiKey, phoneNumber, message, secrectKey);
    }
}

now i use this code by this way in my class :

SendNotification notif = new SendNotification();
NotificationSms smsSend = new NotificationSms(notif);
NotificationEmail emailSend = new NotificationEmail(smsSend);
var sendSms = await smsSend.Send(smsSetting.Result.LineNumber, smsSetting.Result.userApikey, to, content, smsSetting.Result.secretKey);
var sendEmail = await emailSend.Send(emailSetting.Result.From, "Email Confirm Code", content, emailSetting.Result.Username, emailSetting.Result.Password, to, emailSetting.Result.Port, emailSetting.Result.SmtpServer);

now i need to use the best practice for using the decorator . how can i improve my code and best design in decorator???

Aucun commentaire:

Enregistrer un commentaire