聚合,事务一致性和实体框架DbContext [英] Aggregates, Transactional Consistency and the Entity Framework DbContext

查看:148
本文介绍了聚合,事务一致性和实体框架DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

汇总必须设计为具有事务性,并最终保持一致性。围绕实体的一致性边界有助于管理复杂性。

Aggregates must be designed to be transactionally and eventually consistency. This consistency boundary around entities helps manage complexity.

在我们的存储库实现中,我们使用Entity Framework与实际数据库进行接口。从历史上看,我们一直都有巨大的上下文(表格的数十个分数),它们代表数据库(或至少在数据库的某些功能区域)中每个可用的表格,字段和关系。这里的问题是,此上下文用于数百种不同的事物,并且随着系统的变大而呈指数增长,从而导致很难维护的事物。

In our repository implementations, we are using Entity Framework to interface with the actual database. Historically we have always had huge contexts (spanning scores of tables) which represent every available table, field and relationship in the database (or at least in some functional area of the database). The problem here is that this context is used for hundreds of different things and grows exponentially as the system gets bigger, leading to something which is very difficult to maintain.

由于这一点,通常建议为每个对象创建单独的DbContext系统中的有限上下文。朱莉·勒曼(Julie Lerman)在她的文章具有DDD有界上下文的收缩EF模型中提出了这一点。 。

Due to this, it is often suggested that separate DbContexts should be created for each bounded context in the system. Julie Lerman proposed this in her article, Shrink EF Models with DDD Bounded Contexts.

如果我们的总计在交易中保持一致,是什么使我们无法进一步前进并创建专用的上下文来服务每个聚合存储库?

If our aggregates are transactionally consistent, what is stopping us from going one step further and creating dedicated contexts to serve each aggregate repository?

与其混杂(满足每个人的需求),不如给上下文 明确意图 em>

Instead of being promiscuous (serving everyones' needs), it would give the context clear intention.


  • 仅当需要更改汇总时,上下文才需要更改。它随着聚合而发展。在更大的上下文中,系统的许多部分可能取决于上下文的一部分。单个更改可能会危害很多。

  • The context will only ever need to change when the aggregate needs to change. It evolves with the aggregate. With larger contexts, many parts of the system could depend on one part of the context. A single change could jeopardise a lot.

仅聚合所需的表,字段和关系将需要在上下文中存在。通常,在处理更大的上下文时,您不必担心给定表上的大多数关系或字段。

Only the tables, fields and relationships needed by the aggregate will need to exist in the context. Often when dealing with a larger context, you aren't bothered with most of the relations or fields on a given table.

这种方法有缺点。即:


  • 尽管它们的建模方式可能有所不同(取决于使用情况),但某些数据库表和关系可能需要存在在多种情况下。

  • Although they would likely be modeled differently (depending on their use), certain database tables and relationships may need to exist in multiple contexts.

如果使用,则代码优先迁移将非常棘手。

If used, code-first migrations would be tricky.

这可能是

有人可以对此方法提供进一步的见解吗?也许我忽略了什么?

Can anyone provide further any insight on this approach? Is there perhaps something which I have overlooked?

编辑:

请注意,我们不在以下位置使用EF数据实体我们的域名。我们的存储库从这些数据实体实例化并合并了一个更丰富的域模型。

Note that we are not using the EF data entities in our domain. Our repositories instantiate and hydrate from these data entities a richer domain model.

推荐答案

我不认为多重聚合上下文是一种问题,尤其是如果您遵循严格的聚合分隔-没有引用聚合以外的实体,则仅按键进行根到根的松散引用。

I don't see multiple-aggregate Contexts as a problem, especially if you follow strict aggregate separation -- no reference to entities outside aggregate, only root-to-root loose references by key.

如果确定可以肯定,这就是为什么您会想要原子DbContexts的性能瓶颈。

On the other hand, I could see why you would want atomic DbContexts if you know for sure it's a performance bottleneck.

不过,一件事情是:EF上下文不必精确地映射到有界层上下文。如果这样做,并且您尝试在两侧尽可能地缩小上下文,则可能会对域层IMO造成损害。 BC领域可能会失去其连贯性,并且在此过程中可能会丢失重要的普遍存在的语言概念和细分的语义。

One thing though : EF contexts don't have to map exactly to Domain layer Bounded Contexts. If they do and you try to shrink your contexts as much as possible on both sides, it could cause damage in the Domain layer IMO. The domain BC's could lose their coherence and the semantics of important ubiquitous language notions and subdivisions could be lost in the process.

这篇关于聚合,事务一致性和实体框架DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆