| 
      
      
      From: Gavin_King/Cirrus%<CI...@ci...> - 2002-02-04 06:41:14
      
     | 
| >In response to the "Lazy Initialization of Objects" request, some comments: >Not trivial. Approaches that I know of: >- pre/post processing to insert code that manages this >- use of interfaces and proxies >- provide an API and let the coder do it. Unfortunately, there is no way to do this in Java that is extremely compatible with the philosophy of Hibernate (ie. use dynamic techniques as much as possible but minimize model intrusion) There are straightformard approaches in more dynamic languages like SmallTalk, Python et al. If Java would have let you create a java.lang.Proxy from a class, instead of only from interfaces, it would be much easier. My preferred technique is this: 1. Let the developer build the system without lazy instantiation 2. At deployment time have a tool to generate proxy classes that inherit the business classes and override every public, package and protected method with a method that calls the initialization code when needed (and then invokes super). This implies source generation which is quite easy to do using reflection. 3. At runtime, the persistence layer can check for the existence of a proxy class (using a naming convention), if it exists, return that to the application, otherwise return an initialized instance. This has following advantages: 1. The only model intrusion is you cant use final classes or methods if you want lazy initialization. That fine; you can still have one or the other on a class by class basis. 2. No code-generation required during the test/debug cycle. 3. You can choose which classes are initialized lazily at deployment time, simply by choosing which proxies to deploy. 4. No mutilation of sourcecode - so line-numbers, etc stay intact in stacktraces. 5. No extra proxy objects floating around wasting memory. 6. No use of reflection _inside_ the application code. (A reflective solution here could actually be a performance hurdle if proxies get invoked inside a loop.) Does anyone see any major disadvantages of this solution? |