Skip to content

Instantly share code, notes, and snippets.

@khellang
Created April 4, 2014 14:19
Show Gist options
  • Save khellang/9975602 to your computer and use it in GitHub Desktop.
Save khellang/9975602 to your computer and use it in GitHub Desktop.
public interface IEmailSender
{
void Send(string recipient, string subject, string htmlBody);
}
public class EmailSender : IEmailSender
{
private readonly IEmailSettings _settings;
public EmailSender(IEmailSettings settings)
{
_settings = settings;
}
public void Send(string recipient, string subject, string htmlBody)
{
var credentials = new NetworkCredential(_settings.Username, _settings.Password);
using (var client = new SmtpClient(_settings.Host, _settings.Port) { Credentials = credentials })
{
client.Send(new MailMessage(_settings.Address, recipient, subject, htmlBody) { IsBodyHtml = true });
}
}
}
public interface IFileSystem
{
bool FileExists(string path);
string GetFileNameWithoutExtension(string path);
string ReadAllText(string path);
}
public class FileSystem : IFileSystem
{
public bool FileExists(string path)
{
return File.Exists(path);
}
public string GetFileNameWithoutExtension(string path)
{
return Path.GetFileNameWithoutExtension(path);
}
public string ReadAllText(string path)
{
return File.ReadAllText(path);
}
}
public interface IEmailSettings
{
string Host { get; }
int Port { get; }
string Address { get; }
string Username { get; }
string Password { get; }
}
public interface ITemplateTransformer
{
string Transform<T>(string templatePath, T model);
}
public class TemplateTransformer : ITemplateTransformer
{
private readonly IFileSystem _fileSystem;
public TemplateTransformer(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
public string Transform<T>(string templatePath, T model)
{
if (!_fileSystem.FileExists(templatePath))
{
throw new FileNotFoundException(string.Format("Could not find template '{0}'.", templatePath));
}
var templateName = _fileSystem.GetFileNameWithoutExtension(templatePath);
var template = _fileSystem.ReadAllText(templatePath);
using (var handlebars = new Handlebars())
{
handlebars.RegisterTemplate(templateName, template);
return handlebars.Transform(templateName, model);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment