<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="39003" data-post-id="39003">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="karmajunkie" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/karmajunkie/120/1076_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  karmajunkie
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="smithaitufe" data-post="71" data-topic="4367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/49beb7/48.png" class="avatar"> smithaitufe:</div>
<blockquote>
<p>What if a model in a context say A requires certain data available in another model in a different context B?</p>
</blockquote>
</aside>
<p>Your first mistake here is in using the database as an integration layer between different contexts—all you’re doing is coupling together two things which are explicitly meant to be decoupled. If you’re going to use contexts as a service boundary, then they should be independent. This means that context B doesn’t borrow the data of context A—context A broadcasts to the world what it thinks the world ought to know, and context B maintains its own store of data based on what context A has to say. If these contexts are actually decoupled there probably isn’t much that B is interested in from A.</p>
<aside class="quote no-group" data-username="smithaitufe" data-post="71" data-topic="4367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/49beb7/48.png" class="avatar"> smithaitufe:</div>
<blockquote>
<p>How do you ensure data integrity, such that changes made to a model A in context A is propagated to model A in context B?</p>
</blockquote>
</aside>
<p>That’s the job of context B, listening to messages broadcast from context A.</p>
<p>If you’re dead-set on coupling the data in the way you describe, don’t create artificial boundaries where you’re not enforcing a systemic boundary. Just use context A’s schemas in context B. You <em>can</em> have a separate schema that refers to the same underlying database table from a different context; I have proffered this option in the past, but to be honest I don’t think its a great idea—just a way of fitting a round peg in a square hole.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="39003" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-determine-contexts-with-phoenix-1-3/4367/73">Post #72</a>
	                </div>
	            </div>
              <div id="likers-container-39003" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="39003"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #72"></div>
  </section>
</div>
    <div class="postbit" id="39004" data-post-id="39004">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="karmajunkie" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/karmajunkie/120/1076_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  karmajunkie
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="smithaitufe" data-post="70" data-topic="4367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/49beb7/48.png" class="avatar"> smithaitufe:</div>
<blockquote>
<p>For me, the truth is using phx is more difficult than Phoenix.</p>
<p>I have not been able to grasp things like handling relationship across context.</p>
</blockquote>
</aside>
<p>Putting together a good example application that applies this style of architecture and design is not really trivial, and trivial examples just create more questions when you invariably get away from the happy path.</p>
<p>What I would recommend for you is to ignore the notion of contexts for now. Elixir doesn’t actually care about them, and Phoenix really doesn’t either outside of the generators. Learn all the other aspects of Elixir, OTP, and Phoenix first—at some point, the context stuff will just come naturally. Its also good to do some reading around analysis patterns—there are many ways of approaching architecture, and DDD isn’t the best one in every situation.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="39004" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-determine-contexts-with-phoenix-1-3/4367/74">Post #73</a>
	                </div>
	            </div>
              <div id="likers-container-39004" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="39004"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #73"></div>
  </section>
</div>
    <div class="postbit" id="39048" data-post-id="39048">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="peerreynders" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/120/5826_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  peerreynders
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="smithaitufe" data-post="71" data-topic="4367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/49beb7/48.png" class="avatar"> smithaitufe:</div>
<blockquote>
<p>What if a model in a context say A requires certain data available in another model in a different context B?</p>
</blockquote>
</aside>
<p><a href="http://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html" rel="noopener nofollow ugc">Why getter and setter methods are evil (Allen Holub, 2003)</a></p>
<blockquote>
<p>Don’t ask for the information you need to do the work; ask the object that has the information <strong>to do the work for you</strong>.</p>
</blockquote>
<p>Replace “object” in the above statement with “context” and it’s still accurate. These Days Holub phrases this as “Ask for help, not information”.</p>
<p>A little bit earlier Andy Hunt and Dave Thomas talk about the “Tell don’t Ask” principle in:<br>
<a href="https://media.pragprog.com/articles/jan_03_enbug.pdf" rel="noopener nofollow ugc">The Art of Enbugging (pdf, IEEE Software January/February 2003)</a> and<br>
<a href="https://pragprog.com/articles/tell-dont-ask" rel="noopener nofollow ugc">Tell, Don’t Ask</a></p>
<p>Martin Fowler summarizes his take in <a href="https://martinfowler.com/bliki/TellDontAsk.html" rel="noopener nofollow ugc">TellDontAsk</a></p>
<p>“Tell Don’t Ask” is a consequence of Responsibility-Driven Design.</p>
<p><a href="http://practicingruby.com/articles/responsibility-centric-vs-data-centric-design" rel="noopener nofollow ugc">Practicing Ruby: Responsibility-centric vs. data-centric design (2012)</a></p>
<blockquote>
<ol>
<li>Objects tend to be highly cohesive around their behavior, because roles are defined by behavior, not data.</li>
</ol>
</blockquote>
<p>In “context-speak”:</p>
<blockquote>
<p>Contexts tend to be highly cohesive around their capabilities.</p>
</blockquote>
<p>Essentially, if your thinking is <em>even somewhat</em> rooted in the CRUD world your are going to have a hard time isolating contexts.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="39048" data-batch-url="/posts/batch_likers">
                        5
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-determine-contexts-with-phoenix-1-3/4367/75">Post #74</a>
	                </div>
	            </div>
              <div id="likers-container-39048" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="39048"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #74"></div>
  </section>
</div>
    <div class="postbit" id="39129" data-post-id="39129">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="cmkarlsson" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cmkarlsson/120/23000_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  cmkarlsson
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>Essentially, if your thinking is even somewhat rooted in the CRUD world your<br>
are going to have a hard time isolating contexts.</p>
</blockquote>
<p>Spot on! I’ve noticed this design “problem” ever since the first MVC web<br>
frameworks popped up. They are all quite centered around the database<br>
table (the model) and generators and documentation show how to have 1<br>
“Context”/“Api”/“Boundary” per table and then use them directly from<br>
your code.</p>
<p>This goes through in everything and even form generators are done from<br>
the model.  Following this forces you into a design that is not good for<br>
you and hard to get out of. It is also hard to change how you reason<br>
about it.</p>
<p>Generally determining contexts is hard unless you know your domain well,<br>
but you should not base it on your database tables, rather think what<br>
your software is trying to solve and what service it provides.</p>
<p>In terms erlang everything is based around modules (which is true for<br>
elixir as well). In erlang the “best practice” is something like this:</p>
<p>A module should be coherent and loosely coupled. A module should have a<br>
small, well defined and documented API.</p>
<p>An OTP application is a set of modules. The OTP application should have<br>
1 module which is exposed to the “outside”. It should have a small,<br>
well defined and documented API. No implementation details should leak<br>
outside of the application. That is, if you return a data-structure that<br>
data-structure should be opaque and only modifiable by using provided<br>
functions.</p>
<p>Modules within an OTP application can depend on each other, they are an<br>
implementation detail.</p>
<p>I see contexts as something similar. A context should have a small, well<br>
defined API and no implementation details shouild leak outside of the<br>
context.</p>
<p>If you have a small, API you should easily be able to swap out the<br>
implementation of a module/context/OTP application without affecting any<br>
other code.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="39129" data-batch-url="/posts/batch_likers">
                        7
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-determine-contexts-with-phoenix-1-3/4367/76">Post #75</a>
	                </div>
	            </div>
              <div id="likers-container-39129" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="39129"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #75"></div>
  </section>
</div>
    <div class="postbit" id="88710" data-post-id="88710">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ostinelli" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ostinelli/120/25204_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ostinelli
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="idi527" data-post="63" data-topic="4367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/48.png" class="avatar"> idi527:</div>
<blockquote>
<p>Because it seems it can be somewhat of a bottleneck [1] if every message is sent with <code>send</code> .</p>
<p>[1] <a href="http://www.ostinelli.net/boost-message-passing-between-erlang-nodes/" rel="noopener nofollow ugc">http://www.ostinelli.net/boost-message-passing-between-erlang-nodes/</a></p>
</blockquote>
</aside>
<p>This is an old article (2009) that relates to a very specific use case. AFAIK this does not even apply anymore.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="88710" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-determine-contexts-with-phoenix-1-3/4367/77">Post #76</a>
	                </div>
	            </div>
              <div id="likers-container-88710" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="88710"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #76"></div>
  </section>
</div>
    <div class="postbit" id="88711" data-post-id="88711">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="idi527" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  idi527
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>AFAIK this does not even apply anymore.</p>
</blockquote>
<p>So the messages are batched by default when sent to other nodes? I thought not, since there are some projects like <a href="https://github.com/discordapp/manifold" class="inline-onebox" rel="noopener nofollow ugc">GitHub - discord/manifold: Fast batch message passing between nodes for Erlang/Elixir. · GitHub</a> which explore approaches to do that similar to what you’ve described in your article …</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="88711" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-determine-contexts-with-phoenix-1-3/4367/78">Post #77</a>
	                </div>
	            </div>
              <div id="likers-container-88711" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="88711"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>