1 / 18

Implementation of domain models using dynamic proxies Bjørn Vidar Bøe, JavaZone 2005

Implementation of domain models using dynamic proxies Bjørn Vidar Bøe, JavaZone 2005. Agenda. Rationale Overall design Handling business logic Conclusion. Rationale. The new mantra for many new frameworks is simplification (Hibernate, Spring, EJB3, Prevayler, Ruby on Rails…)

laddie
Download Presentation

Implementation of domain models using dynamic proxies Bjørn Vidar Bøe, JavaZone 2005

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Implementation of domain models using dynamic proxies Bjørn Vidar Bøe, JavaZone 2005

  2. Agenda • Rationale • Overall design • Handling business logic • Conclusion

  3. Rationale • The new mantra for many new frameworks is simplification(Hibernate, Spring, EJB3, Prevayler, Ruby on Rails…) • AOP getting popular for handling cross-cutting concerns like logging, security, caching… • How far can we take this?

  4. Simple Patient class and implementation Patient public class Patient { public String firstName; public String lastName; public Date dob; } +firstName : String +lastName : String +dob : Date

  5. Correct implementation of the Patient class public class Patient { private String firstName; private String lastName; public Date dob; public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } ... }

  6. Reverse engineered UML from the new class Patient -firstName : String -lastName : String -dob : Date +getFirstName() : String +setFirstName(in firstName : String) +getLastName() : String +setLastName(in lastName : String) +getDob() : Date +setDob(in dob : Date)

  7. Patient class from a user’s perspective public interface Patient { public String getFirstName(); public void setFirstName(String firstName); public String getLastName(); public void setLastName(String lastName); public Date getDob(); public void setDob(Date dob); }

  8. What’s interesting about this? • What is the minimum of information needed to define a Patient-class? • Should the implementation matter? • Should the user need to know about the implementation? • New opportunities?

  9. What if the interface was all that’s needed? • No implementation, just the interface to specify the model • Let a framework implement the model • Other frameworks do this in varying degree, but not in this way

  10. Dynamic proxies Patient (dynamic proxy) Invocation handler +getFirstName() : String +setFirstName(in firstName : String) +getLastName() : String +setLastName(in lastName : String) +getDob() : Date +setDob(in dob : Date) public Object invoke(Object proxy, Method m, Object[] args) Patient patient = (Patient) Proxy.newProxyInstance(Patient.class.getClassLoader(), new Class[] { Patient.class }, myInvocationHandlerImpl);

  11. What about business logic? public String sayHello() { return ”Hello! My name is ” + firstName; }

  12. Split data and business logic Data definition Business service definition BS Impl Domain object

  13. Patient with sayHello() PatientData PatientService +getFirstName() : String +setFirstName(in firstName : String) +getLastName() : String +setLastName(in lastName : String) +getDob() : Date +setDob(in dob : Date) +sayHello() : String PatientServiceImpl -patient : Patient + sayHello() : String Patient

  14. Implementation of PatientServiceImpl public class PatientImpl implements PatientService { private Patient patient; public PatientImpl(Patient patient) { this.patient = patient; } public String sayHello() { return ”Hello! My name is ” + patient.getFirstName(); } }

  15. Alternate solution to the business logic problem – CGLIB! • Similar to dynamic proxies but is also able to extend classes • Used in frameworks like Spring and Hibernate

  16. CGLIB code-example Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(Patient.class); enhancer.setCallback(myMethodInterceptor); Patient patient = (Patient)e.create(); public Object intercept(Object proxy, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { if (!Modifier.isAbstract(method.getModifiers())) { return methodProxy.invokeSuper(proxy, objects); } else { ... } }

  17. Alternative implementation public abstract class Patient { public abstract String getFirstName(); public abstract void setFirstName(String firstName); public abstract String getLastName(); public abstract void setLastName(String lastName); public abstract Date getDob(); public abstract void setDob(Date dob); public String sayHello() { return ”Hello! My name is ” + getFirstName(); } }

  18. Pros and cons • Easy to change the model • Code-wise extremely lightweight • Not ment to have a database as a back-end • Interface-based solution gets complicated when business logic is added • Haven’t we seen the abstract solution before? • Where will we see this?

More Related