是EF对象业务对象或没有?对象、业务、EF

2023-09-04 00:58:35 作者:挽棠

可能重复:   Using实体框架实体业​​务对象?

我在找到使用实体框架作为一个ORM,但做大量的阅读后,我真的很困惑,在那里EF对象完全吻合。

我下的是IM pression,一个ORM的整点是要删除你的对象映射到关系数据库的苦差事及并发症。您加载数据库,并得到一堆似幻的对象。 CRUD的东西都被做了反对的对象。如果DB变化,你改变映射,但你的对象保持不变。

这将意味着你要在整个系统中使用这些对象,他们将有行为。他们是业务对象。这对我来说很有意义,也确实MS 告诉您如何添加行为

不过,我一直在读很多人说,你不应该使用EF对象为你的业务对象,因为他们关注的是数据的唯一持久性,并与英法会几个业务对象。他们建议使用EF作为数据层的抽象,并将它们映射到实际的业务对象。

这似乎是毫无意义的给我。为什么需要另一个抽象层,当我还是最终映射到我的业务对象?如果我有地图EF性质,我还不如地图数据库列!

我想到了一个ORM的整点是自动映射,并作为一个抽象的概念在后备存储?我失去了一些东西?

编辑:按行为我的意思是业务逻辑。验证,计算性能,经营方式等。

解决方案   华为数据之道 3 面向业务的信息架构建设

如果数据库改变你改变映射,但你的对象保持不变。

没有。分贝的变化也将改变映射实体类。但是,这并不意味着这些类不能用于商业逻辑。如果使用EF开箱,将创建实体类的部分类,所以你被邀请加入自己的code的一种方式,它不会被覆盖所产生的code。

  

人们说(...)[EF对象]关注的是数据的持久化仅

我不这么认为。他们可能是配的坚持和从数据存储填充(因为它们是工作数据库,首当),但是这不是他们关注的问题。我们应该忽略这种附加的行为的不使用它的商业逻辑的(是我的看法)。对象不是持久性昧,业务逻辑应该的。

尽管如此,我使用的实体类的业务逻辑所有的时间。但我不是一个球迷纯面向对象更多的,在密切合作与精心分离的责任对象感。有两个原因:

大多数应用程序都涉及到一个视图层视图模型某种业务层,并通过控制器层粘在一起,这就需要序列化的边界进行通信。所以调用域类的行为直接往往不是一个选项。因此,我倾向于越来越多的BL程序,通过DTO的,少在那直接从其它层寻址领域模型通信服务类或墙面。这些服务方法往往是完美地实现业务逻辑。所以大部分的时间,我向EF教学班,逻辑,涉及到类本身内的数据,并不需要与其他EF类的任何合作。这也prevents延迟加载异常和N + 1的问题。我可以告诉很多例子,我高兴地从域逻辑转移到BL的服务方法,但是这是一个有点超出了这个问题的范围。

同样,我没有使用的粉丝OnPropertyChanging /变更作为MS环节之一描述。我已经做到了,这是意大利面条。它可以在视图模型,例如用响应用户输入,但在域类设置一个属性应该是:设置一个属性。无添加行为得到忘记,有一天咬你。

A(无状态的)函数式编程范式,这是鼓舞LINQ和其他.NET语言结构的进步。

所以,我看到我的域模型靠拢贫血的域模型,但从来没有真正到达那里。我试着给一些原因,这可能是非常明智的现在。

Possible Duplicate: Using Entity Framework entities as business objects?

I'm looking into using Entity Framework as an ORM, but after doing lots of reading I'm really confused as to where EF objects fit exactly.

I was under the impression that the whole point of an ORM was to remove the drudgery and complication of mapping your object to a relational database. You load your DB and get a bunch of objects like magic. CRUD stuff all gets done against objects. If the DB changes you change the mapping but your objects stay the same.

This would imply that you are going to use these objects throughout the system, and that they will have behaviour. They are business objects. This makes sense to me, and indeed MS tells you how to add behaviour

However, I've been reading lots of people saying that your should never use EF objects as your business objects as they are concerned with persistence of data only, and would couple your business objects with EF. They suggest using EF as an abstraction of the data layer, and mapping them to real business objects.

This seems pointless to me. Why do I need yet ANOTHER abstraction layer when I still end up mapping to my business objects? If I have to map EF properties, I might as well map DB columns!

I thought the whole point of an ORM was to automate the mapping and act as an abstraction over the backing store? Am I missing something?

EDIT: By behaviour I mean business logic. Validation, calculated properties, business methods etc.

解决方案

If the DB changes you change the mapping but your objects stay the same.

No. Db changes will also change the mapped entity classes. But that does not mean that these classes can't be used for business logic. If you use EF out of the box it will create entity classes as partial classes, so you are invited to add your own code in a way that it is not overwritten by generated code.

people saying that (...) [EF objects] are concerned with persistence of data only

I don't think so. They may be equipped to be persisted to and populated from a data store (as they are when working database-first), but it is not their concern. We should ignore this added behaviour and not use it in business logic (is my opinion). The objects are not persistence-ignorant, the business logic should be.

Having said this, I use entity classes for business logic all the time. But I'm not a fan of pure OO any more, in the sense of closely cooperating objects with meticulously separated responsibilities. For two reasons:

Most applications involve some sort of business layer and a view layer with view models, glued together by a controller layer and communicating over a boundary that requires serialization. So invoking behaviour of domain classes directly is often not an option. Therefore, I tend to program more and more BL in service classes or façades that communicate through DTO's and less in domain models that are addressed directly from other layers. These service methods tend to be perfect places for business logic. So most of the time, I extend EF classes with logic that involves data within the class itself and does not require any cooperation with other EF classes. This also prevents lazy loading exceptions and n+1 problems. I could show many examples where I happily moved from domain logic to BL in service methods, but that's a bit beyond the scope of this question.

Likewise, I'm not a fan of using OnPropertyChanging/Changed as one of the MS links describes. I've done it and it is spaghetti. It can be useful in view models, e.g. to respond to user input, but in domain classes setting a property should be that: setting a property. No added behaviour that gets forgotten and one day bites you.

The advance of a (stateless) functional programming paradigm, which was encouraged by Linq and other .Net language constructs.

So I see my domain models moving closer to anemic domain models, although never really getting there. I've tried to give some reasons why this can be perfectly sensible nowadays.

 
精彩推荐
图片推荐