2021-04-27

How to apply DynamoDB Bean annotations to a different project's POJOs by using generics?

I'm trying to setup a micro-framework within my application to remove duplication of code ("DRY" principle). Specifically I have a need to use annotations from my project in conjunction with several POJO classes that are in a different project (those are the <T>'s below) and which do not share my dependencies.

So I started with this:

public abstract class DynamoRepo<T> {

    @DynamoDbBean
    public static class Entity<T> {
        private Number id;
        private T bean;
    
        @DynamoDbPartitionKey
        @DynamoDbConvertedBy(IdConverter.class)
        public Number getId() {
            return id;
        }
    
        public void setId(Number id) {
            this.id = id;
        }
    
        @DynamoDbFlatten
        public T getBean() {
            return bean;
        }
    
        public void setBean(T bean) {
            this.bean = bean;
        }
        
        ...

... the annotations are required at runtime for the code below

    private DynamoDbTable<Entity<?>> table;

... with a bit of setup in the constructor ...

    table = enhancedClient.table(tableName, TableSchema.fromBean(new Entity<? extends T>() {}.getClass()));

... I'd like to be able to accept a <T> which doesn't require the caller to know about Entity:

public void putItem(T item) {
    table.putItem(new Entity<T>(item));
}

Alas, this code doesn't compile. There are several problems here and any small tweaks lead to a different compile error.

How can I possibly make this work without having to add @DynamoDb annotations to the underlying POJOs (which are not in my project and don't have DynamoDB as a dependency) or have to manually create subclasses that extend each of the POJOs (and throw DRY out the window by creating a whole parallel object hierarchy)?

This question is about code clarity. Please don't worry about performance overhead.



from Recent Questions - Stack Overflow https://ift.tt/2S8mUdl
https://ift.tt/eA8V8J

No comments:

Post a Comment