From Domain-Driven Design to Domain-Specific Languages: an example

Post on 16-May-2015

3,290 views 2 download

Tags:

description

http://fragmental.tw/2008/03/24/using-the-right-words/

transcript

from: domain-driven-designto: domain-specific language--An Example.

phillip calçadohttp://fragmental.twhttp://www.thoughtworks.com

JavaMail Usage

I want this CSV report delivered

as an e-mailattachment

ClassicExample

public class Mail {

public void sendEmailWithCsvFile(String from, String to, String subject, String body, List<String> csvFileLines) {

Properties props = new Properties(); props.put("mail.smtp.host", "localhost");

Session session = Session.getDefaultInstance(props);

MimeMessage msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(from));

InternetAddress[] address = { new InternetAddress(to) }; msg.setRecipients(Message.RecipientType.TO, address);

msg.setSubject(subject); msg.setSentDate(new Date());

MimeBodyPart part1 = new MimeBodyPart(); part1.setText(body);

MimeBodyPart part2 = new MimeBodyPart(); StringBuffer buffer = new StringBuffer(); for (String line : csvFileLines) buffer.append(line + "\n"); part2.setContent(buffer.toString(), "text/csv"); part2.setFileName("file.csv");

Multipart mp = new MimeMultipart(); mp.addBodyPart(part1); mp.addBodyPart(part2);

msg.setContent(mp);

Transport.send(msg); }}

RemovingsomeNoise

public class Mail {

public void sendEmailWithCsvFile(String from, String to, String subject, String body, List<String> csvFileLines) {

Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); MailService mailService = new MailService(props); MailMessage message = mailService.newMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setBody(body);

StringBuffer buffer = new StringBuffer(); for (String line : csvFileLines) buffer.append(line + "\n"); Attachment csvFile = new Attachment("file.csv", buffer.toString()); message.attach(csvFile);

mailService.send(message); }}

MakingitFlow

public class Mail {

public void sendEmailWithCsvFile(String from, String to, String subject, String body, List<String> csvFileLines) {

StringBuffer buffer = new StringBuffer(); for (String line : csvFileLines) buffer.append(line + "\n"); Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); MailService mailService = new MailService(props); mailService.newMessage() .from(from) .to(to) .subject(subject) .body(body) .attachTextFile("file3.csv", buffer.toString()) .send(); }}

SpeakingtheLanguage

send_message do to 'pcalcado@gmail.com' from 'bemaia@fragmental.tw' subject 'Testing via JRuby'

body %{ Hi, Please do not forget the milk. Bye! }

attach('file4.csv') { ['Sk. Milk', '$1.33'] ['Avocado', '$3.00'] }end

Thanks