title: DDD Layers and External Api
body: <p>Recently I've been trying to make my web application use separated layers.</p>
  
  <p>If I understand the concept correctly I've managed to extract:</p>
  
  <ol>
  <li><p>Domain layer
  This is where my core domain entities, aggregate roots, value objects reside in. I'm forcing myself to have pure domain model, meaning i do not have any service definitions here. The only thing i define here is the repositories, which is actually hidden because axon framework implements that for me automatically.</p></li>
  <li><p>Infrastructure layer
  This is where the axon implements the repository definitions for my aggregates in the domain layer</p></li>
  <li><p>Projection layer
  This is where the event handlers are implemented to project the data for the read model using MongoDB to persist it. It does not know anything other than event model (plain data classes in kotlin)</p></li>
  <li><p>Application layer
  This is where the confusion starts.</p></li>
  <li><p>Controller layer
  This is where I'm implementing the GraphQL/REST controllers, this controller layer is using the command and query model, meaning it has knowledge about the Domain Layer commands as well as the Projection Layer query model.</p></li>
  </ol>
  
  <p>As I've mentioned the confusion starts with the application layer, let me explain it a bit with simplified example.</p>
  
  <p>Considering I want a domain model to implement Pokemon fighting logic. I need to use PokemonAPI that would provide me data of the Pokemon names stats etc, this would be an external API i would use to get some data. </p>
  
  <p>Let's say that i would have domain implemented like this:
  (Keep in mind that I've stretched this implementation so it forces some issues that i have in my own domain)</p>
  
  <pre><code>Pokemon {
    id: ID 
  }
  </code></pre>
  
  <pre><code>PokemonFight {
    id: ID
    pokemon_1: ID
    pokemon_2: ID
  
    handle(cmd: Create) {
      publish(PokemonFightCreated)
    }
  
    handle(cmd: ProvidePokemonStats) {
      //providing the stats for the pokemons
      publish(PokemonStatsProvided)
    }
  
    handle(cmd: Start) {
      //fights only when the both pokemon stats were provided
      publish(PokemonsFought)
    }
  </code></pre>
  
  <p>The flow of data between layers would be like this.</p>
  
  <p>User -> [HTTP] -> Controller -> [CommandGateway] -> (Application | Domain) -> [EventGateway] -> (Application | Domain)</p>
  
  <p>Let's assume that two of pokemons are created and the use case of pokemon fight is basically that when it gets created the stats are provided and then when the stats are provided the fight automatically starts.</p>
  
  <p>This use case logic can be solved by using event processor or even saga.</p>
  
  <p>However as you see in the PokemonFight aggregate, there is [ProvidePokemonStats] command, which basically provides their stats, however my domain do not know how to get such data, this data is provided with the PokemonAPI.</p>
  
  <p>This confuses me a bit because the use case would need to be implemented on both layers, the application (so it provides the stats using the external api) and also in the domain? the domain use case would just use purely domain concepts. But shouldn't i have one place for the use cases?</p>
  
  <p>If i think about it, the only purpose saga/event processor that lives in the application layer is to provide proper data to my domain, so it can continue with it's use cases. So when external API fails, i send command to the domain and then it can decide what to do.</p>
  
  <p>For example i could just put every saga / event processor in the application, so when i decide to change some automation flow i exactly know what module i need to edit and where to find it.</p>
  
  <p>The other confusion is where i have multiple domains, and i want to create use case that uses many of them and connects the data between them, it immediately rings in my brain that this should be application layer that would use domain APIs to control the use case, because I don't think that i should add dependency of different domain in the core one.</p>
  
  <p>TL;DR</p>
  
  <ol>
  <li><p>What layer should be responsible of implementing the automated process between aggregates (can be single but you know what i mean) if the process requires some external API data. </p></li>
  <li><p>What layer should be responsible of implementing the automated process between aggregates that live in different domains / micro services.</p></li>
  </ol>
  
  <p>Thank you in advance, and I'm also sorry if what I've wrote sounds confusing or it's too much of text, however any answers about layering the DDD applications and proper locations of the components i would highly appreciate.</p>