I want to write a common method which returns any model like product, sales,etc. Something like this (.net 3.5; I’m not using entity framework)
public class ProductRepository<TEntity> : IProduct<TEntity>
where TEntity : class
{
public IEnumerable<TEntity> GetProductList(string Type)
{
IEnumerable<Product> fLit = from p in ProductList
select p;
return fLit;
}
}
But I’m getting the following error
Cannot implicitly convert type
System.Collections.Generic.IEnumerable<Product>‘ to
System.Collections.Generic.IEnumerable<TEntity>. An explicit conversion exists (are you missing a cast?)
Any help appreciated. Thanks in advance.
I’m afraid you have to change design of your Domain, well this is not how Repository Pattern going to implement. First of all You have to have a base class for your Domain Models something simple like below (Of course this is not necessary):
then you must have a generic IRepository interface :
after you implement generic IRepository interface you need to have a concrete Repository class which is inherited from you generic interface, like this :
this is neat, so as you can see here we expect DbContext parameter for Repository class constructor. Also we take the advantage of our entity base’s Id property to find what exactly we want.
Well till now you implement a basics of Repository pattern, from now on, you need to create a Repository class for each Domain Entity. let’s implement what you’ve asked here :
Hope this help.