title: How to offer (optional) notification handlers in MediatR
body: <p>I am using Jimmy Bogard's <a href="https://github.com/jbogard/MediatR/" rel="nofollow noreferrer">MediatR</a> library in a console app to call various SOAP end points. The app is configured and working beautifully through the magic of a few <code>IRequestHandler&lt;T1, T2&gt;</code> implementations.</p>
  
  <p>However, in all of my command line utility calls, there are 2 flags that allow the user to save the Xml response (along with other details) to a database table and / or save a text file. I feel like this is a great opportunity for an <code>INotificationHandler&lt;T&gt;</code>, but I can't seem to wrap my head around the workflow. </p>
  
  <p>It is my understanding that once registered via IoC, they are always listening. However, (in my case) they would be dynamically set depending on the command line flags. </p>
  
  <pre class="lang-cs prettyprint-override"><code>public class SoapRequest : IRequest&lt;string&gt;
  {
     // lots of properties here...
  
     public bool SkipDatabase { get; set; }
     public string OutputFile { get; set; }
  }
  
  // in my handler
  public class SoapRequestHandler : IRequestHandler&lt;SoapRequest, string&gt;
  {
  
     public async Task&lt;string&gt; Handler(SoapRequest request, CancellationToken token)
     {
        // fetches soap data, and does other stuff here... 
        var responseXml = await _endPoint.DoStuff(request.Prop1, ...);
  
        if (!request.SkipDatabase)
        {
           var dbCommand = new DatabaseCommand 
           { 
              ResponseString = responseXml, 
              ... 
           };
  
           await _mediator.Send(dbCommand, token);
        }
  
        if (!string.IsNullOrEmpty(request.OutputFile))
        {
           var fileCommand = new XmlFileCommand 
           { 
              FileName = request.OutputFile, 
              Xml = responseXml, 
              ... 
           };
  
           await _mediator.Send(fileCommand, token);
        }
  
        return responseXml;
     }
  }
  </code></pre>
  
  <h2>Consider The Following:</h2>
  
  <p><em>This will save to the results to a database table</em></p>
  
  <pre><code>&gt; myapp fetch_users 
  </code></pre>
  
  <p><em>This will write the results to a text file, and <strong>NOT</strong> save to the database</em></p>
  
  <pre><code>&gt; myapp fetch_users --output-file="c:\temp\users.xml" --skip-db
  </code></pre>
  
  <p>I'm currently managing this with 2 separate <code>IRequest</code> commands / handlers, and it's starting to exude a code smell. Some of my low-level handlers are exposing higher-level properties on the request objects that don't apply. I'd like to make use of notification handlers to solve this, but only register it on demand, depending on the users CLI flags.</p>
  
  <p>Since MediatR is configured in IoC at <em>start up</em>, this presents a problem for me. My concern is that if I register two notification handlers (<code>DatabaseNotificationHandler</code> and <code>FileSystemNotificationHandler</code>), they will always be listening, and I can't figure out how to get the flags into the handlers dynamically without passing them to my deepest handlers. Is there a better way? </p>
  
  <p><code>#WWJBD</code> - What would Jimmy Bogard do? :)</p>