title: CQRS read model projection - business logic
body: <p>So, I trigger command on aggregate root and it has some 10 events happened as a result of the command. This events are internal ones, and since outer systems need aggregation of this events, I decided to make projection (read projection basically). In order to make this projection from 10 events (internal) TO 1 event (external), I have to apply some business rules (business rules concerning merging of events). Where should I put this rules, since it seems like part of domain but I'm creating projections of internal events?</p>
  
  <p>Basically since projection logic is part of domain, should I keep it inside aggregate and call it in code where projection is made?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>So, inside one aggregate root, I have e.g. 3 events (internal) as response to one Command (aggregate.createPaintandwashatsametime(id, red)) that is sent to aggregate root and that are spreading through all the aggregate root entities like: CarCreated(Id), CarSeatColored(Red), CarWashed() etc. (all this 3 events are happened because of single command). External system expects to receive one external event as CarMaintainenceDone(Id, repainted=true, washed=true, somevalue=22);</p>
  
  <p>Now, if i have some complex logic to make this CarMaintainenceDone event (like if(color==red then in projection somevalue==22 otherwise 44) - should this go in projection code or be part of domain?</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Let me try to give you new example. Just ignore how domain is modeled since this is just example:</p>
  
  <p>As you can see we have AggregateRoot that contains Multiplier which is there just to call things with the right name. When we do multiplication we first send integer 1 to ObjectA which has some logic to set internal state and emit ObjectAHasSetParam event. The same thing goes with ObjectB. Finally, ObjectC listens to all of this events, and on paramsHasBeenSet will do actual multiplication.</p>
  
  <p>In event store in this case I would preserve list of events: </p>
  
  <pre><code>[ObjectAHasSetParam , ObjectBHasSetParam , ObjectCHasMultiplied ]
  </code></pre>
  
  <p>My point here was: if I emit all of this events one by one out of process - the state that somebody else updates will possibly be inconsistent, since this 3 events make sense only together. That is why I wanted to make something like projection, but I think in this case I just need to publish list of this events together instead of event by event.</p>
  
  <pre><code>class AggregateRoot{
      Multiplier ml;
  
      void handle(MultiplyCommand(1,2)){
          ml.multiply(1,2);
      }
  }
  
  class Multiplier{
      ObjectA a;
      ObjectB b;
      ObjectC res;
  
      void multiply(1,2){
          a.setParam(1);
          b.setParam(2);
          publish(paramsHaveBeenSet());
      }
  }
  
  class ObjectA{
      int p;
  
      void setParam(1){
          p = 1 + 11;
          publish(ObjectAHasSetParam(12));
      }
  }
  
  class ObjectB{
      int p;
  
      void setParam(2){
          p = 2 + 22;
          publish(ObjectBHasSetParam(24));
      }
  }
  
  class ObjectC{
      int p1; int p2;
      int res;
  
      listen(ObjectAHasSetParam e1){
          p1 = e1.par;
      }
  
      listen(ObjectBHasSetParam e2){
          p2 = e2.par;
      }
  
      listen(paramsHaveBeenSet e3){
          res = p1 * p2;
          publish(ObjectCHasMultiplied(288));
      }
  }
  </code></pre>