This discussion is archived
3 Replies Latest reply: Oct 11, 2012 12:54 AM by 000201 RSS

DynamicBean

000201 Newbie
Currently Being Moderated
Guys,

Any ideas and examples about DynamicBean?
Whys should we use this?

Much thanks in advance.
  • 1. Re: DynamicBean
    Nitin Khare Expert
    Currently Being Moderated
    ATG DynamicBeans extend standard Java beans functionality. In DynamicBean, a Java object can expose properties which are determined at run-time. So if an object X is a Dynamic Bean then you can access a property like X.myProperty even if there is no getMyProperty() present in the class of object X. There are certain classes and interfaces which ATG automatically registers as DynamicBeans including: java.lang.Object, java.util.Map, atg.repository.RepositoryItem, atg.userprofiling.Profile etc. So that is why you can use some thing like *<dsp:valueof bean="Profile.firstName"/>* without any problem even if firstName is present as a property in the Profile object.

    Now you can register other objects as Dynamic Bean also. To make any object as a Dynamic Bean you need to register that class with an implementation of DynamicPropertyMapper interface by calling DynamicBeans.registerPropertyMapper() within a static block so that it can execute as soon as class is loaded. E.g.
    static {
        DynamicBeans.registerPropertyMapper(ProfileFormHashtable.class, new ProfileFormHashtablePropertyMapper());
    }
    This is the code snippet which is actually present in ProfileForm. It registers ProfileFormHashtable class as a Dynamic Bean with the ProfileFormHashtablePropertyMapper class instance to be used as DynamicPropertyMapper for doing get/set on properties that are actually not declared as bean properties of ProfileFormHashtable object.

    The "value" dictionary used in ProfileFormHandler (ProfileFormHandler extends ProfileForm) is actually nothing but instance of ProfileFormHashtable object and being a dynamic bean it allows you to use ProfileFormHandler.value.firstName where firstName is the name of dynamic property used as the key for ProfileFormHashtable. ProfileFormHashtablePropertyMapper implementation handles getPropertyValue() and setPropertyValue() calls to actually do the operations on underlying profile RepositoryItem using map's key as property name and its value as the property value.
  • 2. Re: DynamicBean
    karthik chopperla Pro
    Currently Being Moderated
    All repository items are dynamic beans because whenever you need a value say name, you do not call getName(), you rather call .getPropertyValue("name") and dynamic beans take care of fetching it. Advantage is you don't have to deal with POJO's( representing item) or update getters and setters everytime a new property is added in item-descriptor.

    Downside is, if you typecast it to wrong object, you might get run time exceptions.

    -karthik
  • 3. Re: DynamicBean
    000201 Newbie
    Currently Being Moderated
    Excellent. Thank you Nithin and Karthik.
    It's pretty clear to me.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points