Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 45

Thread: Use Hibernate Entities in the web layer?

  1. #11
    Join Date
    Jun 2007
    Posts
    19

    Default

    Debasish, I have plenty of respects for you. However, I have to disagree on this.

    One thing that I have kept in mind is that Spring MVC and Hibernate's development started in the dark age of EJB 2.0, which forced developers to build enterprise applications with anaemic domain models using transaction scripts. At that time, many people equalled Java Beans to POJOs and domain classes. Unfortunately that was misleading... In a rich domain model, typically entity classes are NOT Java beans, because providing setter methods can easily break invariants of a domain class. Since then, Domain Driven Design has gained wider adoption and recognition.

    In a recent presentation "Are We There Yet?", Rod Johnson, the founder of Spring, asked the attendees a question: when working with Hibernate, who use property access and who use field access? Those who use property access are persisting DTOs, or, using an anaemic domain model. I cannot recall the exact words, but that's basically what he said and meant.

    I would like to add: that those who use domain classes as command objects in the web layer are working with an anaemic domain model.

    For example, in one of the projects I have worked on, there is a Fraud class, which has three fields: fraudPaymentDate, fraudPaymentAmount, amountRecovered and dateRecovered. The invariants regarding these three fields are:
    1) amountRecovered must not exceed fraudPaymentAmount.
    2) When amountRecovered is $0.00, dateRecovered must be null.
    3) When amount Recovered is greater than $0.00, dateRecovered must be present, in the past and must not predate fraudPaymentDate.

    If this class provides setter methods for all these fields and is directly used as a command object, there is no way not to break invariants in the binding phase of Spring MVC:
    If setAmountRecovered() is invoked with a non-zero amount before setDateRecovered(), then invariant no. 3 is broken.
    If setDateRecovered() is invoked before setAmountRecovered(), then invariant no. 2 is broken.

    Therefore, a command object typically does not maintain data integrity. Usually a validate() method is provided to the client to ensure data integrity AFTER the binding.

    There is one big drawback with using validate() method: the domain object relies on its client to call its validate() method to check data integrity or on a trigger from an event such as save(). However, when using Hibernate, domain objects can be persisted transparently without the domain object being persisted beware of the fact that it's being persisted. Thus, a corrupted domain object can easily be persisted to database.

    When using a rich domain model with DTO, this problem is solved:
    The DTO can be used as a command object in the web layer and passed to the service tier. The UpdateService can construct two value objects fraudPaymentInfo, which contains both fraudPaymentDate and fraudPaymentAmount, and recoveryInfo, which contains both amountRecovered and dateRecovered, according to the data in the DTO and invoke the Fraud object's updatePaymentInfo() and updateRecoveryInfo(). Invariants are always maintained in the process.

    http://ozgwei.blogspot.com/2007/06/i...tities-in.html

  2. #12
    Join Date
    Jul 2006
    Location
    Kolkata, India
    Posts
    217

    Default

    Quote Originally Posted by ozgwei View Post
    If this class provides setter methods for all these fields and is directly used as a command object, there is no way not to break invariants in the binding phase of Spring MVC:
    If setAmountRecovered() is invoked with a non-zero amount before setDateRecovered(), then invariant no. 3 is broken.
    If setDateRecovered() is invoked before setAmountRecovered(), then invariant no. 2 is broken.

    Therefore, a command object typically does not maintain data integrity. Usually a validate() method is provided to the client to ensure data integrity AFTER the binding.
    Let me try to address one of your concerns : setter methods in domain objects break invariants, if used from the presentation layer.

    Typically for a domain object, there is a top level contract (Java interface or abstract class) and there is an implementation. The setter methods are part of the implementation only and are NOT present in the contract. Expose the domain contract to the presentation layer in general - by this the presentation layer will have access to the *domain* methods only. Using the domain methods no one will be able to violate any contract. Expose the implementation class *only* to the MVC component. Use aspects to prevent usage of the implementation class by anyone other than the MVC framework. This way, you basically give the domain object a two-faced role - the implementation setters are only for the MVC framework.

    Maybe I should blog more about this :-)

    Cheers.
    - Debasish

  3. #13
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    This has been a long documented debate. The presentation of ROO from Ben Alex at SpringONE showed a DTO approach, but it seemed really nice! The conversion was all done for you so there was zero effort. There were also some compelling reasons for going this way.
    Last edited by karldmoore; Aug 29th, 2007 at 11:10 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  4. #14
    Join Date
    Jul 2006
    Location
    Kolkata, India
    Posts
    217

    Default

    Quote Originally Posted by karldmoore View Post
    This has been a long documented debate. The presentation of ROO from Ben Alex at SpringONE showed a DTO approach, but it seemed really nice! The conversion was all done for you so there was zero effort. There were also some compelling reasons for going this way.
    Karl -

    Is there any pointer / documentation available on ROO ? I would like to see more of this. As a first impression, I think, irrespective of whether DTOs are being generated or hand-written, we have more classes. And most importantly dumb data holders as DTOs. But I really should not be commenting without having a look at ROO.

    Cheers.
    - Debasish

  5. #15
    Join Date
    Jul 2006
    Location
    Rome, Italy
    Posts
    347

    Default

    Quote Originally Posted by ozgwei View Post
    Debasish, I have plenty of respects for you. However, I have to disagree on this.
    Hi Alex,

    I agree with Debasish.
    As already commented in your blog, I also use setters/getters only in implementation classes (so the code using interfaces will never see them), and when needed I use specialized view interfaces introduced into domain objects via AOP by using Spring Modules XT Introductors.

    Why do you think that such an approach still force to use an anemic model?

    Cheers,

    Sergio B.
    Sergio Bossa
    Spring Modules Team

  6. #16
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    Quote Originally Posted by debasishg View Post
    Is there any pointer / documentation available on ROO ? I would like to see more of this. As a first impression, I think, irrespective of whether DTOs are being generated or hand-written, we have more classes. And most importantly dumb data holders as DTOs. But I really should not be commenting without having a look at ROO.
    I'm not sure there's anything around at the minute, it could take a while. The BeJUG lead did say that they wanted to get the presentations online over the coming weeks and months. I guess we'll just have to keep an eye out for more information.
    http://www.bejug.org/confluenceBeJUG...y/PARLEYS/Home
    Last edited by karldmoore; Aug 29th, 2007 at 11:10 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  7. #17
    Join Date
    Jun 2007
    Posts
    19

    Default

    Quote Originally Posted by debasishg View Post
    Karl -

    Is there any pointer / documentation available on ROO ? I would like to see more of this. As a first impression, I think, irrespective of whether DTOs are being generated or hand-written, we have more classes. And most importantly dumb data holders as DTOs. But I really should not be commenting without having a look at ROO.

    Cheers.
    - Debasish
    I went to Ben Alex's ROO introduction presentation in Sydney a few months ago and was blown away by this new framework.

    The framework does not only generate the DTOs but also modify them when the domain model is changed.

    Dozer http://dozer.sourceforge.net is used in ROO to move data between DTOs and domain objects.

    ROO has been used in Woolworths Australia with great success.

    I am looking forward to the public release of ROO.

  8. #18
    Join Date
    Jun 2007
    Posts
    19

    Default Matt Raible's blog entry on Real Object Oriented (ROO)

    Quote Originally Posted by debasishg View Post
    Karl -

    Is there any pointer / documentation available on ROO ? I would like to see more of this. As a first impression, I think, irrespective of whether DTOs are being generated or hand-written, we have more classes. And most importantly dumb data holders as DTOs. But I really should not be commenting without having a look at ROO.

    Cheers.
    - Debasish
    Hi Debasish, I have found this blog entry by Matt Raible about ROO
    [TSE] Hop into Real Object Oriented (ROO) with Ben Alex

    Noticeably, ROO is compared with traditional approach and in the "form backing objects" category, ROO uses DTO while traditional way uses DO (domain object)...

    Cheers,
    alex

  9. #19
    Join Date
    Jun 2007
    Posts
    19

    Smile

    I just noticed that both Debasish & Sergio had commented on Matt's blog on ROO. Nevertheless, I hope the above link is useful for other users to get to know ROO.

  10. #20
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    Has anyone else used any neat frameworks like this that I could take a look at? We are looking to build an internal system at the minute and I'd really like to find a better starting point. Looked at Appfuse already, anymore?
    Last edited by karldmoore; Aug 29th, 2007 at 11:09 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •