ef context + database schema + DDD BoundedContext

2019-09-18 17:50发布

问题:

I understand that at least EF 6 supports multiple DbContexts. This can be used to model BoundedContext. I did some google searches but could not find a definitive answer to this question. Is it advisable to use different db schemas for different DbContexts/BoundedContext? I know that ORMs abstract away the persistence mechanisms but I personally can see parallels between shemas and ddd/ef contexts.

回答1:

It is a possibility. As with most architectural questions, the answer is: it depends.

In this case, it depends on how your overall architecture is and how your bounded contexts are structured. If they have similar aggregates that are persisted to the same tables (that is, they're different because of the context), it might be a good idea to have different DbContexts because then you can evolve them separately.

Note though that you may be introducing hidden constraints and dependencies between your bounded contexts.

If your bounded contexts have very different aggregates, then there's no need to use different DbContexts and you can just share the same one.

Another option you might consider, is using a different DbContext for reading and writing. It also allows you to evolve your model separately. (that's more of a CQRS approach though)