While working on an enterprise integration project about virtual bank payments, I utilized a simple fluent interface approach. The purpose of the code is creating strongly typed objects from a generic SMO (ServiceMessageObject) instance.
Note: In the context of IBM integration technologies, ServiceMessageObject represents the “message”. In our example, it represents a Web Service request.
Old Code:
if (paymentInfo.getDataObject("totalAmount") != null) {
totalAmount = paymentInfo.getDataObject("totalAmount");
DataObject totalAmount2 = totalAmount.getDataObject("totalAmount");
if (totalAmount2.getBigDecimal("amount") != null) {
parameters.setAmount(totalAmount2.getBigDecimal("amount"));
}
String currencyCode = totalAmount2.getString("currencyCode");
parameters.setCurrencyCode(currencyCode);
}
Byte numberOfInstallments = paymentInfo.getByte("numberOfInstallments");
if (numberOfInstallments != null) {
parameters.setNumberOfInstallments(numberOfInstallments);
}
if (transactionInfo.size() != 0) {
if (((DataObject) transactionInfo.get(0)).getString("orderID") != null) {
parameters.setOrderId(((DataObject) transactionInfo.get(0))
.getString("orderID"));
if (((DataObject) transactionInfo.get(0))
.getString("transactionID") != null) {
parameters.setTransactionID(((DataObject) transactionInfo
.get(0)).getString("transactionID"));
}
parameters.setBankRefId(((DataObject) transactionInfo.get(0)).getString("bankReferenceID"));
//...more
It is really painful to read and modify this code. But actually, what it does is really simple. It should
look simple.
New Code:
parameters.setCreditCardNumber(body
.getDataObject("paymentInfo")
.getDataObject("creditCardInfo")
.getString("creditCardNumber"));
parameters.setCreditCardExpiryMonth(body
.getDataObject("paymentInfo")
.getDataObject("creditCardInfo")
.getByte("creditCardExpiryMonth"));
parameters.setAmount(body
.getDataObject("paymentInfo")
.getDataObject("totalAmount")
.getDataObject("totalAmount")
.getBigDecimal("amount"));
parameters.setTransactionID(body
.getListItem("transactionInfo")
.getString("transactionID"));
This is a simple example of an internal DSL. The point I want to stress is this: “Use internal DSLs whenever you need better readability, among other things.”
This simple refactoring helped us a lot to read/modify the code easier than before.
To implement the DSL, what I did is hiding some details in a class that I named DataObjectWrapper. This class contains IBM's DataObject:
public class DataObjectWrapper {
private DataObject dataObject;
public DataObjectWrapper getListItem(String string) {
List list = dataObject.getList(string);
if (list.size() != 0)
return new DataObjectWrapper((DataObject)list.get(0));
return new NullDataObject();
}
public DataObjectWrapper getDataObject(String field) {
if (dataObject.getDataObject(field) != null)
return new DataObjectWrapper(dataObject.getDataObject(field));
return new NullDataObject();
}
public String getString(String field) {
return dataObject.getString(field);
}
public Byte getByte(String field) {
return dataObject.getByte(field);
}
}
To support fluent statements, NullDataObject is an important abstraction:
public class NullDataObject extends DataObjectWrapper {
@Override
public DataObjectWrapper getDataObject(String string) {
return new NullDataObject();
}
@Override
public String getString(String field) {
return null;
}
// ...
}
Comments