简体   繁体   English

如何使表示层(ASP.NET MVC)的数据访问技术(Entity Framework)无知?

[英]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层:

  • Presentation Layer 表示层
    • An ASP.NET Web API 2 project for a RESTful API web service 用于RESTful API Web服务的ASP.NET Web API 2项目
    • An ASP.NET Web MVC5 project for a documentation and admin screens 用于文档和管理屏幕的ASP.NET Web MVC5项目
  • Application Layer 应用层
    • A class library project responsible to take commands from presentation layer and consume any domain services 一个类库项目,负责从表示层获取命令并使用任何域服务
  • Domain Layer 域层
    • A class library project that contains business models and logic 包含业务模型和逻辑的类库项目
    • A class library project that contains the domain services 包含域服务的类库项目
  • Infrastructure Layer 基础设施层
    • A class library project that contains all the concrete implementation, like dataq persistance using Entity Framework, logging using Log4net, IoC using Simple Injector, etc 一个包含所有具体实现的类库项目,例如使用Entity Framework进行dataq持久性,使用Log4net进行日志记录,使用Simple Injector进行IoC等

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. 我们之所以没有直接引用数据访问技术的直接实现(即,使用dbContextLinq具体实现)的原因是,我们可以轻松地将地下访问技术转换为其他东西。

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.

相关问题 如何将数据从表示层传递到业​​务逻辑层? (ASP.NET MVC 5) - How do I pass data from presentation layer to business logic layer? (ASP.NET MVC 5) 在ASP.NET MVC中使用实体框架进行数据访问 - Data access using Entity Framework in ASP.NET MVC 具有实体层和抽象层的ASP.NET MVC - ASP.NET MVC with Entity Framework with Abstraction Layer 如何在ASP.NET MVC 5 /实体框架中绑定数据? - How to bind data in ASP.NET MVC 5 / Entity Framework? 使用 ASP.NET Core 实体框架在数据层中进行数据加密 - Data Encryption in Data Layer with ASP.NET Core Entity Framework 为什么大多数ASP.NET MVC示例都直接在表示层中访问数据库? - Why do most ASP.NET MVC examples access the database directly in the presentation layer? Controller中的ASP.NET MVC实体框架DB访问 - ASP.NET MVC Entity Framework DB access in Controller 如何使用ASP.NET MVC将ModelStateDictionary中的数据手动绑定到表示模型? - How can I manually bind data from a ModelStateDictionary to a presentation model with ASP.NET MVC? ASP.NET MVC3和Unity-将Unity实现与表示层分离? - ASP.NET MVC3 and Unity - Decouple Unity implementation from presentation layer? ASP.NET MVC:如何让我的业务规则验证冒泡到表示层? - ASP.NET MVC: How can I get my business rule validation to bubble up to the presentation layer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM