Created
April 4, 2014 14:19
-
-
Save khellang/9975602 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IEmailSettings | |
{ | |
string Host { get; } | |
int Port { get; } | |
string Address { get; } | |
string Username { get; } | |
string Password { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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