Decoupling an ASP.NET MVC Application from the Entity Framework

If I have this project structure

  • Foo.Data
    • EntityFramework link
  • Foo.Business
    • link Foo.Data li>
  • Foo.web
    • link Foo.Business

Doesn’t this allow me to interfere with adding a link to EntityFramework from Foo.Web?

How can I call System.Data.Entity.Database.SetInitializer() from my global.asax.cs without adding an EntityFramework link?

+4
source share
3 answers

What you can do is create the InitializeDatabase() function in your Foo.Business project, which in turn calls System.Data.Entity.Database.SetInitializer() . You can then call InitializeDatabase() from your Foo.Web project, which already has a link to Foo.Business

+2
source

Why do you want?

The reason you do this decoupling is (I suppose) so that you can switch the data layer at a later stage without changing anything in the web project and as little as possible in the business project. To achieve this, you must ensure that all of your classes work against interfaces, and not against specific implementations.

In your example, you probably should define some kind of Repository interface that includes the Initialize() method. Then you create a class (perhaps your specialized DbContext) that implements the interface, and you work against it. In the Initialize() method in your repository, you call Database.SetInitializer() , and so you never need to reference System.Data.Entity in web or business projects.

+3
source

Nope. If Foo.Web needs classes in EntityFramework, it will have to reference it. Links are not cascaded between projects.

+2
source

All Articles