title: Autofac - constructor parameter based on custom attribute, with assembly scanning
body: <p>I'm having the following issue, with how to configure the Autofac container, and can't really find a solution.</p>
  
  <p>Let's say I have a bunch of repositories, like AccountRepository, ContactRepository, LeadRepository etc.</p>
  
  <p>Each of those repositories has a single constructor parameter of type IService which provides implementations of basic CRUD methods. In my case it's a generic web service connection to a 3rd party application, but that doesn't really matter.</p>
  
  <p>So for example I have something like this:</p>
  
  <pre><code>public class AccountRepository
  {
      private readonly IService service;
  
      public AccountRepository(IService service)
      {
          this.service = service ?? throw new ArgumentNullException(nameof(service));
      }
  
      public int GetContactCount(Guid accountId)
      {
          using(DataContext ctx = new DataContext(service))
          {
              return ctx.Contacts.Where(c =&gt; c.AccountId == accountId).Count();
          }
      }
  }
  </code></pre>
  
  <p>My domain code is implemented through commands and events. So let's say I have a following command handler, using the above repository:</p>
  
  <pre><code>public class UpdateNrOfContactsCommandHandler : IHandleCommand&lt;UpdateNrOfContactsCommand, Account&gt;
  {
      private readonly AccountRepository accountRepo;
  
      public UpdateNrOfContactsCommandHandler(AccountRepository accountRepo)
      {
          this.accountRepo = accountRepo ?? throw new ArgumentNullException(nameof(accountRepo));
      }
  
      public void Execute(Account account)
      {
          account.NrOfContacts = repo.GetContactCount(account.Id);
      }
  }
  </code></pre>
  
  <p>I don't hide the repository behind any interface, because it's part of the business logic and there will only be one implementation. The IService is the dynamic part (and the one I fake in unit tests).
  If it helps I can add an interface over it, but that's just additional typing I would like to avoid.</p>
  
  <p>Both the repositories and command handlers are registered dynamically through assembly scanning. I obviously don't want to update my composition root every time I add a new repository or command handler. It should be configured once and left alone (unless we introduce some new abstractions).</p>
  
  <p>So basically i register all repositories from a certain assembly and all command handlers from another. When a command comes in I resolve the handler and call the .Execute() method on it. Pretty standard stuff.</p>
  
  <p><strong>The issue</strong> is that depending on the context (which only the command handler knows about, the repository should be totally agnostic) I either need the repositories to do their queries in a system admin context or in the current users context.</p>
  
  <p>I have two instances of the IService:</p>
  
  <pre><code>1. IService serviceAsAdmin
  2. IService serviceAsCurrentUser
  </code></pre>
  
  <p>The service in the admin context should be the default one. </p>
  
  <p>A solution I though about which would be elegant would be somethine like this:</p>
  
  <ol>
  <li><p>Define a custom attribute like</p>
  
  <pre><code>public class InUserContextAttribute : Attribute 
  {
  }    
  </code></pre></li>
  <li><p>Later use this attribute like this:</p>
  
  <pre><code>public class UpdateNrOfContactsCommandHandler : IHandleCommand&lt;UpdateNrOfContactsCommand, Account&gt;
  {
      private readonly AccountRepository accountRepo;
      private readonly AccountRepository accountRepoAsUser;
  
      public UpdateNrOfContactsCommandHandler(AccountRepository accountRepo, [InUserContext] AccountRepository accountRepoAsUser)
      {
          this.accountRepo = accountRepo ?? throw new ArgumentNullException(nameof(accountRepo));
          this.accountRepoAsUser = accountRepoAsUser ?? throw new ArgumentNullException(nameof(accountRepoAsUser));
      }
  
      public void Execute(Account account)
      {
          account.NrOfContacts = repo.GetContactCount(account.Id);
          account.NrOfContactsSeenByCurrentUser = accountRepoAsUser.GetContactCount(account.Id);
      }
  }
  </code></pre></li>
  </ol>
  
  <p><strong>And I don't know how to do this :)</strong> Looked into many samples, but none seem to fit this scenario. The additional complication is this needs to be dynamic, through assembly scanning.</p>
  
  <p>I know how to quite easily implement something similar through property injection (by using the Autofac's .OnActivated() method), but those repositories are not optional, so they should be passed in the constructor.</p>
  
  <p>Also I would like to avoid any Autofac references outside the composition root / command bus. Definitely don't want to add any Autofac specific stuff into my business logic.</p>
  
  <p>Basically what I need to do is to somehow register 2 instances of IService in such a way that it get's resolved to one of them if the parameter is not decorated and to the other if it's decorated with the InUserContextAttribute attribute.</p>
  
  <p>Is what I'm trying to achieve possible at all? And how? ;)</p>