Public variables in remote interface?

Discussions

EJB design: Public variables in remote interface?

  1. Public variables in remote interface? (7 messages)

    Is it possible for client to use bean's public variables thru its remote interface?

    Threaded Messages (7)

  2. It is usually not adviseable to allow any other clients to access the class's internal variables. This breaks the encapsulation principle of the OOP and leads to unpredictable results. A much more advisable way to do this is to implement setter and getter methods that encapsulate access to the internal variables. Thus, a short answer to your question is that you should implement setter and getter methods in the remote interface that encapsulate access to the internal variables.

    Leo
  3. So, if I have a bean with 40 "public" variables, the client has to call 40 separate methods to get all data needed?
  4. Accessing Bean data using Remote Interface methods is very inefficient since ejbLoad and ejbStore are constantly called. Below is a way you can avoid this.

    Create a separate serializable class -- we'll call it Accessor -- that represents the data encapsulated by the Bean and derive your Entity Bean from it. This way you kill two birds with one stone: you created a class that can actually be passed over the network and you've implemented all the business logic/storage methods of the Remote Interface -- all you have to do is expose them. Create a method in your Bean called getAllDetails and simply return the Accessor that contains all the data (it my experience, however, it is better to create a deep copy of the object rather than simply returning it). Call this method to get the Bean data in the form of the Accessor class, manipulate it and return it back using setAllDetails methods.

    Hope this helps.
    Leo
  5. No. For this scenario, you should use patterns like Bulk Accessors, and Dependent Objects. That is, group together related attirbutes and package them as an object. Your getter/setter methods should return or take as parameter these Bulk Accessors or Dependent objects.

    For example, if you have the following bean:

    public class MyBean implements SessionBean {
        public String customerName;
        public String street;
        public String city;
        public String phone;
        public String email;
        
        .
        .
        .
    }

    You should make a class like this

    public class CustomerInfo {
        public String name;
        public String street;
        public String city;
        public String phone;
        public String email;
    }

    Your bean will then have accessor methods like this:

    public class MyBean implements SessionBean {
        public String customerName;
        public String street;
        public String city;
        public String phone;
        public String email;
        
        public CustomerInfo getCustomerInfo() {
            
            // instantiate CustomerInfo, set values and return
            
        }

        public void setCustomerInfo(CustomerInfo custInfo) {

            // set the public attributes of the bean

        }

        .
        .
        .

    }

    This way you don't have to call getter/setter for every attribute.

    I hope that will helps.

    rgrds,

    Yaser.
  6. Public variables in remote interface?[ Go to top ]

    Yaser,

    Your CustomerInfo class should be serializable otherwise you can't transfer the object to client.


    Gamini
  7. Public variables in remote interface?[ Go to top ]

    Even better then that, make the customer class like this

    public final class Customer implements org.omg.CORBA.portable.IDLEntity

    In this way

    1) org.omg.CORBA.portable.IDLEntity itself extends java.io.Serializeable so the class will be serializabloe
    2) By doing this it will be recognized by RMI/IIOP marshallers as an IDL marshallable type and will be marshalled by a compliant container in a MUCH faster mechanism them Java serialization which is very slow.

    Dave Wolf
    Internet Applications Division
    Sybase
  8. Only if the bean and the remote have accessor methods. You cannot use direct "dot notation" as such.

    Dave Wolf
    Internet Applications Division
    Sybase