index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <!doctype html>
  2. <html lang="en-nz">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Inversion — Astrologue Developer</title>
  7. <meta name="description" content="Inversion of control for the Web-Stack: a dependency-injection container for Vala with modules, scopes and three lifecycles, wired by field injection. Free software, LGPL-3.0-or-later.">
  8. <meta name="color-scheme" content="light dark">
  9. <meta name="theme-color" content="#f2ebda" media="(prefers-color-scheme: light)">
  10. <meta name="theme-color" content="#0a0a12" media="(prefers-color-scheme: dark)">
  11. <link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Ccircle cx='32' cy='32' r='27' fill='none' stroke='%23a8801f' stroke-width='4'/%3E%3Cpath d='M32 8l6 18 18 6-18 6-6 18-6-18-18-6 18-6z' fill='%23a8801f'/%3E%3Ccircle cx='32' cy='32' r='4' fill='%23003078'/%3E%3C/svg%3E">
  12. <meta property="og:title" content="Inversion — Astrologue Developer">
  13. <meta property="og:description" content="A dependency injection (IoC) container for Vala: three lifecycles, field injection, multi-registration and scope-local registrations.">
  14. <meta property="og:type" content="website">
  15. <link rel="stylesheet" href="../../assets/css/style.css">
  16. </head>
  17. <body>
  18. <a class="skip-link" href="#main">Skip to content</a>
  19. <header class="masthead">
  20. <div class="container masthead__inner">
  21. <a class="brand" href="../../">
  22. <img class="brand__logo brand__logo--light" src="../../assets/img/astrologue-light.png" width="392" height="89" alt="Astrologue">
  23. <img class="brand__logo brand__logo--dark" src="../../assets/img/astrologue-dark.png" width="392" height="89" alt="Astrologue">
  24. </a>
  25. <nav class="site-nav" aria-label="Primary">
  26. <ul class="site-nav__list">
  27. <li><a href="../../">Home</a></li>
  28. <li><a href="../../#news">News</a></li>
  29. <li><a href="../../#download">Download</a></li>
  30. <li><a href="../" aria-current="page">Developer</a></li>
  31. <li><a class="nav-cta" href="../../#download">Get Astrologue</a></li>
  32. </ul>
  33. </nav>
  34. </div>
  35. </header>
  36. <main id="main">
  37. <nav class="breadcrumb" aria-label="Breadcrumb">
  38. <div class="container">
  39. <ol>
  40. <li><a href="../../">Astrologue</a></li>
  41. <li><a href="../">Developer</a></li>
  42. <li aria-current="page">Inversion</li>
  43. </ol>
  44. </div>
  45. </nav>
  46. <section class="docs-header">
  47. <div class="container">
  48. <p class="kicker"><span>Foundation · The wiring of the Web-Stack</span></p>
  49. <h1>Inversion</h1>
  50. <p class="lede">A dependency injection (IoC) container for Vala — three lifecycles, field injection, multi-registration and scope-local registrations. It is how an Astrologue web application wires itself together at startup.</p>
  51. <dl class="docs-facts">
  52. <div><dt>Version</dt> <dd><code>0.2</code></dd></div>
  53. <div><dt>Licence</dt> <dd>LGPL-3.0-or-later</dd></div>
  54. <div><dt>Package</dt> <dd><code>usm&nbsp;install&nbsp;inversion</code></dd></div>
  55. <div><dt>Language</dt> <dd>Vala</dd></div>
  56. </dl>
  57. </div>
  58. </section>
  59. <section class="band band--plain" id="docs">
  60. <div class="container docs-layout">
  61. <nav class="docs-toc" aria-label="On this page">
  62. <h2>On this page</h2>
  63. <ul>
  64. <li><a href="#overview">Overview</a></li>
  65. <li><a href="#features">Features</a></li>
  66. <li><a href="#installation">Installation</a></li>
  67. <li><a href="#usage">Usage &amp; documentation</a></li>
  68. <li><a href="#package">Package &amp; source</a></li>
  69. </ul>
  70. </nav>
  71. <div class="docs-prose">
  72. <header class="section-head" id="overview">
  73. <p class="kicker"><span>Overview</span></p>
  74. <h2>Why it exists</h2>
  75. </header>
  76. <p>Inversion is a dependency injection (IoC) container for Vala. Applications declare what they need; the container decides what concrete thing fills each need, and when a new instance is made versus shared. Nothing in the stack reaches out and constructs its own collaborators — the wiring lives in one place, and that place is testable.</p>
  77. <p>The Web-Stack is built around it. Spry's users and permissions, Invercargill-Sql's connections and sessions, an application's own services — all are registered in a container, resolved in scopes, and injected by field. The companion library <a href="../invercargill-sql-inversion/">Invercargill-Sql-Inversion</a> exists purely to make this container and the database layer one motion.</p>
  78. <header class="section-head" id="features">
  79. <p class="kicker"><span>Features</span></p>
  80. <h2>What it hands you</h2>
  81. </header>
  82. <ul class="docs-features">
  83. <li><strong>Three lifecycles.</strong> Transient — a new instance each time; Scoped — one per scope; Singleton — one per container.</li>
  84. <li><strong>Field injection.</strong> Dependencies resolve via <code>inject&lt;T&gt;()</code> in field initializers; no constructor plumbing.</li>
  85. <li><strong>Multi-registration.</strong> Register several implementations for one service type and resolve them all at once.</li>
  86. <li><strong>Scope-local registrations.</strong> Add registrations that only exist within a scope.</li>
  87. <li><strong>Fluent API.</strong> Chained registration methods keep configuration readable.</li>
  88. <li><strong>Registration metadata.</strong> Registrations carry typed metadata that other services can query — how the type provider of the data layer finds its mappers.</li>
  89. </ul>
  90. <header class="section-head" id="installation">
  91. <p class="kicker"><span>Installation</span></p>
  92. <h2>Getting the library</h2>
  93. </header>
  94. <figure class="code-plate">
  95. <pre><code>usm install inversion</code></pre>
  96. <figcaption>USM resolves <code>invercargill-1</code> itself.</figcaption>
  97. </figure>
  98. <p>From source:</p>
  99. <figure class="code-plate">
  100. <pre><code>git clone &lt;the Inversion repository&gt;
  101. cd Inversion
  102. meson setup build
  103. ninja -C build
  104. ninja -C build install</code></pre>
  105. <figcaption>GLib, GObject and Invercargill are the whole dependency set.</figcaption>
  106. </figure>
  107. <figure class="code-plate">
  108. <pre><code>valac --pkg inversion-0.1 my-program.vala</code></pre>
  109. <figcaption>The binding pulls in its <code>.deps</code> chain automatically.</figcaption>
  110. </figure>
  111. <header class="section-head" id="usage">
  112. <p class="kicker"><span>Usage &amp; documentation</span></p>
  113. <h2>Quick start, from the README</h2>
  114. </header>
  115. <p>Define a service, register an implementation, resolve it through a scope:</p>
  116. <figure class="code-plate">
  117. <pre><code>using Inversion;
  118. // Define your service
  119. public interface Logger : Object {
  120. public abstract void log(string message);
  121. }
  122. public class ConsoleLogger : Object, Logger {
  123. public void log(string message) {
  124. stdout.printf("[LOG] %s\n", message);
  125. }
  126. }
  127. // Register and resolve
  128. var container = new Container();
  129. container.register_singleton&lt;ConsoleLogger&gt;((scope) =&gt; new ConsoleLogger())
  130. .as_type(typeof(Logger));
  131. var scope = container.create_scope();
  132. var logger = scope.resolve&lt;Logger&gt;();
  133. logger.log("Hello!");</code></pre>
  134. <figcaption>Verbatim from the README's Quick Start.</figcaption>
  135. </figure>
  136. <h3>Lifecycles</h3>
  137. <p>One registration per lifetime policy:</p>
  138. <figure class="code-plate">
  139. <pre><code>container.register_transient&lt;MyService&gt;((s) =&gt; new MyService());
  140. container.register_scoped&lt;MyService&gt;((s) =&gt; new MyService());
  141. container.register_singleton&lt;MyService&gt;((s) =&gt; new MyService());</code></pre>
  142. <figcaption>Transient: new instance every resolve. Scoped: one per scope. Singleton: one per container.</figcaption>
  143. </figure>
  144. <h3>Field injection</h3>
  145. <p>Dependencies are injected using the <code>inject&lt;T&gt;()</code> function in field initializers — resolved automatically when the owning class is created:</p>
  146. <figure class="code-plate">
  147. <pre><code>public class UserService : Object {
  148. private Logger logger = inject&lt;Logger&gt;();
  149. public void greet(string name) {
  150. this.logger.log(@"Hello, $name!");
  151. }
  152. }
  153. var userService = scope.resolve&lt;UserService&gt;();</code></pre>
  154. <figcaption>And <code>inject_all&lt;T&gt;()</code> brings in every implementation: <code>private Lot&lt;Logger&gt; loggers = inject_all&lt;Logger&gt;();</code></figcaption>
  155. </figure>
  156. <h3>Multiple implementations</h3>
  157. <figure class="code-plate">
  158. <pre><code>container.register_singleton&lt;FileLogger&gt;((s) =&gt; new FileLogger())
  159. .as_type(typeof(Logger));
  160. container.register_singleton&lt;ConsoleLogger&gt;((s) =&gt; new ConsoleLogger())
  161. .as_type(typeof(Logger));
  162. // Get all implementations
  163. var allLoggers = scope.resolve_all&lt;Logger&gt;();</code></pre>
  164. <figcaption>From the README.</figcaption>
  165. </figure>
  166. <h3>Scopes with their own registrations</h3>
  167. <figure class="code-plate">
  168. <pre><code>var scope = container.create_scope();
  169. // Add a registration only available in this scope
  170. scope.register_singleton&lt;RequestContext&gt;((s) =&gt; new RequestContext())
  171. .as_type(typeof(Context));</code></pre>
  172. <figcaption>Scopes manage scoped instance lifetime and can carry local registrations.</figcaption>
  173. </figure>
  174. <p>The repository's <code>examples/</code> directory walks each feature end to end — <code>BasicUsage.vala</code>, <code>LifecycleDemo.vala</code>, <code>InjectionDemo.vala</code>, <code>MultiRegistration.vala</code> and <code>ScopeRegistrationDemo.vala</code> — and the library's test suite runs through <code>meson test</code> and USM's <code>test</code> exec.</p>
  175. <header class="section-head" id="package">
  176. <p class="kicker"><span>Package &amp; source</span></p>
  177. <h2>The ledger</h2>
  178. </header>
  179. <dl class="docs-package">
  180. <div><dt>Version</dt><dd><code>0.2</code> (from <code>MANIFEST.usm</code>)</dd></div>
  181. <div><dt>Licence</dt><dd>LGPL-3.0-or-later — free to run, study, share and improve; the LGPL keeps it free to link from any program.</dd></div>
  182. <div><dt>Package</dt><dd><code>usm install inversion</code></dd></div>
  183. <div><dt>Provides</dt><dd><code>libinversion-0.1.so</code>, <code>inversion-0.1.h</code>, <code>inversion-0.1.vapi</code> + <code>.deps</code>, <code>inversion-0.1.gir</code>, <code>inversion-0.1.typelib</code>, <code>inversion-0.1.pc</code></dd></div>
  184. <div><dt>Needs at runtime</dt><dd>GLib, GObject, and <code>libinvercargill-1.so</code>.</dd></div>
  185. <div><dt>Source</dt><dd>The repository <code>Tilo15/Inversion</code> on the Astrologue forge (git over ssh, port 222). Free software: the licence guarantees your right to the source.</dd></div>
  186. </dl>
  187. <nav class="docs-siblings" aria-label="Neighbours in the catalogue">
  188. <p>Its neighbours:</p>
  189. <ul>
  190. <li><a href="../invercargill-json/">Invercargill-Json</a></li>
  191. <li><a href="../invercargill-sql/">Invercargill-Sql</a></li>
  192. <li><a href="../invercargill-sql-inversion/">Invercargill-Sql-Inversion</a></li>
  193. </ul>
  194. </nav>
  195. <nav class="docs-pager" aria-label="Project pages">
  196. <a class="docs-pager__link docs-pager__link--prev" href="../invercargill-json/">
  197. <span>Previous</span>
  198. <strong>Invercargill-Json</strong>
  199. </a>
  200. <p class="docs-pager__up"><a href="../">All eleven</a></p>
  201. <a class="docs-pager__link docs-pager__link--next" href="../invercargill-sql/">
  202. <span>Next in the catalogue</span>
  203. <strong>Invercargill-Sql</strong>
  204. </a>
  205. </nav>
  206. </div>
  207. </div>
  208. </section>
  209. </main>
  210. <footer class="site-footer">
  211. <div class="container">
  212. <div class="footer-grid">
  213. <div class="foot-brand">
  214. <img class="brand__logo brand__logo--dark" src="../../assets/img/astrologue-dark.png" width="392" height="89" alt="Astrologue">
  215. <p>An operating system for change — kept for self sufficiency, resilience, and software freedom, so regular people can hold the power of general purpose computers in their own hands.</p>
  216. </div>
  217. <nav aria-label="The four freedoms">
  218. <p class="foot-title">The Four Freedoms</p>
  219. <ol class="freedoms">
  220. <li><strong>Run</strong> the software as you wish, for any purpose.</li>
  221. <li><strong>Study</strong> the source and change it to do your computing as you wish.</li>
  222. <li><strong>Share</strong> copies so you can help others.</li>
  223. <li><strong>Modify</strong> and publish your versions so everyone benefits.</li>
  224. </ol>
  225. </nav>
  226. <nav aria-label="Site">
  227. <p class="foot-title">Site</p>
  228. <ul class="foot-list">
  229. <li><a href="../../">Home</a></li>
  230. <li><a href="../../#news">News</a></li>
  231. <li><a href="../../#download">Download</a></li>
  232. <li><a href="../">Developer</a></li>
  233. </ul>
  234. </nav>
  235. </div>
  236. <p class="colophon">Set in Cinzel and EB Garamond (SIL Open Font License). Astrologue is free software, licensed GPL-3.0-or-later: take it, tend it, pass it on. The libraries are LGPL-3.0-or-later, so they remain free to link.</p>
  237. </div>
  238. </footer>
  239. </body>
  240. </html>