Monday, December 25, 2006
Format source code in Google Blogger
One of most effective way to express a programming idiom is showing some piece of source code as example. In Google Blogger, when we put source code inside <pre> </pre> html markup, it will be show very nice with proper identation.
Labels:
writing
Sunday, December 24, 2006
What should DTO contains
Martin Fowler define DTO as object that carries data between processes in order to reduce the number of method calls. DTO is offen used to transfer data between remote client and server therefore serialization/deserialization mechanism on both side are required. The question is what should a DTO contains and what should be the structure of a DTO ? The general consensus is that DTO should be simple acyclic structure and contains only primitive data type (e.g. string, number, etc.) or other DTO. My view is that such restriction will make DTO easy to write/read to/from dictionary structure like map. Using map as data structure for transfer data between client and server will make client less fragile more torerant to any change of DTO (e.g. adding new field ) on sever side.
In the following example, PersonDTO class to contains non primitive types
class Sex { private String code; private String name; private static Map map = new HashMap(); public static Sex MALE = new Sex("M","Male"); public static Sex FEMALE = new Sex("F","Female"); public static Sex UNKNOWN = new Sex("U","Unknown"); private Sex(String code, String name){ this.code = code; this.name = name; map.put(code,this); } public String getCode() { return code; } public String getName() { return name; } public static Sex valueOf(String code){ return map.get(code) != null ? (Sex)map.get(code) : UNKNOWN; } } class PersonDto { private Sex sex; private Calendar birthDate; private String name; public PersonDto(Sex sex, Calendar birthDate, String name) { this.sex = sex; this.birthDate = birthDate; this.name = name; } public Sex getSex() { return sex; } public Calendar getBirthDate() { return birthDate; } public String getName() { return name; } }We can refactor PersonDto to contains only primitive type
class PersonDto { private String sexCode; private long birthDateInMillis; private String name; public PersonDto(Sex sex, Calendar birthDate, String name) { this.sexCode = sex.getCode(); this.birthDateInMillis = birthDate.getTimeInMillis(); this.name = name; } public Sex getSex() { return Sex.valueOf(sexCode); } public Calendar getBirthDate() { Calendar result = new GregorianCalendar(); result.setTimeInMillis(birthDateInMillis); return result; } public String getName() { return name; } }
Saturday, December 23, 2006
Sample code of the book Agile Web Development with Rails Second Edition
Online code sample referenced in the book is not alway working, check with what is printed on the book
RubyOnRails print debug information
do'nt use
puts "Starting delete_user"instead use
logger.info "Starting delete_user"look at content of the file ./log/development.log
RubyGem view documentation
run
gem_servergoto http://local:8808 to browse content of gem
Labels:
ruby
RubyOnRails commands used in the book Agile Web Development with Rails
All commands
rails depot rake db:migrate ruby script/generate model Product ruby script/generate controller admin ruby script/generate migration add_price ruby script/generate scaffold product admin rake db:sessions:create rake db:sessions:clear, ruby script/console rake db:test:prepareTo enable development/test/production mode
ruby script/server -e development ruby script/server -e test ruby script/server -e production
NullObject with ObjectRelationMapping
Suppose that Foo and Bar are both class of entity object (an domain object with identity).
This is an example of how to use NullObject Pattern with Object Relation Mapping tool.
class Foo { public static NULL_FOO_ID = -1; public static Foo NULL = new NullFoo() ; public static Foo find(long id) { Foo result = DB.find(Foo.class, id); if( result == null ) result = NULL; return result; } private long id; public long getId() {return id} public boolean isNull(){ return false} ... // normal behavior } class NullFoo extends Foo { public boolean isNull(){ return true} public long getId() {return NULL_FOO_ID} ... // other special behavior } class Bar { private Foo foo; public Foo getFoo(){ return foo!=null ? foo : Foo.NULL; } public void setFoo(Foo foo){ if( foo.isNull() ) this.foo = null; else this.foo = foo; } }
Subscribe to:
Posts (Atom)