/// <summary>
        /// Creates the instance of a MailProvider using the given configuration.
        /// </summary>
        /// <param name="configurationLoader">The configuration to be supplied to the MailProvider. The configuration key value pairs are specific to providers.</param>
        /// <param name="mapper">The object mapper, used for mapping between E-Mail domain models and models used by third-party provider libraries.</param>
        /// <returns>The instantiated MailProvider.</returns>
        public IMailProvider GetProvider(IConfigurationLoader configurationLoader, IMapper mapper)
        {
            if (configurationLoader == null)
            {
                throw new ArgumentNullException(nameof(configurationLoader));
            }

            IMailProvider mailProvider = null;

            var config = configurationLoader.GetConfiguration();

            if (config.TryGetValue("MailProvider", out string provider))
            {
                if (!string.IsNullOrWhiteSpace(provider))
                {
                    switch (provider)
                    {
                    case nameof(Provider.SendGrid):
                        mailProvider = new SendGridProvider(configurationLoader);
                        break;

                    default:
                        throw new ArgumentException("Unknown provider.");
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("Configuration is missing MailProvider.");
            }

            return(mailProvider);
        }
Ejemplo n.º 2
0
        public SendGridProvider(IConfigurationLoader configuration)
        {
            _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
            _configuration.GetConfiguration();

            if (configuration.GetConfiguration().TryGetValue("SendGridApiKey", out string apiKey))
            {
                _client = new SendGridClient(apiKey);
            }
        }