General Message Implementation – Adapter, Abstract or Nothing

By Akhil

I am going to create a generic messaging system which includes email and sms services. Both used third party services to send the messages. This general service can be used with any projects for messaging.

Currently my code like below;

1. Generic Interface

public interface IMessagingService
{
    Task<Response> SendAsync();
}

2. Generic Response

public class Response
{
    public HttpStatusCode StatusCode { get; set; }
    public virtual Dictionary<string, dynamic> ResponseBody { get; set; }
}

3. Email Service Class

This services used SendGrid provider.

public class EmailService : IMessagingService
{
    private readonly SendGridClient _client = null;
    private readonly string _apiKey = string.Empty;
    public EmailService(string apiKey)
    {
        this._apiKey = apiKey;
        this._client = new SendGridClient(_apiKey);
    }
    public string Subject { get; set; }
    public string Message { get; set; }
    public MailAddress From { get; set; }
    public MailAddress To { get; set; }
    public async Task<Response> SendAsync()
    {
        var from = new EmailAddress(From.Address, From.DisplayName);
        var to = new EmailAddress(To.Address, To.DisplayName);
        var message = MailHelper.CreateSingleEmail(from, to, Subject, Message, Message);
        var response = await _client.SendEmailAsync(message);
        return new Response()
        {
            StatusCode = response.StatusCode,
            ResponseBody = response.DeserializeResponseBody(response.Body)
        };
    }
}

SMS Service is also same type of implementation by using another provider.

I am ok with this implementation. This service will reference to existing projects as well as new projects.

But i have some confusion with Adapter pattern and Bridge Pattern.

If i implement any of this pattern is this a better design or existing is fine?

  1. I don’t want to include all of the methods from SendGrid provider class. So is this fine to use Adpater pattern and how? please mention the advantages

  2. If any advantages to use Bridge pattern here and how?

This will understand the patterns better and the better way to design to new services like this

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273