2015-06-27 6 views
6

मैं कैश सेवा बनाना चाहता हूं जो नियमित रूप से कन्स्ट्रक्टर पैरामीटर के रूप में सेवा प्राप्त करे। फिर जब कैश कुंजी मौजूद नहीं है तो मैं नियमित सेवा और कैश अपडेट करना चाहता हूं। मेरा विचार नियमित सेवा और कैश सेवा में एक ही इंटरफ़ेस होना है।अपने आप के आधार पर प्रत्यक्ष या परोक्ष रूप से टाइप करें सरल इंजेक्टर

पंजीकृत प्रतिनिधि प्रकार के लिए 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); 
    } 
} 
+2

आप विधि RegisterDecorator उपयोग करने के लिए CacheUserRepository – qujck

उत्तर

7

क्या हो रहा है निम्नलिखित है: आप CacheUserRepository वर्ग पंजीकृत इसकी सेवा प्रकार IUserRepository द्वारा। मतलब यह है कि हर किसी को एक IUserRepository का अनुरोध करता है (या तो GetInstance<IUserRepository>() को फोन करके या निर्माता तर्क के रूप में एक IUserRepository की आवश्यकता के द्वारा), सरल इंजेक्टर एक नया CacheUserRepository उदाहरण के साथ यह आपूर्ति करेगा मतलब है।

अब तक इतना अच्छा है, लेकिन CacheUserRepository में IUserRepository का एक निर्माता तर्क है। यह एक नया CacheUserRepository उदाहरण के साथ अपने CacheUserRepository की आपूर्ति करने के लिए सरल इंजेक्टर का नेतृत्व करेगा। और निश्चित रूप से यह नया उदाहरण फिर से एक नए CacheUserRepository उदाहरण के साथ आपूर्ति की जाती है। चूंकि इससे स्टैक ओवरफ्लो अपवाद होता है, सरल इंजेक्टर इसे रोकता है और आपके द्वारा देखे गए अपवाद को फेंकता है।

आपका CacheUserRepository वास्तव में एक सजावट है। सरल इंजेक्टर को सजावटी हैंडलिंग के लिए समर्थन है।

container.Register<IUserRepository, UserRepository>(); 
container.RegisterDecorator<IUserRepository, CacheUserRepository>(); 

RegisterDecorator विधि चक्रीय निर्भरता का विशेष ख्याल रखता है और यह सुनिश्चित करें कि सरल इंजेक्टर अपनी पूंछ का पीछा नहीं करेंगे कर देगा: यह आपके पंजीकरण दिखना चाहिए कैसे की तरह है।

+0

यह मेरे लिए काम करता रजिस्टर की जरूरत है। आपकी सहायता के लिए धन्यवाद. – Emil

संबंधित मुद्दे