Dynject Assignment

back to index

One of the main goals of Dynject is getting a set of external values stored into your POJO, while performing on the fly any necessary type conversion and validation. This "set of external values" may be values coming from Swing components, HTTP request parameters, strings scraped from a web page, whatever. In the Dynject framework, these are called Value Sources and the framework comes with a complete set of common value sources.

Your POJO, on the other hand, is annotated using a complete set of validating and data transformation annotations. For example, you could have the following field:

public class Customer {
   @Normalized(
     whitespace=WhitespaceProcessing.COLLAPSE,
     trim=TrimType.BOTH,
     transform=TransformType.UPPERCASE
   )
   @Length(
     min=3,
     max=20,
     message="Customer length must be between 3 and 20    characters")
   public String name;
...
}

Here we are saying that:

Dynject does not impose any restrictions about where you place the annotations. You can put them:

Annotations can be placed on the POJO fields, or on its getters / setters.

Using dynject, you can perform either complete assignments (object to object) or per-field assingments. Both are equally easy:

// Sample per-field assignment
Customer customer = new Customer();
ErrorMap errors = Shell.assign(customer, "name","John Doe");

Full object assignments (all the fields) are equally easy. For example, this is what you'd do if you wanted to move all parameters coming from an HTTP request into your model, while at the same time performing validations and normalizations:

// Sample full-object assignment
Customer customer = new Customer();
ErrorMap errors = Shell.assign(customer, new HttpRequestSource(request));

The assign method in both cases performs all validations. Failed validations result in error messages, while data that passes all validations is stored into the appropriate object field. This is important : your model never gets updated unless the data passes all the specified validations.