title: Autofac generic decorator linking
body: <p>I have a generic <code>RepositoryDecorator&lt;T&gt;</code> decorator. <code>MyRepositoryDecoratorBase&lt;T&gt;</code> and <code>NoteRepository&lt;Note&gt;</code> are inheriting from it.</p>
  
  <p><code>IRepository&lt;T&gt;</code> is an interface and <code>Repository&lt;T&gt;</code> is an implementation of it. This is also registered using Autofac as shown in code sample.</p>
  
  <p>Trying to use autofac to generate instance of MyRepositoryDecoratorBase every time an instance of NoteRepository is asked for.</p>
  
  <p>That way I can link the decorators for cross cutting concerns like logging.</p>
  
  <p><strong>Abstract Decorator</strong></p>
  
  <pre><code>    public abstract class RepositoryDecorator&lt;TAggregate&gt;:IRepository&lt;TAggregate&gt; 
      where TAggregate:AggregateRoot, new() 
  {
      protected readonly IRepository&lt;TAggregate&gt; Repository;
  
      protected RepositoryDecorator(IRepository&lt;TAggregate&gt;  repository)
      {
          Repository = repository;
      }
  
      public virtual TAggregate GetById(Guid Id)
      {
          return Repository.GetById(Id);
      }
  
      public virtual void Save(TAggregate aggregate)
      {
          Repository.Save(aggregate);
      }
  
  }
  </code></pre>
  
  <p><strong>Generic LoggingDecorator</strong></p>
  
  <pre><code>    public class MyRepositoryDecoratorBase&lt;T&gt;:RepositoryDecorator&lt;T&gt; where T : AggregateRoot, new()
  {
      private DateTime _commitStartTime;
  
      public MyRepositoryDecoratorBase(IRepository&lt;T&gt; repository) : base(repository)
      {
      }
  
      public override T GetById(Guid Id)
      {
          BeforeLoadAggregate(Id);
          var result =  base.GetById(Id);
          AfterLoadingAggregate(result);
          return result;
      }
  
      public override void Save(T aggregate)
      {
          BeforeSaveAggregate(aggregate);
          base.Save(aggregate);
          AfterSavingAggregate(aggregate);
      }
  
      protected void BeforeLoadAggregate(Guid id)
      {
          Console.ForegroundColor = ConsoleColor.Red;
          Console.WriteLine($"Loading {id} ...");
          Console.ForegroundColor = ConsoleColor.White;
      }
  
      protected void AfterLoadingAggregate(T aggregate)
      {
          Console.ForegroundColor = ConsoleColor.Red;
          Console.WriteLine($"Loaded {aggregate.GetType()} ...");
          Console.ForegroundColor = ConsoleColor.White;
      }
  
      protected void BeforeSaveAggregate(T aggregate)
      {
          _commitStartTime = DateTime.Now;
          Console.WriteLine($"Trying to commit {aggregate.GetUncommittedChanges().Count()} events to storage.");
      }
  
      protected void AfterSavingAggregate(T aggregate)
      {
          Console.WriteLine($"Committed in {DateTime.Now.Subtract(_commitStartTime).TotalMilliseconds} ms.");
      }
  }
  </code></pre>
  
  <p><strong>Note Decorator</strong></p>
  
  <pre><code>public class NoteRepository:RepositoryDecorator&lt;Note&gt;
  {
      public NoteRepository(IRepository&lt;Note&gt; repository) : base(repository)
      {
  
      }
  
      public override void Save(Note aggregate)
      {
          LogManager.Log("Saving Note...", LogSeverity.Information);
          base.Save(aggregate);
          LogManager.Log("Note Saved...", LogSeverity.Information);
      }
  }
  </code></pre>
  
  <p><strong>Note: I'm using this to Register a base Repository implementation for IRepository</strong> </p>
  
  <pre><code>            //This will resolve and bind storage types to a concrete repository of &lt;T&gt; as needed
          builder.RegisterGeneric(typeof(Repository&lt;&gt;)).As(typeof(IRepository&lt;&gt;)).SingleInstance();
  </code></pre>
  
  <p>I've looked at the solution in <a href="https://stackoverflow.com/questions/17438570/autofac-register-multiple-decorators">Autofac - Register multiple decorators</a></p>
  
  <p>But couldn't get it working. I have a feeling my Generic registration of Repository is getting in the way.</p>
  
  <p><strong>The following works but I want to link multiple Decorators automatically without hardcoding it.</strong></p>
  
  <pre><code> NoteRepository rep = new NoteRepository(new MyRepositoryDecoratorBase&lt;Note&gt;(container.Resolve&lt;IRepository&lt;Note&gt;&gt;())); 
  </code></pre>