1 Reply Latest reply on Nov 2, 2010 12:33 AM by Jean Demonceau

    Flex - Java Data transfer objects. How do you usually work ? (Brainstorming)

    Jean Demonceau Level 1

      Hi,

      I would like to explain you how I work and to know how you guys do.

      All this discussion is just an example of how I work. I really want to know how other coders usually work. This way, maybe I can improve my code.

       

      I would like to know how do you do when you want to retreive data into typed objects when these data contains reference to other typed objects.

      Here is how I always work :

       

      I want to retreive all my customers into a flex datagrid and to display all information.

      Here is my Customer table structure (just an example)

      id, name, refCompany, refLanguage, refCountry, refCustomerGroup

       

      refCompany, refLanguage, refCountry, refCustomerGroup are IDs that reference to other tables.

       

       

      To ensure to retreive all data into typed object, I make a Customer,Company, Language, Country, CustomerGroup, Flex and Java classes.

      I make a query that looks like : Select * from Customer

       

      My java code will looks like :

       

      List<Customer> customers = new ArrayList<Customer>;

      ResultSet rs = myConn.execSelect(query);

      while(rs.next)

      {

           customers.add(new Customer(rs))

       

      }

      return customers;

       

      In my Customer class, I have a constuctor that takes a resultSet

      My customer class contains variables (refCompany that is actually a Company object (not an id), refLanguage that is actually a Language object (not an id),... )

      Customer(ResultSet rs)

      {

           this.name = rs.getString("name")

           this.refCompany = new Company.findById(rs.getInt("refCompany"))     //This function returns a Company object

           this.refLanguage = new Language.findById(rs.getInt("refLanguage"))     // This function returns a Language object

           ...

      }

       

      Here is how I always do. I don't know if it is a good way because for each customer found in the database, I will make 4 queries (4 findById to find each object : company, language, country, customerGroup of the current customer).

      But this way is generic, I don't have to make a specific query that will retreive all objects in one time. I select all customers, and in the constructor I select all objects that I have the reference...

       

      The big problem with this way is when I retreive objects, I always retreive all referenced objects and I do not necessarily want it.

       

      What do you think about the way I work ? How you guys do ? what is the best practice ?