Maks Martynov
Maks Martynov

Reputation: 458

EF generic repository to any data provider

I'm trying to make my EF generic repository more "wide" to work with any data provider(ORM) like EF, NHibernate, Amazon Services and so on without any binding to EF context.

For now I have(gist links):

I'm have rewrite IRepository and part of Repository.

But dunno what to do with EF and how finally get rid of it.

Can you give me some articles or code samples to get the right direction in my work.

Upvotes: 2

Views: 426

Answers (1)

Wahid Bitar
Wahid Bitar

Reputation: 14104

The idea of IRepository id to make different implementation for each ORM in the Repository. That way you don't have to build your solution depending on the ORM itself.

Then I think you're making it wrong when you try to make your repository to deal with many ORM's

Update:

You should build your BLL depending on IRepository without knowing anything about the implementation then you may use Constructor Injection or Dependency Resolver to pass the suitable implementation to the BLL.

E.G.

public interface IProductRepository{
   void AddProduct(Product model);
   void UpdateProduct(Product model);
}

EF implementation :

public class ProductRpository : IProductRepository
{
   void AddProduct(Product model)
   {
      //Add any code you want maybe EF code.
   }
   void UpdateProduct(Product model)
   {
      //........
   }
}

Then in the BLL you depend on IProductRepository ont on ProductRepository :

public class ProductService
{
   private readonly IProductRepository repository
   public ProductService(IProductRepository repository){
      this.repository = repository
   }
// ........ the code of your BLL
}

Upvotes: 1

Related Questions