मैं कैश सेवा बनाना चाहता हूं जो नियमित रूप से कन्स्ट्रक्टर पैरामीटर के रूप में सेवा प्राप्त करे। फिर जब कैश कुंजी मौजूद नहीं है तो मैं नियमित सेवा और कैश अपडेट करना चाहता हूं। मेरा विचार नियमित सेवा और कैश सेवा में एक ही इंटरफ़ेस होना है।अपने आप के आधार पर प्रत्यक्ष या परोक्ष रूप से टाइप करें सरल इंजेक्टर
पंजीकृत प्रतिनिधि प्रकार के लिए IUserRepository एक अपवाद दिए: लेकिन जब मैं कैश सेवा कार्यान्वयन इंजेक्षन और विधि निष्पादित करने के लिए कोशिश कर रहा हूँ मैं अपवाद मिलता है। कॉन्फ़िगरेशन अमान्य है। प्रकार कैशयूसर रिपोजिटरी सीधे या परोक्ष रूप से निर्भर करता है।
मेरे कोड:
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;
}
}
यहाँ मेरी रचना जड़ दिया गया है::
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" };
}
}
यहाँ मेरी कैश भंडार है
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);
}
}
आप विधि RegisterDecorator उपयोग करने के लिए CacheUserRepository – qujck