Parse parameters into domain objects

All HTTP request parameter names and values are entirely textual. The problem always arises in a web application of how to translate or parse such data into more meaningful objects in the problem domain.

Such translation varies with the tool you are using. Here is an example taken from an application built with the WEB4J tool.


/** Build a Model Object from request parameters. */
public final class RestoAction extends ActionTemplateListAndEdit {

  public static final RequestParameter RESTO_ID = RequestParameter.withLengthCheck("Id");
  public static final RequestParameter NAME = RequestParameter.withLengthCheck("Name");
  public static final RequestParameter LOCATION = RequestParameter.withLengthCheck("Location");
  public static final RequestParameter PRICE = RequestParameter.withLengthCheck("Price");
  public static final RequestParameter COMMENT = RequestParameter.withLengthCheck("Comment");

  protected void validateUserInput() {
    try {
      ModelFromRequest builder = new ModelFromRequest(getRequestParser());
      fResto = builder.build(Resto.class, RESTO_ID, NAME, LOCATION, PRICE, COMMENT);
    }
    catch (ModelCtorException ex){
      //displays error message to end user
      addError(ex);
    }    
  }
  
  //..many items elided

  //target Model Object, built from user input
  private Resto fResto;
} 

In this case, the parsing of raw HTTP request parameters is shared between these classes :

Validation and parsing of HTTP request parameters are related topics. See the Repel invalid requests topic for further information.

See Also :
Repel invalid requests
Command objects
Model Objects
A Web App Framework - WEB4J
Would you use this technique?
Yes   No   Undecided   
© 2010 Hirondelle Systems | Source Code | Contact | License | Quotes | RSS
Individual code snippets can be used under this BSD license - Last updated on June 5, 2010.
Over 150,000 unique IPs last month - Built with WEB4J.
- In Memoriam : Bill Dirani -