-
Notifications
You must be signed in to change notification settings - Fork 0
Transformation Interfaces
This is a libary for transforming an object which is an instance of EntityType A into an object of EntityType b. Let us start with Conversions. The UniConverter converts an Object of class A into an object of class B.
interface UniConverter<A,B> {
B convert(A a,TransformationContext ctx);
Type<A> getTypeA();
Type<B> getTypeB();
}
The UniTransformation can also merge object a into object b. The result can be a different instance of class B. The reason is that merging the information of b into a can change the type of b. Imagine B is a payment type, whose subclasses are Creditcard and DirectDebit. A represents the data that the user can edit and B is the data mapped to the database. When the user changes his payment data and type, then merging this information back must change the type of B.
interface UniTransformation<A,B> extends Converter<A,B> {
B merge(A a,B b,TransformationContext ctx);
}
UniConverter and UniTrasformation are separate interfaces because Conversion can be done on primitives but Transfomation cannot. The TransformationContext holds custom information such as a locale as input to the transformation. More importantly it helps in managing cyclic object graphs by holding all transformed objects.
##Bidirectional Transformations
So far we have looked at the interfaces for unidirectional transformations. Bidirectional transformations represented by Converter and Transformation:
interface Converter<A,B> {
UniConverter<A,B> getAB(),
UniConverter<B,A> getBA(),
Type<A> getTypeA();
Type<B> getTypeB();
}
interface Transformation<A,B> extends Converter<A,B> {
UniTransformation<A,B> getAB(),
UniTransformation<B,A> getBA(),
}
To easily define a converter implement the iterface JavaConverter or JavaUniConverter.
interface JavaConverter<A,B> {
B convertAB(A a);
A convertBA(B b);
}
To get a converter from this use the ConverterUtil, which will lookup the Atem types by inspecting the parameter types of the JavaConverter interface:
public Converter(JavaConverter c) {
return converterUtils.create(c);
}
##TransformationContext
The TransformationContext may carry informationlike locale version of the transformation or other general information that the transformationdependes on. Also the transformationContext is currently responsible for findig Types for objects. This responsibility will be removed in the future. You can use the implementation SimpleTransformationContext.
UniTransformation t;
t.convert(a,b,new SimpleTransformationContext(entityTypeRespoitory));