Skip to content

Yet another object mapper framework. Explicit configuration for speed.

Notifications You must be signed in to change notification settings

martin-jamszolik/el-entity-mapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Entity Mapper

Another solution to a complex object mapping. This project uses an Expression Language Library to help with mapping.

Entity mapper strengths:

  1. Map deeply structured objects.
  2. Apply mapping from multiple definitions to map complex object.
  3. Load mapping configuration based on context sensitive criteria.
  4. Apply converters from source to target object.
  5. Instantiate a target complex property when missing.
  6. Works on domain objects externally. No need to add any annotations.
  7. Pre-configured for Spring Framework or Guice IOC integration.

Entity mapper weaknesses:

  1. All properties are mapped explicitly, as per configuration only.
  2. Mapping configuration is one way only. If you need to reverse the mapping, you will need to provide another configuration.

Dynamic Object Discovery

This repo includes another useful library called object-criteria-factory. A runtime object locator or discovery based on dynamic or lazy binding criteria.

Imagine asking for a reference of a service under exotic criteria conditions, Here is a sample:

@Criteria({
    @Matcher(property = "payload",matcher = InstanceOfMatcher.class,classValue = Integer.class),
    @Matcher(property = "switch",matcher = ToStringValueMatcher.class,stringValue = "ON") 
})
public class MyServiceSwitchimpl implements MyService {

    public void execute() {
        System.out.println("You got me to do your bidding");
    }

}

How do you use it once objects are configured with criteria?

service = locator.getObject(new MapContext("payload",0,"switch","ON" ) );
assertEquals(MyServiceSwitchimpl.class, service.getClass());
service.execute();

Pretty cool right? Properties like payload or switch are evaluated on the visiting data object. It can by any object as long as it contains the expected properties. Perhaps, annotations are not enough, you want the criteria to change dynamically at the time of evaluation:

  • Per request or per thread context?
  • Per database query?
  • Per External Web service availability? Implement getCriteria() as you see fit.
    public List<PropertyCriteria> getCriteria() {
        return new CriteriaBuilder()
                .build(new PropertyBuilder()
                        .build("payload", new InstanceOfMatcher(Integer.class))
                        .build("switch", new PropertyValueEqualsMatcher("ON")))
                .getCriteria();
    }

We are using this library for the entity mapper configurations. It' gets interesting with more complex scenarios.

Desktop GUI for Quick Demo

Standalone swing gui example project is available for an interactive demo. Just run entity-mapper-demo main class.
All of our projects are maven based and tested to work out of the box when building from the parent pom.


Let's see some code:

Code LinkDemonstrates
PowerdBySpringMappingTest.java Using Spring CDI for auto registering mapping configurations
PoweredByGuiceMappingTest.javaUsing Guice CDI for auto registering mapping configurations
SimpleMappingTest.javaUsing no CDI environment
beanA-to-beanB-mapping1.txtExample of simple custom text file
beanA-to-beanB.xmlExample of basic xml file for mapping configuration

Common use cases

  1. Migrating between legacy to new application models. Not all features have been re-implemented, forcing you to use legacy services, remotely communicating between the two applications. Passing around information that has to be mapped.
  2. You have a JPA persistence layer with JPA entity annotations. However you don't want to expose JPA at your business and UI layer. Map JPA entities against more suited business object models.
  3. You have web services, tailored for specific client requirements. On a Web service side, you have JAXB annotated entities that as decorators for business entities. Using this mapper you can synchronize JAXB input against business data model for you business service to process the request. Results would be mapped back to JAXB entities and returned to the caller.

Would Scala implementation be more elegant?

Have been working on a scala implementation of this feature as well scala-entity-mapper.

About

Yet another object mapper framework. Explicit configuration for speed.

Topics

Resources

Stars

Watchers

Forks

Languages