<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Service Layer in Web applications</title>
	<atom:link href="http://dev.juokaz.com/programming/service-layer-in-web-applications/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com/programming/service-layer-in-web-applications</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Fri, 05 Mar 2010 08:25:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: ed</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications/comment-page-1#comment-8892</link>
		<dc:creator>ed</dc:creator>
		<pubDate>Fri, 12 Feb 2010 16:25:38 +0000</pubDate>
		<guid isPermaLink="false">http://dev.juokaz.com/?p=888#comment-8892</guid>
		<description>Hi Juozas, thanks for quick response.

Lets see if i got it.

My app is an ecommerce application. The user is not authenticated yet, but i already have an id in the session to identify the shopping cart.

So in the bootstrap i could do:
&lt;code&gt;
Zend_Registry::set(&quot;transactionId&quot;,&quot;here can be a cookie value&quot;)&lt;/code&gt;

And then in my controller to retrieve the order i do:

&lt;code&gt;

public function viewOrderAction()
{
   $service = new OrdersService();
   $order = $service-&gt;getOrder(Zend_Registry::get(&quot;transactionId&quot;));
}

&lt;/code&gt;

Is that correct, right? In this case i am letting my service with dependency injection, how it should be.</description>
		<content:encoded><![CDATA[<p>Hi Juozas, thanks for quick response.</p>
<p>Lets see if i got it.</p>
<p>My app is an ecommerce application. The user is not authenticated yet, but i already have an id in the session to identify the shopping cart.</p>
<p>So in the bootstrap i could do:<br />
<code><br />
Zend_Registry::set("transactionId","here can be a cookie value")</code></p>
<p>And then in my controller to retrieve the order i do:</p>
<p><code></p>
<p>public function viewOrderAction()<br />
{<br />
   $service = new OrdersService();<br />
   $order = $service-&gt;getOrder(Zend_Registry::get("transactionId"));<br />
}</p>
<p></code></p>
<p>Is that correct, right? In this case i am letting my service with dependency injection, how it should be.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Juozas</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications/comment-page-1#comment-8890</link>
		<dc:creator>Juozas</dc:creator>
		<pubDate>Fri, 12 Feb 2010 15:57:26 +0000</pubDate>
		<guid isPermaLink="false">http://dev.juokaz.com/?p=888#comment-8890</guid>
		<description>Ed,

Auth class was kind of example how to configure authentication which is used by service. 

In controller that class is not touched at all, because authentication is configured in bootstrap. This class can be used in tests, to set and unset user id/other properties. 

Now how it was written in a last example, is by something like:

&lt;pre lang=&quot;php&quot;&gt;
public function _construct()
{
    $this-&gt;_setId(Auth::getIdentity());
}
&lt;/pre&gt;

Here we have dependency to Auth class, so ideally you&#039;d want to either inject user object/id/whatever to constructor with help of DIC or inject auth class as object. Maybe even like this (use this myself usualy):

&lt;pre lang=&quot;php&quot;&gt;
public function __construct($userId = null)
{
    // handle null case somehow/disallow it
    $this-&gt;_setId($userId);
}
&lt;/pre&gt;

In all cases, dependency to session doesn&#039;t exist. Services (and Auth class if used) have ID, maybe some other properties, but they are just variables: cookies, sessions or any other logic you use to authenticate is decoupled. 

This post was mostly referring to controller which uses session to handle logic. So the goal was to remove it and make controller session-blind. Is it clearer now? (confused whether I&#039;m clear)</description>
		<content:encoded><![CDATA[<p>Ed,</p>
<p>Auth class was kind of example how to configure authentication which is used by service. </p>
<p>In controller that class is not touched at all, because authentication is configured in bootstrap. This class can be used in tests, to set and unset user id/other properties. </p>
<p>Now how it was written in a last example, is by something like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><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>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_setId<span style="color: #009900;">&#40;</span>Auth<span style="color: #339933;">::</span><span style="color: #004000;">getIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here we have dependency to Auth class, so ideally you&#8217;d want to either inject user object/id/whatever to constructor with help of DIC or inject auth class as object. Maybe even like this (use this myself usualy):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><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;">$userId</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// handle null case somehow/disallow it</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_setId<span style="color: #009900;">&#40;</span><span style="color: #000088;">$userId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In all cases, dependency to session doesn&#8217;t exist. Services (and Auth class if used) have ID, maybe some other properties, but they are just variables: cookies, sessions or any other logic you use to authenticate is decoupled. </p>
<p>This post was mostly referring to controller which uses session to handle logic. So the goal was to remove it and make controller session-blind. Is it clearer now? (confused whether I&#8217;m clear)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ed</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications/comment-page-1#comment-8889</link>
		<dc:creator>ed</dc:creator>
		<pubDate>Fri, 12 Feb 2010 15:17:33 +0000</pubDate>
		<guid isPermaLink="false">http://dev.juokaz.com/?p=888#comment-8889</guid>
		<description>&quot;(passing directly to the controller) &quot; i mean 

(passing directly to the constructor)</description>
		<content:encoded><![CDATA[<p>&#8220;(passing directly to the controller) &#8221; i mean </p>
<p>(passing directly to the constructor)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ed</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications/comment-page-1#comment-8888</link>
		<dc:creator>ed</dc:creator>
		<pubDate>Fri, 12 Feb 2010 15:15:20 +0000</pubDate>
		<guid isPermaLink="false">http://dev.juokaz.com/?p=888#comment-8888</guid>
		<description>Hello Juozas.

First your say &quot;I looked at it again and though – why does controller need to know the userId?&quot;

Ok with this phrase you say there&#039;s no dependency injection.

After you say :

&quot;For such things I use service layer: it has user info injected from bootstrap (or directly to a constructor) and operates with data only accessible to the user.&quot;

Ok here you keep with no dependency injection. What i understard that is you set the user sessionId direct to the conctructor.

But at the end you say this is wrong because the service can get the auth instance inside himself:

&quot;$auth = Auth::getInstance();
$auth-&gt;setIdentity(1);
 
$service = new Service(); // here service get&#039;s identity from auth&quot;

Ok as i have understood. You saying this is wrong. Service layer always should has dependency injection.

So i ask why in your first 2 examples you said that the controller doesn&#039;t need to know the session id (passing directly to the controller) ? This is not a contradiction ? 

Please clarify how to handle session Ids and pass them to service layer.</description>
		<content:encoded><![CDATA[<p>Hello Juozas.</p>
<p>First your say &#8220;I looked at it again and though – why does controller need to know the userId?&#8221;</p>
<p>Ok with this phrase you say there&#8217;s no dependency injection.</p>
<p>After you say :</p>
<p>&#8220;For such things I use service layer: it has user info injected from bootstrap (or directly to a constructor) and operates with data only accessible to the user.&#8221;</p>
<p>Ok here you keep with no dependency injection. What i understard that is you set the user sessionId direct to the conctructor.</p>
<p>But at the end you say this is wrong because the service can get the auth instance inside himself:</p>
<p>&#8220;$auth = Auth::getInstance();<br />
$auth-&gt;setIdentity(1);</p>
<p>$service = new Service(); // here service get&#8217;s identity from auth&#8221;</p>
<p>Ok as i have understood. You saying this is wrong. Service layer always should has dependency injection.</p>
<p>So i ask why in your first 2 examples you said that the controller doesn&#8217;t need to know the session id (passing directly to the controller) ? This is not a contradiction ? </p>
<p>Please clarify how to handle session Ids and pass them to service layer.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Service Layer in MVC&#160;&#124;&#160;Developer&#39;s Kanundrum</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications/comment-page-1#comment-7548</link>
		<dc:creator>Service Layer in MVC&#160;&#124;&#160;Developer&#39;s Kanundrum</dc:creator>
		<pubDate>Mon, 28 Dec 2009 21:52:23 +0000</pubDate>
		<guid isPermaLink="false">http://dev.juokaz.com/?p=888#comment-7548</guid>
		<description>[...] web that explains the service layer. A good introduction from the PHP perspective can be found on Juozas&#8217; website but I also like the analogy used here. Between all that I&#8217;ve read I realize that this is [...]</description>
		<content:encoded><![CDATA[<p>[...] web that explains the service layer. A good introduction from the PHP perspective can be found on Juozas&#8217; website but I also like the analogy used here. Between all that I&#8217;ve read I realize that this is [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Giorgio Sironi</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications/comment-page-1#comment-7414</link>
		<dc:creator>Giorgio Sironi</dc:creator>
		<pubDate>Tue, 22 Dec 2009 13:15:58 +0000</pubDate>
		<guid isPermaLink="false">http://dev.juokaz.com/?p=888#comment-7414</guid>
		<description>Strictly Doctrine_Table is a Table Data Gateway, not a Repository. They both are data access patterns.
I consider the Service layer as a thin layer to support mundane tasks which do not fit in the Domain Model, like authentication/authorization, logging, http request management, and similar.</description>
		<content:encoded><![CDATA[<p>Strictly Doctrine_Table is a Table Data Gateway, not a Repository. They both are data access patterns.<br />
I consider the Service layer as a thin layer to support mundane tasks which do not fit in the Domain Model, like authentication/authorization, logging, http request management, and similar.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Exception e</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications/comment-page-1#comment-7413</link>
		<dc:creator>Exception e</dc:creator>
		<pubDate>Tue, 22 Dec 2009 12:44:03 +0000</pubDate>
		<guid isPermaLink="false">http://dev.juokaz.com/?p=888#comment-7413</guid>
		<description>@Federico
&lt;blockquote cite=&quot;Federico&quot;&gt;The service layer doesn’t supply access to data/objects, it coordinates other services&lt;/blockquote&gt;
This means that no service could exist, or some of them need to do mutual coordination.
Myabe you could explain it a little more. 

&lt;blockquote cite=&quot;Federico&quot;&gt;What you can do is inject a repository into a service. &lt;/blockquote&gt;
So what you propose is that if you want to get an Order in your controller, you need to have one extra level of indirection?

OrderService -&gt; OrderRepo -&gt; {source}

Would you consider Doctrine_Table a repository?</description>
		<content:encoded><![CDATA[<p>@Federico</p>
<blockquote cite="Federico"><p>The service layer doesn’t supply access to data/objects, it coordinates other services</p></blockquote>
<p>This means that no service could exist, or some of them need to do mutual coordination.<br />
Myabe you could explain it a little more. </p>
<blockquote cite="Federico"><p>What you can do is inject a repository into a service. </p></blockquote>
<p>So what you propose is that if you want to get an Order in your controller, you need to have one extra level of indirection?</p>
<p>OrderService -&gt; OrderRepo -&gt; {source}</p>
<p>Would you consider Doctrine_Table a repository?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Federico</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications/comment-page-1#comment-7412</link>
		<dc:creator>Federico</dc:creator>
		<pubDate>Tue, 22 Dec 2009 11:19:38 +0000</pubDate>
		<guid isPermaLink="false">http://dev.juokaz.com/?p=888#comment-7412</guid>
		<description>I think the most important is to exlpain what a service layer is and when to use it. In the OrdersService example you provided, you shouldn&#039;t be using a service. The service layer doesn&#039;t supply access to data/objects, it coordinates other services. A service usually refers to some domain logic which does not belongs to entities or repositories. What you can do is inject a repository into a service. Repositories are a type of service and deal with data access. If your method OrdersService::getOrder($id) returns an Order or some procedural data, then it should be moved to OrdersRepository.

Cheers</description>
		<content:encoded><![CDATA[<p>I think the most important is to exlpain what a service layer is and when to use it. In the OrdersService example you provided, you shouldn&#8217;t be using a service. The service layer doesn&#8217;t supply access to data/objects, it coordinates other services. A service usually refers to some domain logic which does not belongs to entities or repositories. What you can do is inject a repository into a service. Repositories are a type of service and deal with data access. If your method OrdersService::getOrder($id) returns an Order or some procedural data, then it should be moved to OrdersRepository.</p>
<p>Cheers</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Juozas</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications/comment-page-1#comment-6987</link>
		<dc:creator>Juozas</dc:creator>
		<pubDate>Mon, 07 Dec 2009 16:26:05 +0000</pubDate>
		<guid isPermaLink="false">http://dev.juokaz.com/?p=888#comment-6987</guid>
		<description>Federico, I&#039;m playing with different locators right now.

Currently I&#039;m using a registry to store identity, and service&#039;s constructor calls identity container method to obtain it. OR in tests i inject it (or null) manually.</description>
		<content:encoded><![CDATA[<p>Federico, I&#8217;m playing with different locators right now.</p>
<p>Currently I&#8217;m using a registry to store identity, and service&#8217;s constructor calls identity container method to obtain it. OR in tests i inject it (or null) manually.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Federico</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications/comment-page-1#comment-6986</link>
		<dc:creator>Federico</dc:creator>
		<pubDate>Mon, 07 Dec 2009 16:18:55 +0000</pubDate>
		<guid isPermaLink="false">http://dev.juokaz.com/?p=888#comment-6986</guid>
		<description>Hi Juozas,

So, how do you inject dependencies? The way I see it you are wiring the objects manually, for example: 

&lt;code&gt;
// Create an instance of OrdersService and inject User
$service = new OrdersService();
$service-&gt;setUser($user);
$order = $service-&gt;getOrder($id);

// ... instead of ...

// Locate class and inject dependencies
$injector = new Injector();
$locator = new Locator();
$locator-&gt;setInjector($injector);

$service = $locator-&gt;getService(&#039;Orders&#039;);
$order = $service-&gt;getOrder($id);
&lt;/code&gt;

Is that how you are doing it?

Cheers</description>
		<content:encoded><![CDATA[<p>Hi Juozas,</p>
<p>So, how do you inject dependencies? The way I see it you are wiring the objects manually, for example: </p>
<p><code><br />
// Create an instance of OrdersService and inject User<br />
$service = new OrdersService();<br />
$service-&gt;setUser($user);<br />
$order = $service-&gt;getOrder($id);</p>
<p>// ... instead of ...</p>
<p>// Locate class and inject dependencies<br />
$injector = new Injector();<br />
$locator = new Locator();<br />
$locator-&gt;setInjector($injector);</p>
<p>$service = $locator-&gt;getService('Orders');<br />
$order = $service-&gt;getOrder($id);<br />
</code></p>
<p>Is that how you are doing it?</p>
<p>Cheers</p>
]]></content:encoded>
	</item>
</channel>
</rss>
