【问题标题】:GWT Autobean set initial value of created interfaceGWT Autobean 设置创建接口的初始值
【发布时间】:2012-10-22 12:30:40
【问题描述】:

我们正在使用 AutoBeans 创建 Pojo 对象以用于 RPC-Calls。 Pojo 具有默认值或其他类初始化的推荐方法是什么?

例如

 public interface SamplePojo {
        // should default to 5
        int getSampleProperty();
        void setSampleProperty(int sampleProperty);
    }


    public interface ModelFactory extends AutoBeanFactory {
        AutoBean<SamplePojo> getSamplePojo();   
    }

SamplePojo 有一个我们总是希望默认为 5 的 int 属性。

【问题讨论】:

    标签: gwt autobean


    【解决方案1】:

    AutoBeans 应该被视为低级的,直接映射到 JSON 或从 JSON 映射。考虑到这一点,您不希望getSampleProperty() 成为 5,而是希望检测该属性是否缺少特定值并在这种情况下使用 5 .

    因此,如果0int 的默认值)不是该属性可接受的值,则只需“如果该属性为 0,则使用 5”。否则,将返回类型更改为Integer 并“如果属性为null,则使用5”。

    【讨论】:

    • 这并没有解决我们希望初始化逻辑集中的问题。在我们的例子中,我们不希望使用 SamplePojo 的每个地方都必须重复“如果属性为空,则使用 5”。
    • 然而,AutoBeans 只不过是经过类型检查的Map;因此,要么在每次创建这样的 AutoBean 时显式放置 5(即使用隐藏 AutoBeanFactory 并添加初始化逻辑的工厂方法),然后您将通过网络显式发送 5;或以某种方式隐藏 AutoBean 和/或其 getSampleProperty(我说 AutoBeans 是低级 API 吗?)以实现“如果属性为空则使用 5”行为(并且优化了线路有效负载,没有 sampleProperty除非已明确设置)。如果你知道 protobuf,那么 AutoBean 大量借鉴了它们:低级!
    【解决方案2】:

    这行得通吗?

    public interface SamplePojo {
            // should default to 5
            int getSampleProperty();
            void setSampleProperty(int sampleProperty);
        }
    
    public class SamplePojoImpl implements SamplePojo{
        private int sampleProperty = 5
        // getters/setters
        int getSampleProperty(){ return sampleProperty;}
        void setSampleProperty(int sampleProperty){this.sampleProperty = sampleProperty;}
    
    }
    
    public interface ModelFactory extends AutoBeanFactory {
        AutoBean<SamplePojo> getSamplePojo(SamplePojoImpl samplePojo );   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      相关资源
      最近更新 更多