How can I create abstract layers between different ORMs (entity framework and Dapper)?

abstraction asp.net-core-2.0 c# dapper entity-framework

Question

I want to create abstract layer between entity framework and Dapper.In runtime i can choose either entity framework or Dapper or i will both of them also. I know that i can use interface

public IORM{
    Save();
    Delete();
    //other ORM functions
}

public EntityFramework : IORM{
    public Save(){
    SaveChanges();
    }
    public Delete(){
    Remove();
    }
}


public Dapper: IORM{
    public Save(){
    //save code goes here
    }
    public Delete(){
    //delete code goes here
    }

But this is basic operations and not sure how to configure in .net core 2.0 CofigureServices() Method.

Is abstractions between different ORMs advisable? If yes how to implement abstract layer between Entity framework and Dapper in .net core 2.0?

1
2
10/4/2017 7:51:06 AM

Popular Answer

It is always advisable to keep concerns separated. But it is an illusion that a full blown ORM can be hidden behind one interface definition. The runtime behavior of the ORM can have quite some consequences for the whole application and it will need alot of work to be able to just "swap" the ORM. There are not many good reasons to maintain that ability. What is your motivation behind that?

Of course, it is a good idea to abstract the concrete ORM work. Concrete examples really depend on your overall architecture, but to mention some points:

  • Keep persistence stuff (Attributes, dependencies) out of the model and controllers.
  • Apply the repository pattern and inject repository interface parameters into your application services. This way, you can hide concrete query / write operations of the ORM.
  • Apply the unit of work pattern and handle the Unit of Work transparently per request using the web framework infrastructure. This way, you can hide concrete transaction handling operations of the ORM.
4
10/4/2017 7:54:39 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow