• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Acessing a business service from a domain object

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a domain object called Ticket. Whenever the property 'soldDate' is set on 'Ticket', I have to send out a message to an external system by calling a service. This property ('SoldDate') is set through different paths by different services. I thought maybe its a good idea to trigger this message sending service from with in the 'setSoldDate()' of 'Ticket' so that I don't have to call the message sending service from each of the services which calls 'setSoldDate()'. But accessing a service from a domain object (entity) seems weird. Is there a pattern to do this right?

 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, welcome to JavaRanch - a friendly place for Java greenhorns!


As with many aspects of software design, there are very rarely a single "right" way to do anything. To calm your doubts a little, there
is nothing "weird" about accessing a service from a business/domain object. Actually, this is a typically normal communication pattern.

What you should avoid is coding or specifying details of the message sending service in the business/domain object. Read up on the Service Locator
design pattern for ideas on how to handle this.

Below is a rough draft of a setSoldDate() method to get you thinking:



Good luck!
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use AOP.
Or you can use Observer pattern, when a Ticket has changed it will send notification to the registered listeners.
Either way enables you to decouple business logic from model changes.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without knowing more about the problem, I would probably prefer the Observer solution.
 
Ben Narendren
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inorder to run the Observer Pattern, I need to run a listener in a separate thread. I really don't want this complication. Calling a service (injected or located) will contaminate the domain layer. AOP seems like the more viable option. Rather than put the AOP config in an xml file, I am planning to see if Spring supports any attribute oriented AOP. Thanks for all the time and effort you guys put to help me out. If you have any further inputs, please let me know.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uh, why does the Observer pattern require an additional thread???
 
Ben Narendren
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I misspoke with my partial understanding of Observer pattern. I was confusing it with the wait/notify stuff. Now that I have read upon it, I feel that its totally doable...just a matter of logistics if I want to use AOP or Observer pattern. Thanks for your help
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the update.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic