开发者

Decoupling an ASP.NET MVC application from Entity Framework

If I have this project structure

  • Foo.Data
    • reference EntityFramework
  • Foo.Business
    • reference Foo.Data
  • Foo.Web
    • reference Foo.Business

Isn't that supposed to allow me to prevent adding a reference to EntityFramework from Foo.Web?

How can I call S开发者_JS百科ystem.Data.Entity.Database.SetInitializer() from my global.asax.cs without adding the EntityFramework reference?


Why do you want to?

The reason you're doing this uncoupling is (I assume) to enable you to switch out the data tier at a later point, without having to modify anything in the Web project, and as little as possible in the Business project. To accomplish this, you should make sure that all your classes work against interfaces, rather than against concrete implementations.

In your example, you should probably define a Repository interface of some sort, which includes an Initialize() method. You then create a class (perhaps your specialized DbContext) implement the interface, and you work against that. In the Initialize() method on your repository, you call Database.SetInitializer() and thus you never have to reference System.Data.Entity in either the web or business projects.


What you can do is create a 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 reference to Foo.Business


Nope. If Foo.Web needs classes in EntityFramework, it will have to reference it. References don't cascade between projects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜