简体   繁体   English

通过代码优先迁移在不同层中复制POCO代码

[英]Duplicating POCO code in different layers with code-first migrations

I'm interested in using entity framework - code first migrations with a new database, and I have some questions/concerns about duplicating POCO code in the data and business layers. 我对使用实体框架感兴趣-使用新数据库进行代码优先迁移,并且我对在数据和业务层中复制POCO代码有一些疑问/问题。

The idea is to have a data access layer containing my POCO entities all decorated with database schema type items like string lengths, foreign key related things that don't work out of the box, and whatever else. 这个想法是要有一个包含我的POCO实体的数据访问层,所有这些实体都装饰有数据库模式类型的项目,例如字符串长度,与外键相关的东西,这些东西开箱即用,等等。 This layer will also act as a repository that will return scalar values, entities, aggregates, and IEnumerables. 该层还将充当存储库,该存储库将返回标量值,实体,聚合和IEnumerable。

Above that will be the business layer, which will handle talking to the repository, and a bunch of business logic. 在其之上的是业务层,它将处理与存储库的对话,以及一堆业务逻辑。

At the top is the presentation layer. 最顶层是表示层。 This layer talks to the business layer, and has no idea about the data layer – all it understands is view models. 该层与业务层进行通信,并且对数据层一无所知–它只了解视图模型。 I would implement the MVC pattern at this layer, using only view models. 我将仅使用视图模型在这一层上实现MVC模式。

The issue I am running into is related to where I should do the mapping between my view models and my data models. 我遇到的问题与我在视图模型和数据模型之间的映射位置有关。 If I define the view models in my presentation layer, the business layer will not know they exist. 如果我在表示层中定义视图模型,则业务层将不知道它们的存在。

  • Am I better off defining the view models in the business layer, or do I need a set of domain models in the business layer that the presentation layer would be aware of and be able to perform mapping against? 我最好在业务层中定义视图模型,还是在业务层中需要表示层可以识别并能够执行映射的一组域模型?
    • Would an additional set of domain models just be an egregious duplication of the models defined in the data layer? 一组额外的领域模型是否只是数据层中定义的模型的惊人复制?

Depending on the scope of the project, I usually use one of the two methods below: 根据项目的范围,我通常使用以下两种方法之一:

1) have a set of domain models separately from all layers, and then have all layers reference them. 1)具有一组与所有层分开的域模型,然后让所有层引用它们。 Pros: layers are still independent, no model duplication, no need to map layer models. 优点:图层仍然是独立的,没有模型重复,不需要映射图层模型。 Cons: change in domain model would affect all layers, eg DB change may break display 缺点:域模型的更改会影响所有层,例如,数据库更改可能会破坏显示

2) have models in each layer. 2)每层都有模型。 Business layer would map its own models to those of data layer, presentation layer would map its models to those of business layer Pros: every layer does one thing, changes in underlying models to not propagate through layer Cons: duplication of the models is likely and mapping needs to be maintained 业务层将其自身的模型映射到数据层的模型,表示层将其模型映射到业务层的模型优点:每一层都做一件事,基础模型中的更改不传播到层上缺点:可能重复模型并且映射需要维护

First approach works best for smaller apps and "direct updates" scenarios, second works better in large projects with multiple moving components. 第一种方法最适合较小的应用程序和“直接更新”方案,第二种方法在具有多个移动组件的大型项目中效果更好。

PS "defining the view models in the business layer" is a definite no-no. PS“在业务层中定义视图模型”绝对是不行的。

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

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