Search code examples
oopdesign-patternsclasslanguage-agnosticclass-design

Confusion using object oriented design .. need help


Let say we have an Order class and OrderManagerService class.

Order class: [some state and methods that act on the state]

  1. Item[]
  2. status

OrderManagerService class: [have no state. only following static methods]

  1. createOrder
  2. getOrder

Question: Let say we are using a relational DB in the back. Our goal is to update Order status. Well, the status need to get updated in the DB. My concern is where to put the updateStatus method.

  1. Should I call OrderManagerService.getOrder then, call Order.updateStatus?
  2. or create a new method as OrderManagerService.updateOrderStatus?

well, 1st option seems following encapsulation. But, personally I dont like it as we may end up calling DAO layer from entity objects [perhaps, that might be ok]. Wondering what would be a right design choice and why? any help is greatly appreciated.


Solution

  • Option 2 - create a new method as OrderManagerService.updateOrderStatus?

    Why?

    Your service layer should encapsulate the logical business unit of work and in your case the UOW is

    1. get the order from DB
    2. update the status of the object
    3. persist the changes

    and you would demarcate the updateOrderStatus(...) with a transaction. and the service is still stateless.