[英]How to make the data access technology (Entity Framework) ignorance from the presentation layer (ASP.NET MVC)?
Premise: 前提:
I am exercising Domain-Driven Design and I separate my solution into 4 layers: 我正在执行域驱动设计,并将解决方案分为4层:
The domain layer only has a set of repository interfaces defined for the aggregates and it's up to the implementation data access mechanism which exists in the infrastructure layer to hide the implementation details. 域层仅具有一组为聚合定义的存储库接口,这取决于基础结构层中隐藏实现细节的实现数据访问机制。
In this exercise, I decide to use Entity Framework Database first approach. 在本练习中,我决定使用实体框架数据库优先方法。 And of course, there is a app.config
in the infrastructure project that contains a connection string. 当然,基础结构项目中有一个app.config
,其中包含一个连接字符串。
Problems: 问题:
Ok, I spend a great deal and time trying to separate all the concerns and to focus on domain models. 好的,我花了大量时间来尝试分离所有问题并专注于领域模型。 In the presentation layer (ie, the API and MVC projects), there is no direct reference to the infrastructure project. 在表示层(即API和MVC项目)中,没有直接引用基础结构项目。 And IoC container has been setup so all concrete implementation of the required interfaces would be injected into controller constructors. 并且已经设置了IoC容器,因此所需接口的所有具体实现都将注入到控制器构造函数中。
When I select, for example, the API project as start project and run it, I got 例如,当我选择API项目作为开始项目并运行它时,我得到了
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code.
Additional information: No connection string named 'xxxxxx' could be found in the application config file.
Question: 题:
Now I understand if I install Entity Framework into the API project, copy and paste connection string from the app.config
of the infrastructure project into the web.config
of the API project, things will work. 现在,我知道如果将Entity Framework安装到API项目中,将基础结构项目的app.config
中的连接字符串复制并粘贴到API项目的web.config
中,则一切正常。 But that breaks our original purpose of separating concerns, doesn't it? 但这违反了我们分离关注点的初衷,不是吗? If we do that, then what's the point of using Domain-Driven Design and making the data access technology ignorance from the presentation layer? 如果这样做,那么使用域驱动设计并使表述层的数据访问技术变得无知的意义何在?
The reason we don't directly reference direct implementation of data access technology (ie concrete implementations that use dbContext
and Linq
) is that we could easily switch the underground access technology to something else. 我们之所以没有直接引用数据访问技术的直接实现(即,使用dbContext
和Linq
具体实现)的原因是,我们可以轻松地将地下访问技术转换为其他东西。
So what would be the proper way to do it?!! 那么什么是正确的方法呢?
I do not want to install Entity Framework in my presentation layer, nor copy the connection string everywhere. 我不想在表示层中安装Entity Framework,也不想在各处复制连接字符串。 I want all the data access and concrete implementation of repositories exist in just one library. 我希望所有数据访问和存储库的具体实现都存在于一个库中。
The Entity Framework configuration must be in the project where it is being used. 实体框架配置必须在使用它的项目中。 This doesn't mean it's going to break your layered structure or your separation of concerns. 这并不意味着它将破坏您的分层结构或您的关注点分离。
Remove all entityframework elements from your app.config. 从您的app.config中删除所有entityframework元素。 Create your own connection string element and provide it to entityframework on app startup. 创建您自己的连接字符串元素,并在应用启动时将其提供给entityframework。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.