Usage: First exampleThis is a featured page

In this brief tutorial I'll show you how to map a simple class to and from a Dto representation.
Before you start, make sure to create a Java project and add Bean2Bean as a dependency (with third party dependencies).

Suppose we have an application that stores information about food.
We want to make a listing of each entry, using DTOs.

Let's start by looking at the Domain class, and the Dto class representation:

  • Food.java
public class Food {

private Double servingSizeGrams;
private Integer calories;
private Integer totalFat;
private Integer saturatedFatGrams;
private Integer transFatGrams;
private Integer cholesterolMiligrams;
private Double sodiumMiligrams;
private Double carbohydrateGrams;
private Double dietaryFiber;
private Double sugarGrams;
private Double proteinGrams;

...
getters and setters
...

}

  • FoodDto.java
public class FoodDto {

private String serving;
private String calories;
private String totalFat;
private String saturatedFats;
private String transFats;
private String cholesterol;
private String sodiumMiligrams;
private String carbohydrateGrams;
private String dietaryFiber;
private String sugarGrams;
private String proteinGrams;

...
getters and setters
...


}
Note that some names are equal but others don't!
Which is a simplified view of the data in previous classes



CopyFrom: Food -> FoodDto


Now, let's see how to create a Dto from a Food object with Bean2Bean:
Food spicyCrispy = ...;
FoodDto nutritionFact = Bean2Bean.getInstance().createFrom(spicyCrispy, FoodDto.class);

Note1: This assumes you are not using Spring as a dependency injection mechanism and for the sake of simplicity. If you want to integrate Bean2Bean with Spring, check How to integrate Bean2Bean with Spring.

Well, if you tested this on Java, you can see that none of the Food properties have passed to the Dto, right?
What's missing? We have to tell Bean2Bean how to map those properties! The simplest conversion Bean2Bean does is creating a new instance of the expected class.

We should add annotations to the Dto, as this is just a view of the original data, and we don't want to modify the domain classes.
public class FoodDto {

@CopyFrom("servingSizeGrams")
private String serving;

@CopyFrom
private String calories;

@CopyFrom
private String totalFat;

@CopyFrom("saturatedFatGrams")
private String saturatedFats;

@CopyFrom("transFatGrams")
private String transFats;

@CopyFrom("cholesterolMiligrams")
private String cholesterol;

@CopyFrom
private String sodiumMiligrams;

@CopyFrom
private String carbohydrateGrams;

@CopyFrom
private String dietaryFiber;

@CopyFrom
private String sugarGrams;

@CopyFrom
private String proteinGrams;

}

Note that properties with different names need an explicit name expected on the other class

That's it! That's all you have to do!
One thing I have to tell you. We used the Bean2Bean method "createFrom()" this method tells Bean2Bean that the annotations are in the object to be created (the DTO).
If we used the "convertTo()" method we were telling Bean2Bean that the annotations were on the Food object, and that wasn't the case!


CopyTo: FoodDto -> Food

Okay, so we know how to create Dtos based on Food, but how to do it otherwise?
We need the other annotation, @CopyTo. We will use it on the Dto again, as we don't want to get our domain class dirty.
public class FoodDto {

@CopyFrom("servingSizeGrams")
@CopyTo("servingSizeGrams")
private String serving;

@CopyFrom
@CopyTo
private String calories;

@CopyFrom
@CopyTo
private String totalFat;

@CopyFrom("saturatedFatGrams")
@CopyTo("saturatedFatGrams")
private String saturatedFats;

@CopyFrom("transFatGrams")
@CopyTo("transFatGrams")
private String transFats;

@CopyFrom("cholesterolMiligrams")
@CopyTo("cholesterolMiligrams")
private String cholesterol;

@CopyFrom
@CopyTo
private String sodiumMiligrams;

@CopyFrom
@CopyTo
private String carbohydrateGrams;

@CopyFrom
@CopyTo
private String dietaryFiber;

@CopyFrom
@CopyTo
private String sugarGrams;

@CopyFrom
@CopyTo
private String proteinGrams;


Note: Since version 0.9 Bean2Bean has an annotation for bi-directional moving so you don't have to use two annotations per property (see below).

Let's try the conversion to see what happens:
Food converted = Bean2Bean.getInstance().convertTo(Food.class, nutritionFact)

Note: We used convertTo() this time! The annotations are on the source object, not on the created, remember?

Now, you should have two instances of Food that are equals!



CopyFromAndTo: FoodDto <-> Food
Last example was a little too much annotations right?
In cases like before where you have a bidirectional mappings with almost no configuration, the best thing you can do is to use CopyFromAndTo annotation.
This annotation reduces the verbosity needed to configure mappings and has almost all the possibilities from CopyTo and CopyFrom together. However you are
warned to use it wisely.
CopyTo and CopyFrom are better to express complex mappings, don't use CopyFromAndTo y the annotation body starts to grow a few lines. It's better to split the configuration in two and the code will be easier to read.

Let's see how dos our mapping looks like with CopyFromAndTo
public class FoodDto {

@CopyFromAndTo("servingSizeGrams")
private String serving;

@CopyFromAndTo
private String calories;

@CopyFromAndTo
private String totalFat;

@CopyFromAndTo("saturatedFatGrams")
private String saturatedFats;

@CopyFrom("transFatGrams")
private String transFats;

@CopyFromAndTo("cholesterolMiligrams")
private String cholesterol;

@CopyFromAndTo
private String sodiumMiligrams;

@CopyFromAndTo
private String carbohydrateGrams;

@CopyFromAndTo
private String dietaryFiber;

@CopyFromAndTo
private String sugarGrams;

@CopyFromAndTo
private String proteinGrams;

Note: This annotation was introduced on Bean2Bean 0.9. You won't find it on previous versions.

As you can see we reduced the annotations and there are no duplicated mappings. CopyFromAndTo annotation enables the bidirectional mapping of the properties.










kfgodel
kfgodel
Latest page update: made by kfgodel , Mar 15 2010, 5:25 PM EDT (about this update About This Update kfgodel Moved from: Documentation - kfgodel

No content added or deleted.

- complete history)
Keyword tags: None
More Info: links to this page
There are no threads for this page.  Be the first to start a new thread.