title: Type directly or indirectly depending on itself Simple Injector
body: <p>I want to create Cache service which get regular service as constructor parameter. Then when cache key does not exist I want to call regular service and update cache. My idea is to have the same interface in regular service and cache service. But when I'm trying to inject cache service implementation and execute method I get exception:</p>
  
  <blockquote>
    <p>The registered delegate for type IUserRepository threw an exception. The configuration is invalid. The type CacheUserRepository is directly or indirectly depending on itself.</p>
  </blockquote>
  
  <p>My code:</p>
  
  <pre><code>public interface IUserRepository
  {
      UserDTO Get(int userId);
  }
  
  public class UserRepository : IUserRepository
  {
      public virtual UserDTO Get(int userId)
      {
              return new UserDTO() { Id = 1, Age = 28, Name = "Emil" };
      }
  }      
  </code></pre>
  
  <p>Here's my cache repository:</p>
  
  <pre><code>public class CacheUserRepository : IUserRepository
  {
      private readonly IUserRepository _userRepository;
      private readonly ICache _cache;
  
      public CacheUserRepository(IUserRepository userRepository, ICache cache)
      {
              _userRepository = userRepository;
              _cache = cache;
      }
  
      public DTO.UserDTO Get(int userId)
      {
              var userKey = "User_" + userId.ToString();
  
              UserDTO val = _cache.Get&lt;UserDTO&gt;(userKey);
  
              if (val != null)
                      return val;
  
              UserDTO user = _userRepository.Get(userId);
              _cache.Add(userKey, user);
  
              return user;
      }
  }
  </code></pre>
  
  <p>Here's my composition root:</p>
  
  <pre><code>public class ExecutionClass
  {
      private readonly Container _container;
  
      public ExecutionClass()
      {
          _container = new Container();
  
          _container.Register&lt;IUserRepository, CacheUserRepository&gt;();
          _container.Register&lt;ICache, Cache&gt;();
      }
  
      public UserDTO GetUser(int Id)
      {
          //throws: The type CacheUserRepository is directly or indirectly depending
          // on itself.
          var userRepo = _container.GetInstance&lt;IUserRepository&gt;(); \
          return userRepo.Get(Id);
      }
  }
  </code></pre>