使用实体框架时是否应该使用部分类作为业务层?
我正在开发一个使用实体框架的项目。使用EF生成的类的部分类作为业务层可以吗?我开始认为这就是 EF 的用途。
我尝试使用 DTO 模式,很快意识到我只是创建了一堆映射类,这重复了我的工作,而且还导致更多的维护工作和额外的层。
我想使用自我跟踪实体并将 EF 实体传递到所有层。请分享您的想法和想法。谢谢
I am working on a project using entity framework. Is it okay to use partial classes of the EF generated classes as the business layer. I am begining to think that this is how EF is intended to be used.
I have attempted to use a DTO pattern and soon realized that i am just creating a bunch of mapping classes that is duplicating my effort and also a cause for more maintenance work and an additional layer.
I want to use self-tracking-entities and pass the EF entities to all the layers. Please share your thoughts and ideas. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我研究了使用部分类,发现向 UI 层公开数据库模型会受到限制。
出于以下几个原因:
如果您进行 TDD 并遵循 YAGNI,那么您将倾向于拥有专门为您正在编写的操作设计的结构,这将更容易构建测试(不需要创建与测试无关的其他对象,因为它们都在模型上)。在这种情况下,您可能...
而不是使用由 EF 生成的具有公开引用的实体模型...
这样,只有接受订单的操作才需要客户的 id。为什么您需要为与接受订单相关的操作构造一个
Customer
(以及可能的其他对象)?如果您担心重复和映射,请查看 Automapper
I had a look at using partial classes and found that exposing the database model up towards the UI layer would be restrictive.
For a few reasons:
If you do TDD and follow YAGNI then you will tend to have a structure specifically designed for the operation you are writing, which would be easier to construct tests against (not requiring to create other objects not realated to the test just because they are on the model). In this case you might have...
Instead of using the Entity model generated by the EF which have references exposed...
This way the id of the customer is only needed for an operation that takes an order. Why would you need to construct a
Customer
(and potentially other objects as well) for an operation that is concerned with taking orders?If you are worried about the duplication and mapping, then have a look at Automapper
我不会这样做,原因如下:
但是,如果您有一些特定于数据模型的代码,请将其放置为分部类以避免在重新生成模型时丢失。
I would not do that, for the following reasons:
However, if you have some data model specific code, place that is a partial class to avoid it being lost when you regenerate the model.
我认为部分课程将是一个好主意。如果重新生成模型,那么您将不会丢失部分类中的业务逻辑。
作为替代方案,您也可以仅查看 EF4 代码,这样您就不需要从数据库生成模型。
I think partial class will be a good idea. If the model is regenerated then you will not loose the business logic in the partial classes.
As an alternative you can also look into EF4 Code only so that you don't need to generate your model from the database.
我会使用部分类。 DDD 风格的代码中不存在数据层这样的东西。有一个数据层,它驻留在 SQL Server 上。应用程序代码应该只包含业务层和一些允许在上述数据层中持久保存业务对象的映射。
实体框架是您的数据访问代码,因此您不应该构建自己的代码。在大多数情况下,数据库模式将被修改,因为模型已更改,而不是相反。
话虽如此,我不鼓励您在所有层中共享实体。我重视 UI 层和领域层的分离。我会使用 DTO 将数据传入和传出域。如果我有必要的自由,我什至会使用 CQRS 模式来摆脱将实体映射到 DTO —— 我会简单地创建第二个 EF 数据访问项目,仅用于读取 UI 数据。它将构建在同一个数据库之上。您通过读取(贫血——没有业务逻辑)模型读取数据,但可以通过发出针对使用 EF 和部分方法实现的真实模型执行的命令来修改数据。
这能回答你的问题吗?
I would use partial classes. There is no such thing as data layer in DDD-ish code. There is a data tier and it resides on SQL Server. The application code should only contain business layer and some mappings which allow persisting business objects in the mentioned data tier.
Entity Framework is you data access code so you shouldn't built your own. In most cases the database schema would be modified because the model have changed, not the opposite.
That being said, I would discourage you to share your entities in all the layers. I value separation of UI and domain layer. I would use DTO to transfer data in and out of the domain. If I have the necessary freedom, I would even use CQRS pattern to get rid of mapping entities to DTO -- I would simply create a second EF data access project meant only for reading data for the UI. It would be built on top of the same database. You read data through read (anemic -- without business logic) model, but you modify it by issuing commands that are executed against real model implemented using EF and partial methods.
Does this answer your question?
我不会那样做。也尽量保持各层独立。因此,数据库模式的微小变化不会影响所有层。
实体可以用于数据层,但不应该。
如果有的话,提供要使用的接口并让您的实体实现它们(在部分文件上),BL 不应该知道实体,而应该知道接口。
I wouldn't do that. Try too keep the layers independent as possible. So a tiny change in your database schema will not affect all your layers.
Entities can be used for data layer but they should not.
If at all, provide interfaces to be used and let your entities implement them (on the partial file) the BL should not know the entities but the interfaces.