2011-01-24 20 views
5

के लिए जेनेरिक इंटरफेस विरासत और कक्षा कार्यान्वयन मैंने बाधाओं के बारे में कुछ पढ़ा है और इसे अपने भंडार पैटर्न में लागू करने की कोशिश कर रहा हूं।रिपोजिटरी पैटर्न

मुझे नीचे की तरह कुछ चाहिए लेकिन इसे संकलित करने के लिए काफी कुछ नहीं मिल सकता है।

public interface IRepository<T> 
{ 
    void GetAllData<T>(); 
} 

//This needs to inherit from IRepository 
//T has to be a model class 
//V has to be a class that implements IEmployeeRepo 
public interface IEmployeeRepo<T, V> where V : EmployeeRepo where T : class : IRepository<T> 
{ 
    void DoSomethingEmployeeRelated(); 
} 

//Dont think this inheritance is correct 
public class EmployeeRepo<Employee, this> : IEmployeeRepo 
{ 


} 

//My example model class 
public class Employee 
{ 
    public string Name {get;set;} 
} 
+0

आप जो चाहते हैं उसे समझने के लिए आपके कोड में बहुत अधिक त्रुटियां हैं (मैं कम से कम 3 स्पॉट कर सकता हूं)। – leppie

+0

आईएमएमटीआई रिपो के ऊपर दिए गए नोट्स पढ़ें और मैंने यह भी बताया कि यह – Jon

उत्तर

16

सुनिश्चित नहीं है कि आपके पास रिपोजिटरी पर दो प्रकार के पैरामीटर क्यों हैं - बिंदु क्या है? *

* पहले, भंडार इंटरफ़ेस:: *

public interface IRepository<T> where T : class 
{ 
    T FindSingle(Expression<Func<T,bool>> predicate); 
    IQueryable<T> FindAll(); // optional - matter of preference 
    void Add(T entity); 
    void Remove(T entity); 
} 

* दूसरा, सामान्य भंडार कार्यान्वयन

* यहाँ जेनेरिक्स का उपयोग कर एक .NET भंडार का उत्कृष्ट उदाहरण है (उदाहरण के रूप में ईएफ): *

public abstract class GenericRepository<T> : IRepository<T> 
{ 
    private IObjectSet<T> _ObjectSet; // get this in via DI (for example) 

    public T FindSingle(Expression<T,bool>> predicate) 
    { 
     return _ObjectSet.SingleOrDefault(predicate); 
    } 

    // you can figure out how to do the other implementation methods 
} 

* फिर, विशिष्ट भंडार (आप कुल जड़ प्रति एक और साथ ही प्रत्येक विशिष्ट भंडार विशिष्ट विधियों का विवरण के लिए एक इंटरफेस होना चाहिए): *

public EmployeeRepository : GenericRepository<Employee>, IRepository<Employee> 
{ 
    // all regular methods (Find, Add, Remove) inherited - make use of them 
    public Employee FindEmployeeByName(string name) 
    { 
     return FindAll().SingleOrDefault(x => x.Name == name); 
     // or you could do: return FindSingle(x => x.Name == name);  
    } 
} 

उपयोग:

IRepository<Employee> repository = new EmployeeRepository<Employee>(); 

जेनेरिकों के साथ बहुत पागल होने के लिए बाहर निकलना न भूलें - आपको केवल एक ही रिपोजिटरी को रिपोजिटरी के पीछे encapsulated एक इकाई द्वारा उपयोग किया जाना है।

मैं बस where T : class का उपयोग करता हूं।

अन्य उपयोग where T : IDomainAggregate या इसी तरह के, वास्तविक प्रकार की इकाई पर बाधा डालने के लिए उपयोग किया जाता है।

+0

संकलित नहीं करेगा क्या मैं पूछ सकता हूं कि आप इस दस्तावेज़ को कहीं से प्राप्त करते हैं या आपने इसे लिखा है? – Jon

+0

ने इसे स्वयं लिखा था। कोड जो मैं उपयोग करता हूं उसके समान है। डोमेन समेकन के लिए – RPM1984

+0

+1। – womp

1

इसे आजमाएं;

public interface IRepository<T> 
{ 
    void GetAllData<T>(); 
} 

//This needs to inherit from IRepository 
//T has to be a model class 
//V has to be a class that implements IEmployeeRepo 
public interface IEmployeeRepo<T, V> : IRepository<T> where V : EmployeeRepo where T : class 
{ 
    void DoSomethingEmployeeRelated(); 
} 

//Dont think this inheritance is correct 
public class EmployeeRepo : IEmployeeRepo<Employee, EmployeeRepo> 
{ 
    public void DoSomethingEmployeeRelated() 
    { 

    } 

    public void GetAllData<Employee>() 
    { 

    } 
} 

//My example model class 
public class Employee 
{ 
    public string Name {get;set;} 
} 
5

इस स्थिति में मैं आम तौर पर एक आधार रेपो वर्ग कि IRepository < लागू करता है>, और एक आधार मॉडल वर्ग के लिए लिखा गया है।

public interface IRepository<T> where T : IModel 
{ 
    void GetAll<T>(); 
    void GetById<T>(int id); 
}  

public interface IEmployeeRepo<T> : IRepository<T> where T : IModel 
{ 
    void DoSomethingEmployeeRelated(); 
} 

public class BaseRepo : IRepository<T> where T : IModel 
{ 

    public void GetAll<T>() 
    { 

    } 

    public void GetById<T>(int id) 
    { 

    } 
} 


public class EmployeeRepo : BaseRepo<Employee>, IEmployeeRepo<Employee> 
{ 
    public void DoSomethingEmployeeRelated() 
    { 

    } 

} 

//My example model class 
public class Employee : IModel 
{ 
    public int Id {get;set;} 
    public string Name {get;set;} 
} 
संबंधित मुद्दे