2011-01-24 35 views
5

Tôi đã đọc một chút về các ràng buộc và đang cố triển khai nó trong mẫu kho lưu trữ của mình.Thực hiện Thừa kế Giao diện Chung và Lớp cho Mô hình Kho lưu trữ

Tôi muốn một cái gì đó như dưới đây nhưng không thể làm cho nó biên dịch.

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

Có quá nhiều lỗi trong mã của bạn (tôi có thể phát hiện 3 ít nhất) để hiểu bạn muốn gì. – leppie

+0

Đọc các ghi chú trên IEmployeeRepo và tôi cũng đã đề cập đến nó sẽ không biên dịch – Jon

Trả lời

16

Không chắc chắn lý do bạn có hai thông số loại trên Kho lưu trữ - điểm là gì?

* Dưới đây là ví dụ điển hình của một Repository NET sử dụng Generics: *

* Đầu tiên, giao diện Repository: *

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); 
} 

* Thứ hai, việc thực hiện Generic Repository (EF làm ví dụ): *

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 
} 

* Sau đó, Repository cụ thể (bạn nên có một cho mỗi gốc tổng hợp, và cũng là một giao diện cho từng kho cụ thể chi tiết các phương pháp cụ thể): *

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);  
    } 
} 

Cách sử dụng:

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

Đừng đi ra ngoài tìm cách để đi quá điên với Generics - người duy nhất bạn cần là để hạn chế Repository được sử dụng bởi một thực thể được đóng gói đằng sau Repository.

Tôi chỉ cần sử dụng where T : class.

Sử dụng khác where T : IDomainAggregate hoặc tương tự, để đặt các ràng buộc vào loại thực thể thực thể được phép.

+0

Tôi có thể hỏi bạn có nhận được tài liệu này từ đâu đó hay bạn đã viết nó? – Jon

+0

đã tự viết nó. Mã này tương tự như những gì tôi sử dụng. – RPM1984

+0

+1 cho tổng hợp miền. – womp

1

Hãy thử điều này;

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

Trong trường hợp này, tôi thường có lớp repo cơ sở triển khai IRepository <> và được nhập vào lớp Mô hình cơ sở.

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;} 
} 
Các vấn đề liên quan