<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Not My Idea &#187; Architecture logicielle</title>
	<atom:link href="http://www.notmyidea.org/article/tag/architecture-logicielle/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.notmyidea.org</link>
	<description>Carnets Web d&#039;Alexis Métaireau</description>
	<lastBuildDate>Tue, 20 Jul 2010 10:07:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introduction à l&#8217;injection de dépendances: le cas du singleton.</title>
		<link>http://www.notmyidea.org/article/dependency-injection-singleton-design-pattern-ioc-dendencies-dependences/</link>
		<comments>http://www.notmyidea.org/article/dependency-injection-singleton-design-pattern-ioc-dendencies-dependences/#comments</comments>
		<pubDate>Mon, 25 May 2009 14:36:58 +0000</pubDate>
		<dc:creator>Alexis Metaireau</dc:creator>
				<category><![CDATA[Architecture logicielle]]></category>
		<category><![CDATA[Injection de dépendances]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[spiral]]></category>

		<guid isPermaLink="false">http://www.notmyidea.org/?p=67</guid>
		<description><![CDATA[L&#8217;architecture logicielle est un des aspects du développement qui m&#8217;intéresse particulièrement. Ça fait d&#8217;ailleurs un bout de temps que l&#8217;idée d&#8217;un article à propos des dépendances (de l&#8217;injection de dépendances) et du cas du singleton me trotte dans la tête, alors, le voici:
Avant tout, il faut bien comprendre que sans contexte il n&#8217;existe pas de [...]]]></description>
			<content:encoded><![CDATA[<p>L&#8217;architecture logicielle est un des aspects du développement qui m&#8217;intéresse particulièrement. Ça fait d&#8217;ailleurs un bout de temps que l&#8217;idée d&#8217;un article à propos des dépendances (de l&#8217;injection de dépendances) et du cas du singleton me trotte dans la tête, alors, le voici:</p>
<p>Avant tout, il faut bien comprendre que sans contexte il n&#8217;existe pas de manière plus censée qu&#8217;une autre pour construire une application. Il existe par contre un ensemble de bonnes pratiques qu&#8217;il est bon de connaitre et d&#8217;appliquer, dans la plus large mesure possible. Ces pratiques sont souvent mal connues, et nombre d&#8217;entre elles sont appliquées à la va vite.</p>
<p>Nous allons tenter d&#8217;expliquer ici pourquoi et comment l&#8217;implémentation la plus rependue du singleton favorise la création de dépendances, et comment nous pouvons y remédier, grâce à l&#8217;injection de dépendances.<br />
<span id="more-67"></span></p>
<h3 id="toc-le-singleton">Le singleton</h3>
<div id="attachment_140" class="wp-caption alignright" style="width: 160px"><img class="size-thumbnail wp-image-140" title="singleton" src="http://www.notmyidea.org/wp-content/uploads/2009/05/singleton-150x150.jpg" alt="Le singleton, à l'origine de dépendances" width="150" height="150" /><p class="wp-caption-text">Le singleton, à l&#39;origine de dépendances</p></div>
<p>Le Singleton est un patron de conception (souvent le premier patron de conception qui nous est présenté) qui doit être appliqué avec précaution. Son utilisation est souvent mal comprise et celui ci peut être considéré à tort comme une solution &laquo;&nbsp;miracle&raquo;&nbsp; pour accéder facilement à un objet, comme on le ferait d&#8217;une variable globale. On dit d&#8217;ailleurs aussi qu&#8217;il s&#8217;agit de l&#8217;<em>antipattern</em> singleton, à raison. Pourquoi ?</p>
<p>Très rapidement, un singleton est une classe dont il existe un nombre défini d&#8217;instances. Typiquement, le singleton est utilisé afin de réduire le nombre possible d&#8217;instances d&#8217;une classe à une seule. Cela peut être très utile, et est utilisé dans de nombreux cas à bon escient (je pense par exemple à l&#8217;accès à une base de données).</p>
<p>Un des défauts majeur de ce motif, est l&#8217;implémentation la plus souvent présentée, qui va de paire avec l&#8217;usage de méthodes statiques pour récupérer une instance de la classe. Pour mieux comprendre, voici l&#8217;implémentation type, en PHP (le motif est sensiblement le même dans de nombreux langages):</p>
<div class="codecolorer-container php notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> DbConnection_MySql <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_instance</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __construct <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_instance</span> instanceof <span style="color: #000000; font-weight: bold;">self</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_instance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_instance</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// do some stuff</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Ici, pour récupérer l&#8217;instance de la classe, il est nécessaire de faire appel à la méthode statique <em>getInstance </em>de la classe <em>DbConnection_Mysql</em>.</p>
<p>Il est alors nécessaire, dans la classe utilisatrice du singleton (imaginons qu&#8217;il s&#8217;agisse d&#8217;une classe User), de <em>hardcoder</em> le nom de la classe DbConnection_Mysql, la classe A devenant alors <span style="text-decoration: underline;">dépendante</span> de cette implémentation particulière du singleton.</p>
<div class="codecolorer-container php notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> User<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// dépendance avec la classe DbConnection</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$dbc</span> <span style="color: #339933;">=</span> DbConnection_MySql<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Avec cette implémentation, il est impossible de changer la classe en charge de la connexion à la base de données sans modifier le code. Imaginons que nous souhaitons utiliser la classe <em>DbConnection_Oracle</em> par exemple, nous sommes bloqués. Il s&#8217;agit d&#8217;une problème de conception, et peut faire perdre un temps important lors du développement.</p>
<h3 id="toc-inversion-de-controle-injection-de-dependances">Inversion de contrôle / Injection de dépendances</h3>
<p>Comme nous venons de le voir, le singleton, ou plutôt, cette implémentation du singleton, introduit et favorise les dépendances entre nos classes.</p>
<p>Une des solutions possible est d&#8217;inverser le flux de contrôle de notre application: au lieu que ce soit la classe <em>User</em> qui appelle directement <em>DbConnection_Mysql</em>, celui ci peut être crée en amont, pour être ensuite injecté dans la classe qui souhaite l&#8217;utiliser. De cette manière,  Ce principe à un nom, il s&#8217;agit de <span style="text-decoration: underline;">l&#8217;injection de dépendances</span>.</p>
<h4 id="toc-theorie">Théorie</h4>
<p><em>Alice</em> a besoin d&#8217;une Glace pour la <em>manger()</em>.<br />
<em>Alice</em> aime les <em>GlaceALaFraise</em>&#8230; Si on donne de l&#8217;argent à <em>Alice</em> elle achète systématiquement une <em>GlaceALaFraise</em>. <em>Alice</em> est bloquée au seul parfum &laquo;&nbsp;fraise des bois&raquo;&nbsp; aussi savoureux soit-il.</p>
<p>Pour lui faire découvrir de nouvelles saveurs sa <em>Maman</em> décide de ne plus laisser <em>Alice</em> choisir sa <em>Glace</em> elle-même, mais se charge de choisir pour elle une <em>GlaceAuPaté</em>. </p>
<p>Quels que soit les goûts d&#8217;<em>Alice</em>, une <em>GlaceAuPaté</em> reste une <em>Glace</em>, et donc <em>Alice</em> est tout à fait à même de la <em>manger()</em><sup>1</sup>.</p>
<p>Le concept exposé ici est connu comme le <em><a href="http://en.wikipedia.org/wiki/Hollywood_principle">principe d&#8217;Holywood</a></em>: &laquo;&nbsp;Ne vous appelez pas vous même, nous vous appellerons&raquo;&nbsp;.</p>
<p>Ce qu&#8217;il faut retenir, c&#8217;est qu&#8217;<em>Alice</em> peut, si elle le souhaite, <em>manger()</em> tout type de <em>Glace</em>, même si au départ <em>Alice</em> n&#8217;à besoin de <em>manger()</em> q&#8217;une <em>GlaceALaFraise</em>. Nous favorisons ici la réutilisations du code: nous pouvons réutiliser Alice facilement et substituer sa <em>Glace</em> par une autre facilement.</p>
<h4 id="toc-application-a-notre-exemple">Application à notre exemple</h4>
<p>Reprenons l&#8217;exemple des utilisateurs et de l&#8217;utilisation de la DbConnection pour y appliquer ce principe d&#8217;inversion de contrôle. Notre code ressemblerait alors à quelque chose dans cet esprit:</p>
<div class="codecolorer-container php notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$dbc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DbConnection_Default<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassA_Default<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDbConnection</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Avec les deux interfaces/implémentations suivantes pour DbConnection et User:</p>
<div class="codecolorer-container php notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:420px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// Interfaces</span><br />
<span style="color: #000000; font-weight: bold;">interface</span> DbConnection <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">interface</span> User<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> setDbConnection<span style="color: #009900;">&#40;</span>DbConnection<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Implementations</span><br />
<span style="color: #000000; font-weight: bold;">class</span> DbConnection_Mysql implements DbConnection<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// store parameters into the object</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// connect to the database</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> User_Default implements User<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setDbConnection<span style="color: #009900;">&#40;</span>DbConnection <span style="color: #000088;">$dbc</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// store $dbc into the object</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Ici, il subsiste des dépendances dans le code. Il s&#8217;agit de dépendances vis à vis de contrats (interfaces) et non d&#8217;implémentations données (classes), puisque j&#8217;ai choisi d&#8217;utiliser le paradigme de <a href="http://fr.wikipedia.org/wiki/Programmation_par_contrat">programmation par contrats</a>.</p>
<p>Nous avons parlé de singleton et de création de dépendances, mais l&#8217;injection de dépendances est applicable à tout type de classes, et devrait d&#8217;ailleurs être utilisé dans la plupart des projet d&#8217;importante envergure.</p>
<h3 id="toc-utilisation-dun-conteneur">Utilisation d&#8217;un conteneur</h3>
<div id="attachment_170" class="wp-caption alignright" style="width: 197px"><img src="http://www.notmyidea.org/wp-content/uploads/2009/05/exemple.png" alt="schéma des dépendances entre nos classes" title="exemple" width="187" height="155" class="size-full wp-image-170 alignleft" /><p class="wp-caption-text">schéma des dépendances entre nos classes</p></div>
<p>Pour aller un peu plus loin, il peut être utile et efficace de déléguer la tâche de création des objets à un conteneur. Il s&#8217;agit d&#8217;une classe, en charge de la création de tous les objets, ce dernier pouvant résoudre les dépendances nécessaires à la création de l&#8217;objet souhaité.</p>
<p>Nous avons donc le schéma de dépendances décrit à gauche. Une fois ces informations fournies au conteneur (via un fichier de configuration), nous pouvons facilement récupérer l&#8217;object <em>User_Default</em> en le demandant à notre conteneur:</p>
<div class="codecolorer-container text notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$container-&gt;user</div></div>
<p>. Ce dernier se charge alors de créer l&#8217;ensemble des dépendances de cet objet (un objet de la classe DbConnection_Mysql ici). Plutôt sympa, non ?</p>
<p>Pour faire en sorte que la classe d&#8217;accès aux données soit un singleton, c&#8217;est le schéma qu&#8217;il faut alors modifier (via le fichier de configuration ici.)</p>
<p>Ici, j&#8217;ai essentiellement parlé de dépendances entre classes. Cependant, de la même manière, il est possible de contrôler tous les arguments passés à nos objets, qu&#8217;il s&#8217;agisse de tableaux, de chaines de caractère ou autres.</p>
<h3 id="toc-moi-aussi-jen-veux-spiral-di-container">Moi aussi j&#8217;en veux ! Spiral Di Container</h3>
<p>Pour mon usage personnel, j&#8217;ai développé un injecteur de dépendances flexible, permettant d&#8217;appliquer ce modèle à mes futurs développements. Il existe déjà des projets similaires, mais ces derniers font un usage trop abusif &#8211; à mon gout &#8211; du mécanisme de réflexion, perdant alors grandement en performance.</p>
<p>Voyons comment nous pouvons utiliser le conteneur dans notre cas. Cela revient à créer le schéma de dépendances. Cela est possible pour le moment par 2 biais: via XML ou directement via php.</p>
<h4 id="toc-directement-via-php">Directement via php</h4>
<div class="codecolorer-container php notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$fluent</span><br />
<span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerService</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'User_Default'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">call</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'setDbConnection'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">withServices</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dbc'</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerService</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dbc'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'DbConnection_Mysql'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">-&gt;</span><span style="color: #004000;">construct</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">with</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span></div></div>
<h4 id="toc-via-un-fichier-xml">Via un fichier XML</h4>
<div class="codecolorer-container xml notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;container<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;user&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;User_Default&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;setDbConnection&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;dbc&quot;</span> <span style="color: #000066;">service</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dbc&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;DbConnection_Mysql&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;__construct&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;localhost&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;root&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/container<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>Il est à noter que le fichier XML à été directement généré via le <em>Dumper</em> Xml, en utilisant le schema.</p>
<p>Si vous êtes interessé pour regarder plus en détail le fonctionnement de ce conteneur, les sources sont d&#8217;ores et déjà disponibles via le framework de travail spiral, disponible via <a href="http://bitbucket.org/ametaireau/spiral-php52/src/di/">son dépot mercurial</a>.</p>
<p>L&#8217;injecteur de dépendances est présent dans la partie /core/Di de l&#8217;architecture. Une version standalone devrait bientôt voir le jour.</p>
<p>Si cet article vous à plu, je publierais prochainement une série d&#8217;article expliquant en détail comment utiliser un conteneur de dépendances, en m&#8217;appuyant sur des exemples concrets que j&#8217;ai pu rencontrer.</p>
<p>Ah, et j&#8217;oubliais, un grand merci aux relecteurs pour votre aide précieuse ! (Fred, David, Florent, Joel, Nico &#8230;)</p>
<ol class="footnotes"><li id="footnote_0_67" class="footnote">Merci Frédéric pour ton super exemple !</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.notmyidea.org/article/dependency-injection-singleton-design-pattern-ioc-dendencies-dependences/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
