Skip to main content
Added in change with removing nullable services
Source Link
Ben
  • 59
  • 3
public class EmailService
{
    private readonly Dictionary<SluRecipients, IEmailSender?>IEmailSender> _emailSenders;
    private readonly DbContextReghoundContext _context;

    public EmailService(ReghoundContext context, IServiceProvider serviceProvider)
    {
        _context = context;
        _emailSenders = new Dictionary<SluRecipients, IEmailSender?>IEmailSender>
        {
            { SluRecipients.Alert, serviceProvider.GetService<IAlertEmailSender>() ?? throw new InvalidOperationException("Service cannot be registered") },
            { SluRecipients.Company, serviceProvider.GetService<ICompanyEmailSender>() ?? throw new InvalidOperationException("Service cannot be registered") },
            { SluRecipients.Invitation, serviceProvider.GetService<IInvitationEmailSender>() ?? throw new InvalidOperationException("Service cannot be registered") }
        };
    }

    public async Task SendEmailAsync(int emailTemplateId, SendEmailRequest request)
    {
        var emailTemplate = await _context.TblEmailTemplates.FirstOrDefaultAsync(t => t.EmtpId == emailTemplateId);

        if (!_emailSenders.TryGetValue((SluRecipients)emailTemplate.ErecId, out var emailSender) || emailSender == null)
        {
            throw new InvalidOperationException($"No email sender found for key: {emailTemplate.ErecId}");
        }

        await emailSender.SendEmailAsync(emailTemplate, request);
    }

}
public class EmailService
{
    private readonly Dictionary<SluRecipients, IEmailSender?> _emailSenders;
    private readonly DbContext _context;

    public EmailService(ReghoundContext context, IServiceProvider serviceProvider)
    {
        _context = context;
        _emailSenders = new Dictionary<SluRecipients, IEmailSender?>
        {
            { SluRecipients.Alert, serviceProvider.GetService<IAlertEmailSender>() },
            { SluRecipients.Company, serviceProvider.GetService<ICompanyEmailSender>() }
        };
    }

    public async Task SendEmailAsync(int emailTemplateId, SendEmailRequest request)
    {
        var emailTemplate = await _context.TblEmailTemplates.FirstOrDefaultAsync(t => t.EmtpId == emailTemplateId);

        if (!_emailSenders.TryGetValue((SluRecipients)emailTemplate.ErecId, out var emailSender) || emailSender == null)
        {
            throw new InvalidOperationException($"No email sender found for key: {emailTemplate.ErecId}");
        }

        await emailSender.SendEmailAsync(emailTemplate, request);
    }

}
public class EmailService
{
    private readonly Dictionary<SluRecipients, IEmailSender> _emailSenders;
    private readonly ReghoundContext _context;

    public EmailService(ReghoundContext context, IServiceProvider serviceProvider)
    {
        _context = context;
        _emailSenders = new Dictionary<SluRecipients, IEmailSender>
        {
            { SluRecipients.Alert, serviceProvider.GetService<IAlertEmailSender>() ?? throw new InvalidOperationException("Service cannot be registered") },
            { SluRecipients.Company, serviceProvider.GetService<ICompanyEmailSender>() ?? throw new InvalidOperationException("Service cannot be registered") },
            { SluRecipients.Invitation, serviceProvider.GetService<IInvitationEmailSender>() ?? throw new InvalidOperationException("Service cannot be registered") }
        };
    }

    public async Task SendEmailAsync(int emailTemplateId, SendEmailRequest request)
    {
        var emailTemplate = await _context.TblEmailTemplates.FirstOrDefaultAsync(t => t.EmtpId == emailTemplateId);

        if (!_emailSenders.TryGetValue((SluRecipients)emailTemplate.ErecId, out var emailSender) || emailSender == null)
        {
            throw new InvalidOperationException($"No email sender found for key: {emailTemplate.ErecId}");
        }

        await emailSender.SendEmailAsync(emailTemplate, request);
    }

}
update wording
Source Link
  • Is this overcomplicatingapproach overcomplicated? 
  • Is there a simpler solution / design pattern.?
  • Is the service selector pattern in email service class bad practice?
  • Is this overcomplicating? Is there a simpler solution / design pattern.
  • Is the service selector pattern in email service class bad practice?
  • Is this approach overcomplicated? 
  • Is there a simpler solution / design pattern?
  • Is the service selector pattern in email service class bad practice?
update formatting, Make title describe what code does per site convention - see https://codereview.stackexchange.com/help/how-to-ask
Source Link

Best Design Practice for replacing Replacing tags in email

I have a dictionary and register a different email sender depending on the enum like sothis:

Then in a concrete email sender I can override the methods can be overridden like sothis:

Then in each email sender I will replace content like sothis (this is done inside aan IContentProcessor and will have a different one for each email sender.)

This is the basic functionallityfunctionality I am going with but this solution seems flawed in many ways. Has anyone done something similar and have a better approach than this?

Concerns Is this overcomplicating? Is there a simplier solution / design pattern. Is the service selector pattern in email service class bad practice?

Concerns

  • Is this overcomplicating? Is there a simpler solution / design pattern.
  • Is the service selector pattern in email service class bad practice?

Best Design Practice for replacing tags in email

I have a dictionary and register a different email sender depending on the enum like so

Then in a concrete email sender I can override the methods like so

Then in each email sender I will replace content like so (this is done inside a IContentProcessor and will have a different one for each email sender.)

This is the basic functionallity I am going with but this solution seems flawed in many ways. Has anyone done something similar and have a better approach than this?

Concerns Is this overcomplicating? Is there a simplier solution / design pattern. Is the service selector pattern in email service class bad practice?

Replacing tags in email

I have a dictionary and register a different email sender depending on the enum like this:

Then in a concrete email sender the methods can be overridden like this:

Then in each email sender I will replace content like this (this is done inside an IContentProcessor and will have a different one for each email sender.)

This is the basic functionality I am going with but this solution seems flawed in many ways. Has anyone done something similar and have a better approach than this?

Concerns

  • Is this overcomplicating? Is there a simpler solution / design pattern.
  • Is the service selector pattern in email service class bad practice?
added 164 characters in body
Source Link
Ben
  • 59
  • 3
Loading
added 187 characters in body
Source Link
Ben
  • 59
  • 3
Loading
edited tags
Link
Ben
  • 59
  • 3
Loading
Source Link
Ben
  • 59
  • 3
Loading