title: Accessing Session in Domain Object
body: <p>We use event sourcing in our app and also have a strict need to track the user who initiated changes to many of our objects. Currently we have code like this</p>
  
  <pre><code>class Order { 
    setNameBy(newname, User user) {
      applyChange(new OrderRenamed(user.id, newname));
    }
    :
  }
  </code></pre>
  
  <p>Since most of our methods are like this and all of them are called like this </p>
  
  <pre><code>setNameBy("a new name", SessionContext.currentUser)
  </code></pre>
  
  <p>we where contemplating accessing the SessionContext inside the domain object. i.e:</p>
  
  <pre><code>setNameBy(newname, User user) {
    applyChange(new OrderRenamed(user.id, newname));
  }
  </code></pre>
  
  <p>becomes</p>
  
  <pre><code>setName(newname) {
    applyChange(new OrderRenamed(SessionContext.currenUser.id, newname));
  }
  </code></pre>
  
  <p>I personally prefer the later method signature as it seams more natural on the other hand it feels a bit messy to access the SessionContext inside the Domain object. </p>
  
  <p>So how do you best handle Session data like this in DDD/CQRS apps ?. Is it OK to access the SessionContext in the Domain objects or should I use other methods like event enrichment to add this information to the events emitted from the domain ?.</p>