-1

I have service which is getting domain from repository and passing its own instance to the domain for some external dependency. Can it become anti pattern? RichDomainModel suggests to pass dependency to domain as required, however I am concerned if this can be called as cyclic dependency and would become anti pattern?

class Order
{
  private $currencyVal;

  public function __construct()
  {

  }

  public function processBusinessLogic($arg1, $arg2)
  {
      // do some business logic operation on $arg1, $arg2 & $this->currencyVal
  }

  public function populateExternalValue(OrderService $service)
  {
    $this->currencyVal = $service->getCurrencyValue($this);
  }
}


OrderService 
{
  private $orderRepository;
  private $externalServiceClient;

  public function __construct(OrderRepository $orderRepository, Client $externalServiceClient)
  {
    $this->orderRepository = $orderRepository;
    $this->externalServiceClient = $externalServiceClient;
  }

  public function processDomain(Request $request)
  {
     $order = $this->orderRepository->findById($request->orderId);
     $order->populateExternalValue($this);
     $order->processBusinessLogic($request->arg1, $request->arg2);
     $this->orderRepository->save($order);
  }

  public function getCurrencyValue(Order $order) 
  { 
    return $this->externalServiceClient->getValue($order->currency)
  }
}

Alternatively I could use something like this but that would lead to AnemicDomainModel

$order->setCurrencyValue($this->getCurrencyValue($order));

Thoughts?

1 Answers1

0
  1. This is definitely a cyclic dependency and that is an anti-pattern.
  2. If you want to enrich the domain model, the logic surrounding $currencyVal should move out of OrderService and into Order.
  3. Since the logic depends on $externalServiceClient, that client would move into Order as well.
  4. Note that anemic domain model is an OOP anti-pattern; but you don't have to do OOP. A rich domain model makes sense if you have a rich domain. For a basic CRUD application with very little logic, it may be over-engineering.
jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • It's initial phase and I have some level of complexity in Order domain which will increase over a period of time and that's the reason I wanted to have rich domain model. I am wondering if this is cyclic dependency and anti-pattern how can I get rid of it keeping the domain as rich? – Sunil Sharma Mar 11 '20 at 04:45