9 Replies Latest reply: Jan 9, 2007 6:58 PM by jschellSomeoneStoleMyAlias RSS

    DAO and DTO

    807607
      In the following piece of code:-
      package com.informit.myejb.dao;
      
      public interface SampleDAO
      {
       public SampleModel create( long id, String name, String data ) throws 
      SampleDAOException;
       public void delete( long id ) throws SampleDAOException;
       public void update( long id, SampleModel model ) throws 
      SampleDAOException;
       public SampleModel[] findByName( String name ) throws 
      SampleDAOException;
       public SampleModel findById( long id ) throws SampleDAOException;
      }
      we are creating a DTO named sampleModel. Now considering that I just need one value out of the whole table. Wouldn't creating the whole DTO object be a waste of time effort and memory, specially if each time we hae to access the DTO we have to create a new one. specially if the keys being passed to the method are used only once.

      I understand the concept of separation of concerns but I am concerned over here about the amount of effort required to create these DAOs and DTOs specially in cases where only one or two fields are required.

      To me it seems like an over kill.

      Any comments.
        • 1. Re: DAO and DTO
          807607
          To me it seems like an over kill.
          It appears to be an overkill but it will scale better to changes. Then, there is always a balance between the amount of separation you want to introduce with the development time available. Having a DTO and DAO, IMO, hardly takes any development time even for a small project.

          If you are certain that you always get one value out of the database, the DTO may be discarded for simplicity.

          That's just me and my funny ideas. :)
          • 2. Re: DAO and DTO
            807607
            Annie!
            • 3. Re: DAO and DTO
              807607
              yes?
              • 4. Re: DAO and DTO
                807607
                yes?
                Ummm. Well glad to see you have returned.
                • 5. Re: DAO and DTO
                  807607
                  Thanks.
                  • 6. Re: DAO and DTO
                    807607
                    well Martin Fowlwer does propose IdentityMap pattern in such a case where he says that thes objects should be stored in a Map with the primary key being the key for the map. So next time you need an object you check with the map if it exists or not and if it doesnot you try to grab it again.


                    But IMHO if the hit ratio(no. of times the same data is used again) is less these Maps might pose a nightmare in terms of memory and garbage collection.

                    So should one use IdentityMap or avoid it?

                    What do you think.
                    • 7. Re: DAO and DTO
                      807607
                      I can't address garbage collection, but it seems to me if you use
                      Weak/SoftReferences in your cache the memory issue should be
                      mitigated.
                      • 8. Re: DAO and DTO
                        807607
                        Why reinvent Hibernate?
                        • 9. Re: DAO and DTO
                          jschellSomeoneStoleMyAlias
                          >
                          I understand the concept of separation of concerns
                          but I am concerned over here about the amount of
                          effort required to create these DAOs and DTOs
                          specially in cases where only one or two fields are
                          required.
                          effort = manually typing it?

                          If yes then use code generation. Or a framework.

                          If instead you are concerned about volume (network, cpu, whatever) then the first step is to actually estimate volume so you can know which parts need extra effort.