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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="sasajuric" data-post="21" data-topic="34612">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/48/991_2.png" class="avatar"> sasajuric:</div>
<blockquote>
<p>What’s wrong with calling one context from another?</p>
</blockquote>
</aside>
<p>As an example the <code>People</code> context (contains <code>User</code>) has many other contexts depending upon it. I’m trying to avoid that context calling out to, or even knowing about, other contexts which depend upon it. There are schemas in other contexts that have a <code>belongs_to</code> association to <code>User</code>, but the <code>User</code> schema does not have a <code>has_many</code> to these resources.</p>
<p>Suppose I am archiving a user. There can be resources from various contexts that need to be removed or disabled and other shutdown operations, reporting, etc. This means calling out to contexts like <code>Intercoms</code>, <code>Printers</code>, <code>Networking</code>, etc. Currently that is happening in a module dedicated to that task, <code>ArchiveUser</code>. It mostly consists of a <code>call</code> function that contains a <code>Multi</code> that walks through the steps. These “cross-context” modules also sometimes have their own dedicated embedded schemas and changesets for custom workflow forms.</p>
<p>In some ways, these are just another context that happens to reach across multiple contexts, but they are smaller and typically only focused on one main operation so I dumped them into that “/services” directory. It was also just familiar to turn these into verb “use cases / services” because I did that quite a bit back in my Rails days.</p>
<p>Avoiding calling child contexts from a parent context is the only scenario where I’ve tried to be vigilant. Other than that, I’ve generally tried to limit the amount of dependencies between smaller contexts out on branches of the dependency tree. If I find that two otherwise independent contexts seem to need to be dependent upon each other, I take that as a sign that maybe they belong in the same context. This just happened recently where I merged the <code>CRM</code> and <code>Leasing</code> contexts into one.</p>
<p>I am also interested in trying your Boundary library to further enforce the dependency tree amongst contexts, but wonder if that would be a misuse of the intent of the library.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="190645" data-batch-url="/posts/batch_likers">
                        2
                      </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/project-structure-and-layering/34612/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-190645" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="190645"
                     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 #21"></div>
  </section>
</div>
    <div class="postbit" id="190661" data-post-id="190661">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sasajuric" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/120/991_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sasajuric
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Elixir In Action</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="baldwindavid" data-post="22" data-topic="34612">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/baldwindavid/48/17559_2.png" class="avatar"> baldwindavid:</div>
<blockquote>
<p>As an example the <code>People</code> context (contains <code>User</code> ) has many other contexts depending upon it. I’m trying to avoid that context calling out to, or even knowing about, other contexts which depend upon it.</p>
</blockquote>
</aside>
<p>Oh I see. Yeah that’s fine. I asked b/c I occasionally see people advising against calling one context from another, which is for me too dogmatic. But if I get this right, you otherwise call contexts from other contexts, but here you want to avoid the cycle, which is a noteworthy goal <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<aside class="quote no-group" data-username="baldwindavid" data-post="22" data-topic="34612">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/baldwindavid/48/17559_2.png" class="avatar"> baldwindavid:</div>
<blockquote>
<p><code>ArchiveUser</code> . It mostly consists of a <code>call</code> function</p>
</blockquote>
</aside>
<p>This IMO presents a smell. Occasionally I might create a module having a verb name, and a single public function named <code>call</code> or <code>execute</code>, but I only when I want to extract some large chunk of code related to a single operation into a separate, internal module.</p>
<p>In this case, I’d put this function into the root context. You mentioned you want to avoid calling children from their parent, which is funny b/c I avoid the opposite. I feel that children are further subdivision of the parent’s scope, and so having parent calling them is IMO fine (e.g. boundary implicitly allows this), while the opposite indicates something suspicious in the subdivision.</p>
<p>Either way, if you want to avoid that, consider adding a context named <code>Admin</code>, or something like that.</p>
<p>In general, it seems that your problem is that of naming. Perhaps you have too few cases to find a meaningful abstraction for such complex operations, so stashing them in some common bucket (either root context or some vaguely named context (Admin, Management, etc)). This is a technique I often use when I can’t find the proper name. I stash things inside some common bucket until I gather enough material to recognize emerging abstractions.</p>
<p>To summarize, extracting operations which need to involve multiple boundaries (some of which already depend on others) to avoid cycles is IMO fine. One context per operation, with a generic <code>call</code> function is not something I’d do though <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<blockquote>
<p>I am also interested in trying your Boundary library to further enforce the dependency tree amongst contexts, but wonder if that would be a misuse of the intent of the library.</p>
</blockquote>
<p>Nope, this is what it’s intended for <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20">  It even support nesting boundaries. You may want to wait until I release the next version (should be out in a few days), b/c there will be some breaking changes.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="190661" data-batch-url="/posts/batch_likers">
                        2
                      </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/project-structure-and-layering/34612/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-190661" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="190661"
                     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 #22"></div>
  </section>
</div>
    <div class="postbit" id="190663" data-post-id="190663">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks for the guidance. Both you and <a class="mention" href="/u/pedromtavares" rel="nofollow">@pedromtavares</a> have suggested placing these sort of homeless functions onto the root context which, unless I’m misunderstanding, would effectively be <code>MyApp</code> (not the real name, but you get it :)). I don’t actually have any qualms with this from a dependency perspective as I already delegate to those functions from this root module, but moving that code out of the services directory will allow the file structure to better match the code and push me to consider potential new contexts/groupings rather than turning into a junk drawer. When I mentioned “child” I just meant in terms of that dependency tree between contexts like <code>People</code>, <code>Intercoms</code>, <code>Printing</code>, etc.</p>
<aside class="quote no-group" data-username="sasajuric" data-post="23" data-topic="34612">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/48/991_2.png" class="avatar"> sasajuric:</div>
<blockquote>
<p>In general, it seems that your problem is that of naming. Perhaps you have too few cases to find a meaningful abstraction for such complex operations, so stashing them in some common bucket (either root context or some vaguely named context (Admin, Management, etc)).</p>
</blockquote>
</aside>
<p>I think you’re right there. I don’t have many of these yet, but can already see some potential groupings between some of them. Thanks!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="190663" data-batch-url="/posts/batch_likers">
                        4
                      </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/project-structure-and-layering/34612/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-190663" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="190663"
                     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>