Generic Interface Inheritance and Class Implementation for Repository Pattern?

Here is the classic example of a . NET Repository using Generics: First, the Repository Interface: public interface IRepository where T : class { T FindSingle(Expression> predicate); IQueryable FindAll(); // optional - matter of preference void Add(T entity); void Remove(T entity); } Second, the Generic Repository Implementation (EF as the example): public abstract class GenericRepository : IRepository { private IObjectSet _ObjectSet; // get this in via DI (for example) public T FindSingle(Expression> predicate) { return _ObjectSet. SingleOrDefault(predicate); } // you can figure out how to do the other implementation methods } Then, the Specific Repository (you should have one per aggregate root, and also an interface for each specific repository detailing specific methods): public EmployeeRepository : GenericRepository, IRepository { // 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); } } Usage: IRepository repository = new EmployeeRepository() Don't go out looking to go too crazy with generics - the only one you need is to constrain the Repository to be used by a entity that is encapsulated behind the Repository I simply use where T : class Other's use where T : IDomainAggregate or similar, to put constraints on the actual type of entity which is allowed.

Here is the classic example of a . NET Repository using Generics: * *First, the Repository Interface: * public interface IRepository where T : class { T FindSingle(Expression> predicate); IQueryable FindAll(); // optional - matter of preference void Add(T entity); void Remove(T entity); } *Second, the Generic Repository Implementation (EF as the example): * public abstract class GenericRepository : IRepository { private IObjectSet _ObjectSet; // get this in via DI (for example) public T FindSingle(Expression> predicate) { return _ObjectSet. SingleOrDefault(predicate); } // you can figure out how to do the other implementation methods } *Then, the Specific Repository (you should have one per aggregate root, and also an interface for each specific repository detailing specific methods): * public EmployeeRepository : GenericRepository, IRepository { // 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); } } Usage: IRepository repository = new EmployeeRepository(); Don't go out looking to go too crazy with generics - the only one you need is to constrain the Repository to be used by a entity that is encapsulated behind the Repository. I simply use where T : class.

Other's use where T : IDomainAggregate or similar, to put constraints on the actual type of entity which is allowed.

– Jon Jan 24 at 11:57 wrote it myself. The code is similar to what I use. – RPM1984 Jan 24 at 20:54.

In this situation I usually have a base repo class that implements IRepository, and is typed to a base Model class. Public interface IRepository where T : IModel { void GetAll(); void GetById(int id); } public interface IEmployeeRepo : IRepository where T : IModel { void DoSomethingEmployeeRelated(); } public class BaseRepo : IRepository where T : IModel { public void GetAll() { } public void GetById(int id) { } } public class EmployeeRepo : BaseRepo, IEmployeeRepo { public void DoSomethingEmployeeRelated() { } } //My example model class public class Employee : IModel { public int Id {get;set;} public string Name {get;set;} }.

Try this; public interface IRepository { void GetAllData(); } //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 : IRepository where V : EmployeeRepo where T : class { void DoSomethingEmployeeRelated(); } //Don't think this inheritance is correct public class EmployeeRepo : IEmployeeRepo { public void DoSomethingEmployeeRelated() { } public void GetAllData() { } } //My example model class public class Employee { public string Name {get;set;} }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions