Dynamic Value Object Creation

Discussions

J2EE patterns: Dynamic Value Object Creation

  1. Dynamic Value Object Creation (3 messages)

    The entity bean business interface define more than 150 getter/setter -Methods:

    public interface Article {
        public String getArtNo();

        public String getDescription();
        public void setDescription(String desciption);
     
       ....
    }


    The entity bean's local interface extends the business interface:

    public interface ArticleLocal extends Article, EJBLocalObject {
    }


    The entity bean also includes the business interface:

    public abstract ArticleLocal implements Article, EntityBean {

    ...
    }


    The client uses the session facade to get the value object and to update the entity bean:

        public Article getArticle() {
            try {
                return new ArticleValues(getArticleLocal);
            } catch(Exception e) {
                throw new EJBException("Unable to create Value Object. Cause: " + e.getMessage());
            }
        }



    The ArticleValue object implements Article. The Constructor uses the Article interface to initialize its fields:

    public class ArticleValues implements Article {

        ....

        public ArticleValues(Article article) throws Exception {
            Class c = this.getClass();
            String methodName = null;
    Object[] parameter = new Object[1];
    Class[] returnType = new Class[1];

    Method[] methods = Artikel.class.getMethods();
    for (int i = 0; i < methods.length; i++) {
    if (methods[i].getName().startsWith("get")) {
    methodName = "set" + methods[i].getName().substring(3);
    returnType[0] = methods[i].getReturnType();
    Method localMethod = c.getMethod(methodName, returnType);
    parameter[0] = methods[i].invoke(artikel, new Object[] {});
    localMethod.invoke(this, parameter);
    }
    }

        }


        public String getArtNo() {
            return this.artNo;
        }
        public void setArtNo(String artNo) {
            this.artNo = artNo;
        }

        ...
    }

    Threaded Messages (3)

  2. Dynamic Value Object Creation[ Go to top ]

    I'm not sure if this is as much a design pattern as it is a useful tool.
    In my expirience data model interfaces tend to have references to other data model interfaces. So the copying process might not be as simple and clear-cut as copying each property as is: you may need to convert other data models to value objects in the process as well.

    I don't see how you save much if you have to declare all the getter/setters and fields yourself anyway. You might as well write the constructor by hand too and get faster results.

    A note about the implementation:
    - you will find the "getClass" method and try to invoke a "setClass" method. This will cause an exception.
    - This implementation is wasteful in terms of memory because it creates three garbage arrays on each invocation. If you do a lot of reflection of this sort in your project, I think you should consider using ThreadLocal variables for the paramter and return type array, an a static final field for the empty array.

    Gal
  3. Dynamic Value Object Creation[ Go to top ]

    thx for comment.

    your're right. it's more a tool than a design pattern.
  4. Dynamic Value Object Creation[ Go to top ]

    I love to use simply jacarta: commons-beanutils

    BeanUtils.copyProperties(java.lang.Object dest, java.lang.Object orig)