-
Notifications
You must be signed in to change notification settings - Fork 41
Relational examples
We will see now some very interesting examples of use, which makes the code more concise and elegant.
A classic example is a web application that consists of three layers: facade, business and data access. Each layer has two bean, one of input and one of output.
____________ ____________ ____________
PivotI | | BoI | | DtoI | |
------>| Facade |------>| Business |------>| Data |
| | | | | Access |
PivotO | Layer | BoO | Layer | DtoO | Layer |
<------| |<------| |<------| |
|__________| |__________| |__________|
In an architecture of this type, for each service you need to implement four mapping: PivotI -> BoI -> DtoI
and DtoO -> BoO -> PivotO
.
With JMapper you need to configure only the two business objects with the surrounding objects.
class PivotI{
String pivotIfield;
}
class BoI{
@JMap(attributes={"pivotIfield", "dtoIfield"}
classes ={ PivotI.class, DtoI.class})
Integer boIfield;
}
class DtoI{
Integer dtoIfield;
}
class PivotO{
String pivotOfield;
}
class BoO{
@JMap(attributes={"pivotOfield", "dtoOfield"}
classes ={ PivotO.class, DtoO.class})
Integer boOfield;
}
class DtoO{
Integer dtoOfield;
}
below the sample code:
class ExampleFacade{
RelationalJMapper<BoI> rmI;
RelationalJMapper<BoO> rmO;
ExampleBusiness exampleBusiness;
public PivotO executes(PivotI pivotI){
// mapping
BoI boI = rmI.manyToOne(pivotI);
// logic
BoO boO = exampleBusiness.executes(boI);
// mapping
PivotO pivotO = rmO.oneToMany(PivotO.class, boO);
return pivotO;
}
}
class ExampleBusiness{
RelationalJMapper<BoI> rmI;
RelationalJMapper<BoO> rmO;
ExampleDAO exampleDao;
public BoO executes(BoI boI){
// mapping
DtoI dtoI = rmI.oneToMany(DtoI.class, boI);
// logic
DtoO dtoO = exampleDao.executes(dtoI);
// mapping
BoO boO = rmO.manyToOne(dtoO);
return boO;
}
}
class ExampleDao{
public DtoO executes(DtoI dtoI){
// some operations
return dtoO;
}
}
Two Configurations and two instances of RelationalJMapper is all you need.
You may have the need to apply a common logic in most parts of the web application, using different objects as input.
____________ ___________ ___________
| | | | | |
| Service | | Service2 | | Service3 |
|____________| |___________| |___________|
\ | /
ServiceI\ Service2I / Service3I
\ | /
V________V_______V
| |
| CommonLogic |
|________________|
Standard solutions can be:
- use
instanceof
to identify class type - pass, as input to the method, the values to handle
Any solution you can design there will be always the need to change the code for every change request. With JMapper becomes:
Input objects:
class Service1I{
List<String> customersIds;
// getter and setter...
}
class Service2I{
Set<Integer> companyIds;
// getter and setter...
}
class Service3I{
TreeSet<String> supplierIds;
// getter and setter...
}
As you can see the fields have different structures, JMapper handles all implicitly.
class CommonLogicI{
List<Integer> ids;
// getter and setter...
}
Xml configuration:
<jmapper>
<class name="com.myapplication.CommonLogicI">
<attribute name="ids">
<!-- regex example -->
<value name=".*ids$"/>
<classes>
<class name="com.myapplication.Service1I" />
<class name="com.myapplication.Service2I" />
<class name="com.myapplication.Service3I" />
</classes>
</attribute>
</class>
</jmapper>
service implementations:
class Service1{
CommonLogic cl;
public void executes(Service1I service1I){
cl.idControl(service1I);
}
}
class Service2{
CommonLogic cl;
public void executes(Service2I service2I){
cl.idControl(service2I);
}
}
class Service3{
CommonLogic cl;
public void executes(Service3I service3I){
cl.idControl(service3I);
}
}
CommonLogic implementation:
class CommonLogic{
RelationalJMapper<CommonLogicI> rm;
public void idControl(Object source){
// mapping
CommonLogicI clI=rm.manyToOne(source);
// logic
someOperations(clI);
}
}
The conversions between different structures are performed implicitly, including the conversion of their elements.
© 2016 Alessandro Vurro
- Home
- How to map
- Relations
- Conversions
- creation/enrichment
- XML
- Annotation
- API
- Configurations
- Utilities
- Examples
- Articles
- More information
- Performance tests
- Release Notes