title: CQRS: Should the ICommandHandler<TCommand> be responsible for calling SaveChanges() on the IUnitOfWork?
body: <p>Let's say I have a command handler:</p>
  
  <pre><code>public class AddNewUserCommandHandler : ICommandHandler&lt;AddNewUserCommand&gt;
  {
      //IUserManagementUnitOfWork implements IUnitOfWork.SaveChanges()
      //hold references to related repositories
      //basically an abstraction over DbContext/DbSet
      private IUserManagementUnitOfWork uow; 
  
      public AddNewUserCommandHandler(IUserManagementUnitOfWork uow)
      {
          this.uow = uow;
      }
  
      public void Handle(AddNewUserCommand command)
      {
          var user = new User
          {
              FirstName = command.FirstName,
              LastName = command.LastName
          };
          uow.UserRepository.Add(user);
          uow.SaveChanges(); //should this be here?
          command.UserId = user.Id;
      }
  }
  </code></pre>
  
  <p>My question is: should the <code>ICommandHandler&lt;TCommand&gt;</code> be responsible for calling <code>SaveChanges()</code> on the <code>IUnitOfWork</code>? or should this be the responsibility of the caller?</p>
  
  <p>For example, the <code>AddNewUserCommandHandler</code> would only have the <code>IRepository&lt;User&gt;</code> as its dependency, and the caller would hold reference to the <code>IUnitOfWork</code> and call <code>SaveChanges()</code> when it feels like it. The caller might be an <code>ICommandProcessor</code> that can process multiple <code>ICommand</code>s and call <code>SaveChanges</code> when all commands are handled by its handlers successfully.</p>
  
  <p>Or, leave the <code>SaveChanges()</code> duty to the <code>ICommandHandler&lt;TCommand&gt;</code> and wrap the batch command processor in a <code>TransactionScope</code>, and delegate <code>command.UserId = user.Id</code> to an event that will execute after calling <code>TransactionScope.Complete()</code>.</p>
  
  <p>Which approach is the better one?</p>
  
  <p>Thanks!</p>