amacgregor
On Phoenix Contexts, DDD and BDD
I had a couple of questions around contexts and best practices, or at the very least I’m trying to validate the my understanding of the subject is okay.
As I understand Contexts or Bounded contexts are nothing more than a code design practice that Phoenix 1.3 is introducing and laxly enforcing, the core concept makes sense and in principle it sounds like a good practice to set boundaries between the multiple components of an application.
What I’m finding is that the idea of contexts is fairly abstract and it only gets murkier when trying to put into practice, DDD in particular cares a lot (primarily) about the domain core and for someone that has no experience trying to define boundaries let alone optimal boundaries can be challenging, specially without experience.
Things can get even more challenging on situations where there might be overlap and the boundaries are not as clear or easily defined, for example a table or data needs to be accessed from multiple contexts (users would be a good example) I’ve seen a lot of mixed opinions on how to best tackle the problem.
As I see it right now Contexts just introduced a lot of confusion and uncertainty to developers, mostly because of the lack of experience and the introduction of particular way of designing/architecting code that most people will find strange.
The other thing that keeps jumping on the back of my mind is the idea of “Modelling by Example” introduced by Konstantin Kudryashov a while back ago in the php community, it tries to marry BDD+DDD and it does into what I think is a pretty elegant fashion, check it out here:
And the following talk on BDD:
https://skillsmatter.com/skillscasts/6240-taking-back-bdd
Sorry for the mini mental dump. Curious to hear what other people think.
Most Liked
OvermindDL1
Welcome to my hell if you have to do this. ^.^;
/me works with an ancient Oracle database that was apparently designed by intellect-less committee…
Avoid it if you can!
If you cannot though, look at Absinthe. I’ve been hiding the hell behind it’s GraphQL interface and I make GraphQL calls from in-server to in-server, it has become an excellent ‘Context’ in the system for data calls distributed across everything. ^.^
peerreynders
I fully understand where you are coming from … but at the same time we have to acknowledge that from a beginner’s perspective setting up boundaries to isolate contexts while at the same time violating these boundaries with SQL joins creates a bit of cognitive dissonance.
The only way to remove that is to emphasize:
- contexts serve only the domain
- while reports/analysis/intelligence are legitimate user features they typically aren’t considered part of the core domain
- therefore reports are a concern separate from the core domain (and the goals contained therein)
- therefore SQL joins for reporting purposes aren’t viewed as violating any boundaries**
- and ultimately those reporting SQL joins need to be considered a “shortcut” because that approach to reporting simply doesn’t scale. The perception of boundaries being violated is gone once reporting is moved to a separate database (not with a mirrored schema - but with a data warehousing schema).
**However there still is contention in the sense that whenever you change the context’s schema you also have to update your reporting query.
peerreynders
Actually there is a reason the OLTP vs OLAP dichotomy evolved. Your domain runs against a set of schemas on the online transactions processing systems side while your reporting, analysis, and intelligence runs on the online analysis processing side - typically reorganized in star schemas which are optimized towards the needs of data warehousing. So for reporting purposes data is duplicated from the OLTP system to the OLAP systems (Philip Greenspun: Data Warehousing for Cavemen (1997), Data Warehousing (2003)).
So it wouldn’t be too outlandish to suggest that for reporting purposes data should be “exported” to separate reporting schemas. In Phoenix when running multiple contexts against the same database one has the “luxury” of using a SQL join - but at the same time one has to be aware that one is engaging in an integration database.
When a context is promoted to a microservice this has to stop because an integration database violates service autonomy (a notion of “context autonomy” is probably appropriate as a context is all about cohesion). The typical solution to serve reporting needs is an Event Data Pump (Building Microservices p. 98) - while conducting it’s responsibilities each domain microservice emits events that are processed separately to be included in a reporting database - and that reporting database is used by the reporting service.
One interesting thing about contexts is that there are times where contexts legitimize violating DRY - but only when necessary in order to promote loose coupling because DRY can lead to tight coupling.
Development By Slogan with DRY (2015)
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








