简体   繁体   English

BLL 层或单独层中的数据传输对象?

[英]Data Transfer Objects in BLL layer or a separate layer?

I have:我有:

  • DAL layer -> entities and DbContext (Entity Framework) DAL 层-> 实体和 DbContext(实体框架)
  • BLL layer -> services BLL 层-> 服务
  • UI layer -> Razor Pages UI层-> Razor页面

My services in BLL layer converts entities to data transfer objects and returns data transfer objects to UI layer.我在 BLL 层的服务将实体转换为数据传输对象,并将数据传输对象返回给 UI 层。 The UI layer also passes data transfer objects to BLL service. UI 层还将数据传输对象传递给 BLL 服务。

Should I place data transfer objects in BLL layer or create a separate layer?我应该将数据传输对象放在 BLL 层还是创建一个单独的层?

Each layer should have its own Models/ViewModels.每一层都应该有自己的模型/视图模型。

The Persistence layer has EF Entities.持久层具有 EF 实体。 Those should not surface to the upper layers.那些不应该出现在上层。 Otherwise, you'll be forever tied to EF.否则,您将永远与 EF 绑定。

The BL contains your Domain Models. BL 包含您的领域模型。 The Persistence layer will take care of the mapping Entity->Domain Model (and vice-versa).持久层将负责映射实体->域 Model(反之亦然)。

The UI has its own ViewModels and takes care of the mapping. UI 有自己的 ViewModel 并负责映射。

Now, with that said, you could manage to use Domain Model classes as EF Entities, achieving what is called "persistence ignorance".现在,话虽如此,您可以设法使用域 Model 类作为 EF 实体,实现所谓的“持久性无知”。 This usually requires some "magic", and is normally done by avoiding any kind of library-specific data annotation attribute on the properties (like [Table], [ForeignKey] and so on).这通常需要一些“魔法”,通常通过避免属性上的任何类型的库特定数据注释属性来完成(如 [Table]、[ForeignKey] 等)。 The db mapping is handled via IEntityTypeConfiguration . db 映射通过IEntityTypeConfiguration处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM