Chcę utworzyć usługę pamięci podręcznej, która otrzyma regularną usługę jako parametr konstruktora. Następnie, gdy klucz pamięci podręcznej nie istnieje, chcę wywołać zwykłą usługę i zaktualizować pamięć podręczną. Moim pomysłem jest posiadanie tego samego interfejsu w zwykłej usłudze usługi i pamięci podręcznej. Ale kiedy próbuję wstrzyknąć implementację usługi buforowania i wykonać metodę otrzymuję wyjątek:Wpisz bezpośrednio lub pośrednio w zależności od siebie Prosty wtryskiwacz
Zarejestrowany przedstawiciel dla typu IUserRepository zgłosił wyjątek. Konfiguracja jest nieprawidłowa. Typ CacheUserRepository jest bezpośrednio lub pośrednio zależny od niego samego.
Mój kod:
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" };
}
}
Oto mój repozytorium cache:
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<UserDTO>(userKey);
if (val != null)
return val;
UserDTO user = _userRepository.Get(userId);
_cache.Add(userKey, user);
return user;
}
}
Oto mój główny skład:
public class ExecutionClass
{
private readonly Container _container;
public ExecutionClass()
{
_container = new Container();
_container.Register<IUserRepository, CacheUserRepository>();
_container.Register<ICache, Cache>();
}
public UserDTO GetUser(int Id)
{
//throws: The type CacheUserRepository is directly or indirectly depending
// on itself.
var userRepo = _container.GetInstance<IUserRepository>(); \
return userRepo.Get(Id);
}
}
Musisz użyć metody RegisterDecorator, aby zarejestrować CacheUserRepository – qujck