I have a email service which passes an email model through the constructor and that all using the same base class. For example one model could be for authentication, another model for password reset. My problem is how to allow the service to pass an anonymous model with the same base class.

sample code:

public class EmailService<T> : IEmailService 
    private readonly T _emailModel;
    private readonly EmailType _emailType;
    private readonly IEmailRepository _emailRepository;
    private MailBuilder _mailBuilder;
    private EmailTemplates _message;

    public EmailService(T emailModel, EmailType emailType, IEmailRepository emailRepository)
        _emailModel = emailModel;
        _emailType = emailType;
        _emailRepository = emailRepository;


    private void getMessage()
        _message = _emailRepository.GetTemplateByUser((int)_emailType, _emailModel.UserTypeId);

    private void constructEmail()
        _mailBuilder = new MailBuilder(_message, _emailType, ObjectConverters.ConvertProperiesToDictionary(_emailModel)); 

    public void Send()
        EmailSettings emailSettings = SiteSettingsService.SiteConfiguration.EmailSettings;

        MailSettings settings = new MailSettings
            MailFrom = emailSettings.MailFrom,
            MailSmtpHost = emailSettings.SmtpHost,
            MailSmtpPort = emailSettings.SmtpPort,
            EnableSsi = emailSettings.EnableSsi,
            MailSmtpUsername = emailSettings.SmtpUsername,
            MailSmtpPassword = emailSettings.SmtpPassword

        new EmailSender(settings).SendEmail(_emailModel.EmailAddress, _message.Title, _mailBuilder.HtmlTemplate);

Related posts

Recent Viewed