<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Marcin Jeleński on Medium]]></title>
        <description><![CDATA[Stories by Marcin Jeleński on Medium]]></description>
        <link>https://medium.com/@marcin.jelenski?source=rss-b294a7e8efff------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qH1IRflPURbSgN3Fh5Cl_A.jpeg</url>
            <title>Stories by Marcin Jeleński on Medium</title>
            <link>https://medium.com/@marcin.jelenski?source=rss-b294a7e8efff------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 13:11:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marcin.jelenski/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to reproduce Dagger functions or develop your own DI in Kotlin?]]></title>
            <link>https://medium.com/indoorway-blog/how-to-reproduce-dagger-functions-or-develop-your-own-di-in-kotlin-b8dfd5cdea0c?source=rss-b294a7e8efff------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8dfd5cdea0c</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[kotlin-extension]]></category>
            <category><![CDATA[english]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Marcin Jeleński]]></dc:creator>
            <pubDate>Fri, 27 Apr 2018 07:31:01 GMT</pubDate>
            <atom:updated>2019-03-13T18:29:24.308Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*MRAabyT5zgEnDF44__Lbbw.png" /><figcaption>Illustration by <a href="https://dribbble.com/magdalenatomczyk">Magdalena Tomczyk</a></figcaption></figure><p>As an Android developer, you must have heard of Dagger. Moreover, you’ve probably faced some difficulties with setting it up in Kotlin projects. What are the benefits of this library? Do we still need those Dagger’s annotation processors and code generators? Being inspired by <a href="https://medium.com/u/a199eb0fa9a0">Antonio Leiva</a> great article on <a href="https://antonioleiva.com/dagger-android-kotlin/">how to use Dagger 2 on Android With Kotlin</a> and <a href="https://medium.com/u/5742b4fcf89e">Elye</a>’s <a href="https://medium.com/@elye.project/dagger-2-for-dummies-in-kotlin-with-one-page-simple-code-project-618a5f9f2fe8">Dagger 2 for Dummies in Kotlin</a>, as well as relying on my own experience, I’ll try to convince you that actually we don’t need them at all.</p><p>In this article I’ll show you how basic Dagger features can be easily replaced.</p><h3>Setup</h3><p>The concept of skipping Dagger in Kotlin deserves its own name. Double “g” for the similarity with “Dagger” and “K” as in “Kotlin”. I called it <em>Kagger, and </em>surprisingly<em>,</em> in Danish <em>kager</em> means <em>cakes </em>🍰. It’s not a library, so assuming you’ve already defined Kotlin dependencies, you have nothing to configure at all. Or solution will be pure-kotlin and therefore a piece of cake :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*PWjgXcd7sbP9QuUg." /><figcaption>source: imgflip.com</figcaption></figure><h3>Components and modules</h3><p>Just like in Dagger, we are using “components” and “modules” terms. The main difference is we split them to their <strong><em>interfaces</em></strong> and <strong><em>implementations</em></strong>. The following comparison should be self-explanatory.</p><h4>Dagger</h4><p>In Dagger you would probably define module like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba21861fd73682ed8e0b22e32cfba63a/href">https://medium.com/media/ba21861fd73682ed8e0b22e32cfba63a/href</a></iframe><p>@Singleton annotation is used to mark single-instance beans, so function provideSomeSingleton() will be called only once. The second one, provideSomeBean() is not annotated as singleton and will be invoked every time it’s being injected. Let’s imagine that we have another few modules, such as AxModule, BxModule and CxModule.</p><p>When it comes to a component, you have to define a list of contained modules and specify which classes will be able to use that component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa094b490fd4f084cac385b61fa66749/href">https://medium.com/media/fa094b490fd4f084cac385b61fa66749/href</a></iframe><p>Then you can build your component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f6c67ee2cc39239d3ca7b9774453ace/href">https://medium.com/media/5f6c67ee2cc39239d3ca7b9774453ace/href</a></iframe><p>And finally you are able to inject your dependencies:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93b6ab5b79346499917cdbe0c018512e/href">https://medium.com/media/93b6ab5b79346499917cdbe0c018512e/href</a></iframe><p>PowerManager and LocationManager will be accessible after OnCreate call.</p><blockquote>Note: In the newest version of Dagger, it’s not necessary to access components by casting application property. I used it to make example clear.</blockquote><h4>Kagger</h4><p>How to achieve the same only with Kotlin?</p><p>Firstly, define <strong>module interface</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f5741b37503c4be6f0e7ac93c30dd61/href">https://medium.com/media/2f5741b37503c4be6f0e7ac93c30dd61/href</a></iframe><p>Please notice I’ve replaced singleton declaration with a field. That’s the way we emphasize the difference between singletons and non-singletons to avoid misleadings in the future. It’s up to you if you follow this pattern or not. When the definition is done, let’s make an <strong>module implementation</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31b321cba6f3a94c8d42c81d5e6102d2/href">https://medium.com/media/31b321cba6f3a94c8d42c81d5e6102d2/href</a></iframe><p>What is more, for singleton field, <a href="https://kotlinlang.org/docs/reference/delegated-properties.html#lazy">lazy property</a> was used. It’s worth to know it’s thread-safe and we can be sure that object will be instantiated only once.</p><p>Let’s move on to component interface:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4f2776a233a427317e42b063c636851/href">https://medium.com/media/b4f2776a233a427317e42b063c636851/href</a></iframe><p>It may seem familiar to Dagger component declaration. Again, apart from its name, we are declaring a list of modules that component should contain. It’s not necessary to specify which classes will be able to “inject” those objects.</p><p>For similarity with above Dagger example, let’s implement a component as an object expression:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f8aed38af327fda225684f5d09918db/href">https://medium.com/media/5f8aed38af327fda225684f5d09918db/href</a></iframe><p>Here we are using the true power of Kotlin — delegation. MyComponent interface extends AxModule, BxModule and CxModule. Thanks to the <a href="https://kotlinlang.org/docs/reference/delegation.html#implementation-by-delegation">implementation by delegation</a> we are keeping those modules apart and we don’t have to implement all methods and fields in one place. Each module can contain its own dependencies, as AxModule which uses application context.</p><p>You may create separate class for component implementation as well:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f26789d9316f469a0fa8491a2ef29aab/href">https://medium.com/media/f26789d9316f469a0fa8491a2ef29aab/href</a></iframe><p>So MyApplication class simplifies to:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a17088469497c1d107912b097ef61cf0/href">https://medium.com/media/a17088469497c1d107912b097ef61cf0/href</a></iframe><p>If you do so, the access to items looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/601ccff8dc6cb4700a398a8d61e5b190/href">https://medium.com/media/601ccff8dc6cb4700a398a8d61e5b190/href</a></iframe><p>As you can see above, we are “injecting” items without annotation or lateinit keyword. Vals remains vals. In case you want to parameterize non-singleton objects, just add a parameter to a module function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa1e4e9246c51beb879475b032e6ab63/href">https://medium.com/media/aa1e4e9246c51beb879475b032e6ab63/href</a></iframe><p>For testing purposes, you can create a mocked version of MyComponent and replace only necessary modules, methods or fields.</p><p>To help you with better understanding of this matter, I’ve put a compilable project to GitHub repository: <a href="https://github.com/marcin-jelenski/kagger">https://github.com/marcin-jelenski/kagger</a></p><h3>Why does it matter?</h3><p>With the mechanism described above, we were able to remove simple Dagger dependencies in projects created for <a href="https://medium.com/u/fa4b8ed6b69c">Indoorway</a>. What’s more, Kagger may be used outside of Android projects. I’m looking forward for your opinions and thoughts on this.</p><figure><a href="https://www.indoorway.com/"><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*QO9xdueFo5qpaiGGfdbzmg.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8dfd5cdea0c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/indoorway-blog/how-to-reproduce-dagger-functions-or-develop-your-own-di-in-kotlin-b8dfd5cdea0c">How to reproduce Dagger functions or develop your own DI in Kotlin?</a> was originally published in <a href="https://medium.com/indoorway-blog">Indoorway Blog | Actual data about your manufacturing processes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>