<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="163963" data-post-id="163963">
  <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="tomekowal" data-post="10" data-topic="29145">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomekowal/48/2847_2.png" class="avatar"> tomekowal:</div>
<blockquote>
<p>I don’t claim that this is the right choice for everything <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"> Blog engine or any other CRUD is simpler when we pass the schemas everywhere directly.</p>
</blockquote>
</aside>
<p>Yes, I think we’re in full agreement. My current clients build these types of systems. I mean there’s always some more logic than just store data and display it, but nothing as involved as e.g. a banking system. We actually briefly deliberated about pure model data structures, but they didn’t seem worth it for us.</p>
<p>I absolutely agree that in more complex cases using the underlying storage view for domain model won’t be optimal. I mean, you can always hack around it, but this will bring needless complexity in the app code. So, yeah, in such cases having a pure domain model decoupled from internal db representation is definitely a good choice.</p>
<p>To be clear, this isn’t a shortcoming of Ecto, but rather it’s property. Ecto makes IMO a great choice to keep schemas very close to the underlying db representation, which makes things simpler in the beginning, and is sufficient for smaller domains. For anything else we need to do our own work, and I don’t think that any library/framework can help us here.</p>
<p>So I just wanted to point out that starting with the pure model IMO is an overkill for many projects, and also that such approach needlessly raises the bar for the people who are just starting to use the technology.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="163963" data-batch-url="/posts/batch_likers">
                        3
                      </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/any-downsides-to-using-the-same-table-for-multiple-contexts/29145/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-163963" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="163963"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="163965" data-post-id="163965">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="tomekowal" data-post="11" data-topic="29145" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomekowal/48/2847_2.png" class="avatar"> tomekowal:</div>
<blockquote>
<p>In the scheduling example, I believe you don’t need two contexts. If you want to schedule something for a user, do you need his name? Probably don’t. Unless you have business rules like “anyone named Andrew can’t book on Thursdays”. So you only need to identify <em>which</em> user does booking. You can pass only an id.</p>
<p>There might be the case where you really need the user in scheduling. Maybe there is a party only for adults. You can pass user.id and user.age to scheduling context but that starts to seem smelly. There might be more things you need later. In that case, you still don’t use two contexts. You duplicate the schema with only necessary fields in scheduling context (as in original question of this thread).</p>
<p>Since only one context should be responsible for writing data, <code>create</code> and <code>update</code> should almost always be one function call + handling errors.</p>
<p><code>index</code> and <code>show</code> can often read stuff from multiple contexts to display it nicely for the user. Showing user.name next to schedule sounds like a good idea <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"> So those would be two separate calls in the <code>show</code> action.</p>
</blockquote>
</aside>
<p>My users come to me with a token, not an id, so I do need to fetch the user. I guess I could use a plug here, but I’m not convinced. Also the authorization requiries querying across schemas, so I don’t see how I could stay in one context. Having a slimmed down duplicate of the schema sounds like an interesting idea to try out.</p>
<p>index and show is when things get murky. My controller asks for quite some data, so the dependencies are there - in the controller. I end up with an action that calls 5 functions. I think it crosses the line of being “thin”. Moving this out to a new context just makes it easier to test. But that doesn’t seem to add more coupling.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="163965" 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/any-downsides-to-using-the-same-table-for-multiple-contexts/29145/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-163965" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="163965"
                     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 #12"></div>
  </section>
</div>
    <div class="postbit" id="163968" data-post-id="163968">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tomekowal" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomekowal/120/2847_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tomekowal
                  </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="12" data-topic="29145">
<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>To be clear, this isn’t a shortcoming of Ecto, but rather it’s property. Ecto makes IMO a great choice to keep schemas very close to the underlying db representation, which makes things simpler in the beginning, and is sufficient for smaller domains.</p>
</blockquote>
</aside>
<p>Amen! Ecto is fantastic! It makes easy things easy and hard things still doable!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="163968" data-batch-url="/posts/batch_likers">
                        3
                      </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/any-downsides-to-using-the-same-table-for-multiple-contexts/29145/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-163968" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="163968"
                     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 #13"></div>
  </section>
</div>
    <div class="postbit" id="163977" data-post-id="163977">
  <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
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </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="8" data-topic="29145">
<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>I used ids here mostly b/c I assume the client is the web tier which doesn’t have the user struct</p>
</blockquote>
</aside>
<p>Understood. I usually have a struct because I have grabbed it to authorize.</p>
<aside class="quote no-group" data-username="stefanchrobot" data-post="9" data-topic="29145">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanchrobot/48/7657_2.png" class="avatar"> stefanchrobot:</div>
<blockquote>
<p>Let me chime in with a question: how thin are your controllers really?</p>
</blockquote>
</aside>
<p>They are generally pretty thin, I think. In a lot of admin areas they are authorized at router level, so it is a simple case statement.</p>
<p>Other areas have per action authorization which adds a <code>with</code>. Anything more intensive typically takes place in whatever context function is being called. Some of these functions within the context end up as big multis. In a few cases, a user is passed to the context function for authorization. That is typically to use a different changeset based upon user permissions.</p>
<aside class="quote no-group" data-username="stefanchrobot" data-post="9" data-topic="29145">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanchrobot/48/7657_2.png" class="avatar"> stefanchrobot:</div>
<blockquote>
<p>But as a result a new context emerged - a context that’s tailored for the specific controller. Since that controller is not tied to a single schema, but a use case, so is the context. Which means that it depends on other, simpler contexts (like <code>Accounts</code> , <code>Schedule</code> , etc.). What do you think about this approach? Do you happen to have similar things (more complex contexts; contexts dependant on other contexts) in your codebase?</p>
</blockquote>
</aside>
<p>I think as I refactor a bit, I will likely end up with some cross-context, um, contexts. The Phoenix docs talk about this a bit at the end… <a href="https://hexdocs.pm/phoenix/contexts.html#strategies-for-cross-context-workflows" class="inline-onebox" rel="noopener nofollow ugc">1. Intro to Contexts — Phoenix v1.8.8</a></p>
<aside class="quote no-group" data-username="tomekowal" data-post="10" data-topic="29145">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomekowal/48/2847_2.png" class="avatar"> tomekowal:</div>
<blockquote>
<p>I also try not to overcomplicate stuff for the sake of some future benefits because the future might never come. I focused on the ability to refactor DB structure because that bit me hard one time and because of the original question of <a class="mention" href="/u/baldwindavid" rel="nofollow">@baldwindavid</a> concerned schemas.</p>
</blockquote>
</aside>
<p>I appreciate the information and can experiment and learn even if I’m not going to use it right away. It is good to have that as a potential route when I encounter a situation where the defaults aren’t quite cutting it.</p>
<aside class="quote no-group" data-username="tomekowal" data-post="11" data-topic="29145">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomekowal/48/2847_2.png" class="avatar"> tomekowal:</div>
<blockquote>
<p><code>index</code> and <code>show</code> can often read stuff from multiple contexts to display it nicely for the user. Showing user.name next to schedule sounds like a good idea <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"> So those would be two separate calls in the <code>show</code> action.</p>
</blockquote>
</aside>
<p>To be clear though, I am absolutely duplicating the user name in a <code>Calendar.User</code> schema. I will not be planning to write the user name from the Calendar context, but will definitely read from it. Perhaps that right there is one of the issues with using the same table with multiple schemas though. If you can/should only update a given column/field from a single context, you don’t immediately know in a given context whether a field can be written to or should be read-only.</p>
<p>Anyway, to me, the beauty of the ability to use the same table for schemas in multiple schemas is that I can more easily reason about in whatever context I’m currently in. Quickly skimming the schemas within the Calendar context shows most of the data used in the context. Context-specific authorization can be performed directly on structs within the calendar context.</p>
<p>It’s not to say I won’t/don’t reach out to other contexts/services when it makes sense, but contexts mostly being able to operate on their own feels pretty nice. As a side benefit, it might make it easier to break these a contexts off on its own app in the future. That is not the reason I’m doing it though. It is attempting to (maybe even wrongly) make it easier to understand and, ultimately, more maintainable.</p>
<aside class="quote no-group" data-username="tomekowal" data-post="14" data-topic="29145">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomekowal/48/2847_2.png" class="avatar"> tomekowal:</div>
<blockquote>
<p>Amen! Ecto is fantastic! It makes easy things easy and hard things still doable!</p>
</blockquote>
</aside>
<p>Agreed. Regardless of the direction I’m going now, I don’t feel particularly locked in to anything. Ecto and Phoenix are both light and flexible and changing things often just comes down to changing some module names.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="163977" 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/any-downsides-to-using-the-same-table-for-multiple-contexts/29145/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-163977" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="163977"
                     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 #14"></div>
  </section>
</div>
    <div class="postbit" id="164019" data-post-id="164019">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is a great thread. I’ve had a lot of these questions myself and haven’t come up with answers for all of them yet.</p>
<p>I just wanted to chip in by sharing a link to “Building beautiful systems with Phoenix contexts… by Andrew Hao” <a href="https://youtu.be/l3VgbSgo71E" rel="noopener nofollow ugc">https://youtu.be/l3VgbSgo71E</a> which I think has some good advice around getting started with contexts, and particularly on sharing concepts between contexts starting from 22:03.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="164019" data-batch-url="/posts/batch_likers">
                        3
                      </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/any-downsides-to-using-the-same-table-for-multiple-contexts/29145/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-164019" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="164019"
                     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 #15"></div>
  </section>
</div>
    <div class="postbit" id="164180" data-post-id="164180">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="baldwindavid" data-post="15" data-topic="29145" data-full="true">
<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>
<aside class="quote no-group" data-username="stefanchrobot" data-post="9" data-topic="29145">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanchrobot/48/7657_2.png" class="avatar"> stefanchrobot:</div>
<blockquote>
<p>But as a result a new context emerged - a context that’s tailored for the specific controller. Since that controller is not tied to a single schema, but a use case, so is the context. Which means that it depends on other, simpler contexts (like <code>Accounts</code> , <code>Schedule</code> , etc.). What do you think about this approach? Do you happen to have similar things (more complex contexts; contexts dependant on other contexts) in your codebase?</p>
</blockquote>
</aside>
<p>I think as I refactor a bit, I will likely end up with some cross-context, um, contexts. The Phoenix docs talk about this a bit at the end… <a href="https://hexdocs.pm/phoenix/contexts.html#strategies-for-cross-context-workflows" class="inline-onebox" rel="noopener nofollow ugc">1. Intro to Contexts — Phoenix v1.8.8</a></p>
</blockquote>
</aside>
<p>Thanks for the link! Somehow I’ve missed the guide on contexts and it’s full of great content (thanks <a class="mention" href="/u/chrismccord" rel="nofollow">@chrismccord</a>!) . I’m actually doing the exact same thing as the <code>UserRegistration</code> context mentioned in the guide.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="164180" 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/any-downsides-to-using-the-same-table-for-multiple-contexts/29145/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-164180" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="164180"
                     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>