Skip to content

Implement Sending Notification and Template Rendering #157

@ilhesam

Description

@ilhesam

One of the needs of any software is sending notifications like email to the users.
We want to make implementation of notification services easier for developers.

Use cases:

  • Sending a notification with static template like marketing emails
  • Sending a notification with dynamic template like forgot password email

We have to handle two purposes in this phase:

  • CRUD and Rendering notification templates
  • Sending notifications with static or dynamic template

uBeac.Core.Notification.Abstractions

We should have a class for the notifications:

public class Notification
{
    public NotificationReceptor Receptor { get; set; } 
    public string Subject { get; set; } // "Welcome to uBeac!"
    public string Body { get; set; } // "Hello Hesam, Your account has been created successfully!"
}

public class NotificationReceptor
{
    public string Receptor { get; set; } // "[email protected]"
}

public class EmailReceptor : NotificationReceptor
{
    public string Cc { get; set; }
    public string Bcc { get; set; }
}

And the notification provider interfaces:

public interface INotificationProvider
{
    Task SendAsync(Notification notification, CancellationToken cancellationToken = default);
}

We don't implement any providers currently. The end developer should implement the notification providers.

uBeac.Core.Notification.Templates.Abstractions

public class NotificationTemplate
{
    public string UniqueKey { get; set; } // "sign-up-email"
    public string Subject { get; set; } // "Welcome to {{SiteName}}"
    public string Body { get; set; } // "Hello {{Name}}, Your account has been created successfully!"
}

Also, we should create a repository and service with the CRUD methods for notification templates + template renderer interface:

public interface INotificationTemplateRepository // inherits from base repository
{
    // CRUD methods
}

public interface INotificationTemplateService // inherits from base repository
{
    // CRUD methods
}

public interface INotificationTemplateRenderer
{
    string Render(string template, object model);
}

And, we should implement the interfaces in the another packages:

  • uBeac.Core.Notification.Templates.Repositories.MongoDB
  • uBeac.Core.Notification.Templates.Services
  • uBeac.Core.Notification.Templates.Renderers.Mustache

uBeac.Core.Notification

Finally, We should have a notification service like this:

public interface INotificationService
{
    // Just sends the notification (base methods)
    Task SendAsync(Notification notification, CancellationToken cancellationToken = default);
    Task SendAsync(INotificationProvider provider, Notification notification, CancellationToken cancellationToken = default);

    // First renders the template and Then sends the notification (easy-to-use methods)
    Task SendAsync(NotificationTemplate template, object model, NotificationReceptor receptor, CancellationToken cancellationToken = default);
    Task SendAsync(string templateUid, object model, NotificationReceptor receptor, CancellationToken cancellationToken = default);
    Task SendAsync(INotificationProvider provider, NotificationTemplate template, object model, NotificationReceptor receptor, CancellationToken cancellationToken = default);
    Task SendAsync(INotificationProvider provider, string templateUid, object model, NotificationReceptor receptor, CancellationToken cancellationToken = default);
}

Registration

The developers should can register it easily! I think the following structure is good:

services.AddNotification(options =>
{
    options.AddProvider<SmtpEmailProvider>(); 
    options.AddProvider<MessageBirdProvider>();

    options.AddMustacheTemplateRenderer();
});

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions