0

First of all, I'm quite new in DDD, so I may get some concepts wrong.

The problem I'm facing in is as follows:

I have a list of tickets for some processing system. The tickets are based on a data from external sources crawled / tracked by Crawlin System.

Obviously, these two are separate Bounded Contexts. As for now I'm quite certain, that my boundaries are correct here, as they have little to none common points (ticket is connected to an external source, but they are processed and updated separately and in different ways).

Now, I want to create a ticket list with all tickets. Each ticket has some data from one bounded context (available actions etc) and some data from another (graphical representation of external data).

I already know, that to avoid data duplication I may use a composite view of each ticket, where each bounded context renders its own UI parts. I've seen Amazon site decomposition. Let's also assume, that my templating framework allows me to use blocks, or components to compose the ticket view.

The problem

I'm struggling with a method of retrieving these views / UI parts in an application without too many queries. I don't want to make one request for each component to avoid N+1 query hell (even if those queries are on some NoSQL cache). I imagine, that my view controller somehow asks both bounded contexts to provide a list of controls and then renders them (e.g one to retrieve a list of tickets based on search criteria and other to retrieve external content based on a list of external content IDs). But I'm not sure if I'm on a right track.

Code examples in any language (pseudo language) are much appreciated.

Tomasz Struczyński
  • 3,273
  • 23
  • 28

1 Answers1

0

I think you are on right track.

Just as you wrote, you can create some sort of view model (view entity) for this with a service that would query one source and second (external) source just once. Then it would merge them into single view models which will be passed onto the UI layer.

This way you also keep the logic of data merging in the domain layer. In the presentation layer you would work with them just as normal view models (entities) - without leaking the information such as their source, how to merge them etc.

Once in the domain layer, you can add local caching too (if required).

<?php

class ViewTicketProvider
{
    private $externalService;

    public function __construct(ExternalProviderInterface $externalService)
    {
        $this->externalService = $externalService;
    }

    public function getViewTickets(Collection $tickets)
    {
        $ids = $tickets->getIds();

        $externalEntities = $this->externalService->getByIds($ids);

        $viewTickets = array();

        foreach ($tickets as $ticket) {
            $viewTicket = new ViewTicket($ticket);
            // If this is too complex, use another service for the merging.
            $viewTicket->addMoreInfoFromExternalSource($externalEntities);

            $viewTickets[] = $viewTicket;
        }

        return $viewTickets;
    }
}


$ticketRepo      = new TicketRepository();
$externalService = new ExternalService();

$tickets = $ticketRepo->getTicketsBasedOnSomeCriteria($criteria);

$viewTicketProvider = new ViewTicketProvider($externalService);
$viewTickets = $viewTicketProvider->getViewTickets($tickets);
Tomas Dermisek
  • 778
  • 1
  • 8
  • 14