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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="kostonstyle" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  kostonstyle
                    <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">
								<p>I like the concept you mentioned earlier, passing module to the function parameter.<br>
But how can I ensure, that user pass the right module as function parameter, that contains the function?</p>
<p>May I solve the problem with protocol?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="20573" 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-use-dependency-injection-pattern-in-elixir/3202/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-20573" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="20573"
                     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="20581" data-post-id="20581">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Generally speaking, this question comes up a lot when people are coming from OO languages. <a class="mention" href="/u/mkunikow" rel="nofollow">@mkunikow</a> makes a valid point that you don’t need it. The solution is fairly simple especially when it comes to testing. If you take the following steps, you won’t need to mock your functions that are touching the outside world.</p>
<ol>
<li>
<p>Keep your impure functions separate from your pure functions. If you have a function that touches a database, calls a webservice, gets the date, etc. Or anything that touches the outside world it should be put in a separate module/function and there should be only one place in your program where it’s called and that data gets passed to your pure functions.</p>
</li>
<li>
<p>Your unit test will only need the stubbed data that would have come from an impure function like the data that would come from a webservice or a database.</p>
</li>
</ol>
<p>The only time mocking is necessary is when you are calling impure functions from pure functions and they are intertwined (this is true even in OO languages). However, if you keep your impure functions separate from your pure functions then there is no need to have DI or a mocking framework when unit testing.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="20581" 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/how-to-use-dependency-injection-pattern-in-elixir/3202/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-20581" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="20581"
                     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="20595" data-post-id="20595">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="JEG2" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/JEG2/120/936_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  JEG2
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Designing Elixir Systems with OTP</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="Korbin73" data-post="13" data-topic="3202">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/korbin73/48/281_2.png" class="avatar"> Korbin73:</div>
<blockquote>
<p>The only time mocking is necessary is when you are calling impure functions from pure functions…</p>
</blockquote>
</aside>
<p>If it calls an impure function, it can’t be a pure function.  <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="20595" 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/how-to-use-dependency-injection-pattern-in-elixir/3202/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-20595" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="20595"
                     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="20600" data-post-id="20600">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="JEG2" data-post="14" data-topic="3202">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jeg2/48/936_2.png" class="avatar"> JEG2:</div>
<blockquote>
<p>If it calls an impure function, it can’t be a pure function.  <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>I know, I was referring to that’s the reason why it’s used in OO languages in the first place <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> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="20600" 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-use-dependency-injection-pattern-in-elixir/3202/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-20600" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="20600"
                     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="20602" data-post-id="20602">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Throwing another article out here: <a href="http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/" rel="noopener nofollow ugc">Mocks and explicit contracts</a>.</p>
<p>It presents several ways to “inject” dependencies to your functions, specifically by 1) defining the dependency module on your config, 2) passing the dependency’s handler function as the caller params, or 3) same as 2 but passing the dependency module altogether.</p>
<p>These three approaches allow you to use mock dependencies for your tests.</p>
<aside class="quote no-group" data-username="kostonstyle" data-post="6" data-topic="3202">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/5f9b8f/48.png" class="avatar"> kostonstyle:</div>
<blockquote>
<p>The problem is only the dynamic typing. How can I ensure, when someone use the function, that will pass the right module?</p>
</blockquote>
</aside>
<p>My initial guess is to use a combination of behaviors and typespecs.</p>
<aside class="quote no-group quote-modified" data-username="kostonstyle" data-post="7" data-topic="3202">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/5f9b8f/48.png" class="avatar"> kostonstyle:</div>
<blockquote>
<p>I’ve find this <a href="https://www.djm.org.uk/posts/writing-extensible-elixir-with-behaviours-adapters-pluggable-backends/" class="inline-onebox" rel="noopener nofollow ugc">Writing extensible Elixir with Behaviours</a> article, but do not know, if it is the right way.</p>
</blockquote>
</aside>
<p>This article echoes José’s Plataformatec article I shared above on dependency injection methods, so I think you can use that as a base. The only thing it left out is for you to validate if a module has adopted a specific behavior. I’m not sure at the specifics on how to do that, though.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="20602" data-batch-url="/posts/batch_likers">
                        1
                      </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-use-dependency-injection-pattern-in-elixir/3202/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-20602" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="20602"
                     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="20681" data-post-id="20681">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>If a module fails to fully implement a behaviour, the compiler will warn about it..</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="20681" data-batch-url="/posts/batch_likers">
                        1
                      </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-use-dependency-injection-pattern-in-elixir/3202/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-20681" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="20681"
                     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 #16"></div>
  </section>
</div>
    <div class="postbit" id="20683" data-post-id="20683">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m surprised this topic didn’t spark any discussions around GenStage?</p>
<p>Personally, I’ve been using a pattern similar to Pact, but I have an item in the TODO list to properly learn and understand GenStage to decouple my system. Granted, it won’t solve your synchronous usecases, but I believe it’s worth a mention.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="20683" 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-use-dependency-injection-pattern-in-elixir/3202/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-20683" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="20683"
                     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 #17"></div>
  </section>
</div>
    <div class="postbit" id="20687" data-post-id="20687">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Article - example in F#</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://fsharpforfunandprofit.com/posts/dependency-injection-1/">
  <header class="source">
      <img src="https://fsharpforfunandprofit.com/favicon-32x32.png" class="site-icon" alt="" width="32" height="32">

      <a href="https://fsharpforfunandprofit.com/posts/dependency-injection-1/" target="_blank" rel="noopener nofollow ugc">fsharpforfunandprofit.com</a>
  </header>

  <article class="onebox-body">
    <img width="128" height="128" src="http://fsharpforfunandprofit.com/favicon-128x128.png" class="thumbnail onebox-avatar" alt="">

<h3><a href="https://fsharpforfunandprofit.com/posts/dependency-injection-1/" target="_blank" rel="noopener nofollow ugc">Functional approaches to dependency injection | F# for fun and profit</a></h3>

  <p>Part one of a series, starting with partial application.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="20687" 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/how-to-use-dependency-injection-pattern-in-elixir/3202/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-20687" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="20687"
                     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 #18"></div>
  </section>
</div>
    <div class="postbit" id="87850" data-post-id="87850">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>There is one line in Jose’s blog post that is intriguing:</p>
<blockquote>
<p>Finally, you could also make the dependency a data structure and define the contract with a protocol.</p>
</blockquote>
<p>Can anyone illustrate this for me?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="87850" 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-use-dependency-injection-pattern-in-elixir/3202/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-87850" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="87850"
                     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 #19"></div>
  </section>
</div>
    <div class="postbit" id="87853" data-post-id="87853">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Just to follow up, I’ll give here the same example for this I gave in Slack.</p>
<p>We can define a protocol for our use case - e.g. a repository of users:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defprotocol UserRepository do
  def get(database, id)
end
</code></pre>
<p>And then we can define two implementations - one based on a real database and one based on a simple in-memory ETS store:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Database do
  defstruct [:ecto_repo]

  def new(repo), do: %__MODULE__{ecto_repo: repo}
end

defmodule EtsDatabse do
  defstruct [:table]

  def new(), do: %__MODULE__{table: :ets.new(__MODULE__, [:set, :public])}
end
</code></pre>
<p>We can then implement the protocol for those data structures</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defimpl UserRepository, for: Database do
  def get(%{ecto_repo: repo}, id) do
    repo.get(User, id)
  end
end

defimpl UserRepository, for; EtsDatabase do
 def get(%{table: table}, id) do
  # assuming {{User, id}, struct} tuples in ets table
  :ets.lookup_element(table, {User, id}, 2)
 end
end
</code></pre>
<p>That’s a simple sketch of a protocol-based dependency injection.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="87853" data-batch-url="/posts/batch_likers">
                        8
                      </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-use-dependency-injection-pattern-in-elixir/3202/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-87853" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="87853"
                     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 #20"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/3202/load_more?page=3">Load more posts (2 remaining)</a>
</div></template></turbo-stream>