<?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>Juozas devBlog &#187; Programming</title>
	<atom:link href="http://dev.juokaz.com/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Mon, 22 Mar 2010 10:48:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Service Layer in Web applications</title>
		<link>http://dev.juokaz.com/programming/service-layer-in-web-applications</link>
		<comments>http://dev.juokaz.com/programming/service-layer-in-web-applications#comments</comments>
		<pubDate>Thu, 26 Nov 2009 16:53:47 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[acl]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[dao]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[matthew]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[permissions]]></category>
		<category><![CDATA[saas]]></category>
		<category><![CDATA[users]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[zf]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=888</guid>
		<description><![CDATA[In my professional live I mostly work with enterprise web applications which are quite demanding for big layer of business logic (that&#8217;s another article I guess) and decoupling of application layers. During this year I invested quite a lot for a search of a good ways to architecture a big application and make it simply [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/11/ServiceLayerSketch.gif"><img class="alignnone size-full wp-image-889" title="Service layer sketch" src="http://dev.juokaz.com/wp-content/uploads/2009/11/ServiceLayerSketch.gif" alt="Service Layer Sketch" width="199" height="229" style="float: right;" /></a>In my professional live I mostly work with enterprise web applications which are quite demanding for big layer of business logic (that&#8217;s another article I guess) and decoupling of application layers. During this year I invested quite a lot for a search of a good ways to architecture a big application and make it simply good. Quite a while ago <a href="http://weierophinney.net/matthew/">Matthew Weier O&#8217;Phinney</a> introduced service layer in one of his great talks about models, since then service layer become one of the key architectural component one my applications. Here I&#8217;m going to show a few examples and use cases where it&#8217;s very useful. </p>
<h5>&#8220;Old-style&#8221; interaction with data</h5>
<p>I&#8217;ve used it for different projects, but one of the best examples of how great this concept is <a href="http://en.wikipedia.org/wiki/Software_as_a_service">SaaS</a> or any other users-based application. Some years ago I used to have code which worked like this (let&#8217;s say this is controller action):</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> userInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$userDao</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$userDao</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And then in my database access class <em>Users</em> I just execute sql query with a given user id. However, after some thinking I looked at it again and though &#8211; why does controller need to know the userId? I mean, of course it&#8217;s a job of controller to process requests and control application flow, but logically &#8211; if an action is named <em>userInfo</em> and we got to the point where we need the user info (hence the user is authenticated and validated) why do we need to pass user id? It&#8217;s clear that some part of code already knows it.</p>
<p>One more case: if a site is a e-commerce it&#8217;s clear that user has only access to his orders, addresses information etc. but in a <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">basic MVC</a> you either fetch by <em>Id</em> and then check if it&#8217;s in fact user&#8217;s order or create a method like in a first example. Again, not very clear and not easy to maintain. There are more problems too: by passing user id and id of a record you assume that controller knows that this is a key to get the information. But it&#8217;s wrong &#8211; business layer knows that user has orders; controller only knows that there is such a thing like orders and it can be retrieved by id. <a href="http://www.mikebernat.com/blog/MVC_-_Fat_Models_and_Skinny_Controllers_">That&#8217;s it</a>.</p>
<h5>Service layer</h5>
<p>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. So previous method becomes to:</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> userInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$service</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UsersService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$service</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>of course you would have a separate method to fetch other users data, but for sake of simplicity let&#8217;s just say that this method returns some private info (like address for example). Here my action is completely unaware of what user id actually is &#8211; it expects a user object, it gets it. Very simple, very clean and very easy to maintain. </p>
<p>Getting back to the e-commerce example, action for a view order would look like this:</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> viewOrder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$service</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> OrdersService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$order</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$service</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOrder</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again &#8211; this controller action doesn&#8217;t care what user id is in current session, it just gets an order by its id. If this action returns false (or throws exception with error type) then it means order cannot be retrieved, or in other words &#8211; service cannot return id by given id. It can be permissions problem, it can be something else &#8211; but controller is completely freed from checking all this unnecessary things.</p>
<h5>Practical usage</h5>
<p>As you might have noticed, service layer is intermediate layer between models and controllers &#8211; in the same way as you would use Flickr or <a href="http://code.google.com/apis/youtube/overview.html">Youtube API</a> to work with remote data, you use service layer API to work with application resources. All the business logic resides in service layer, where also using other service layer, models are retrieved, changed, saved, returned etc. Controller has zero lines which contain a word <em>Doctrine</em> (or any other database layer class). None.</p>
<p><img src="http://dev.juokaz.com/wp-content/uploads/2009/11/soa-additional-service-layer.jpg" alt="Service layer " title="Service layer " width="450" height="322" class="alignnone size-full wp-image-905" /></p>
<p>Another advantage of using a service layer &#8211; it&#8217;s a good place to merge information sources. I think it&#8217;s more like a style of mine, but I tend to have models very clean &#8211; only with logic on that model. This is mainly because I usually use <a href="http://dev.juokaz.com/tag/doctrine">Doctrine</a> models and I don&#8217;t want to put anything in them. For example just yesterday Adam from <em>jazzslider.org</em> posted a very good article about <a href="http://www.phpquebec.org/modules/news/article.php?storyid=103">using Acl with models</a> by creating custom listeners. With all respect, even though it works really great, I don&#8217;t think it&#8217;s a clean approach &#8211; I see models and permissions control as separate layers.</p>
<p>Furthermore, having this layer makes replacing database layer (or even models layer) a little bit easier &#8211; because all the other code communicates with data using given <a href="http://en.wikipedia.org/wiki/Application_programming_interface">API</a> (from service layer) so as long as results returned are the same, they don&#8217;t care how they are actually retrieved (for example they can come from cache, text file or created on-the-fly). But if you would have <a href="http://framework.zend.com/manual/en/zend.db.select.html">Zend_Db_Select</a> calls all other the place, migrating to Doctrine&#8217;s <a href="http://www.doctrine-project.org/documentation/manual/1_0/en/dql-doctrine-query-language">DQL</a> can be a pain. From my personal experience, I successfully migrated my own ORM code with about 50 models to Doctrine in about 4 days without changing a line in controllers (also because of tests I had).</p>
<p>To be honest, I&#8217;ve only tried various service layer implementations with <a href="http://framework.zend.com/">Zend Framework</a>. Even a default autoloader has a resource namespace <em>Service_</em> with <em>services</em> folder inside application, so I didn&#8217;t need to do any hacking to get it working. Nevertheless, this pattern doesn&#8217;t require any specific framework futures, but if a framework has good dependency injector (like <a href="http://components.symfony-project.org/dependency-injection/">this</a> one from Symfony) it can make things even cleaner. </p>
<h5>Conclusion</h5>
<p>I don&#8217;t know if I have convinced you to look at this pattern, but I definitely recommend looking at it. Especially when your application gets quite big and you need some sort of functionality to work with all these models (on average, I used service layer with applications having roughly 80 models). Nevertheless, there are tons of different ways to do this, so I definitely recommend reading a book by M. Fowler called <a href="http://martinfowler.com/eaaCatalog/">&#8220;Patterns of Enterprise Application Architecture (P of EAA)&#8221;</a>. One of the best sources for enterprise applications I&#8217;ve read so far.</p>
<p><em><sup>*</sup> Image copyright: <a href="http://martinfowler.com/eaaCatalog/serviceLayer.html">http://martinfowler.com/eaaCatalog/serviceLayer.html</a> and <a href="http://www.tutorialspoint.com/images/soa-additional-service-layer.jpg">http://www.tutorialspoint.com/images/soa-additional-service-layer.jpg</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/programming/service-layer-in-web-applications/feed</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Free Microsoft products? Yes</title>
		<link>http://dev.juokaz.com/programming/free-microsoft-products-yes</link>
		<comments>http://dev.juokaz.com/programming/free-microsoft-products-yes#comments</comments>
		<pubDate>Mon, 02 Mar 2009 20:40:01 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dreamspark]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[myed]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[no charge]]></category>
		<category><![CDATA[online]]></category>
		<category><![CDATA[openid]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[staff]]></category>
		<category><![CDATA[students]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=305</guid>
		<description><![CDATA[I&#8217;m not fan of Microsoft, but I&#8217;m not hater too. I stand in neutral position &#8211; I use Linux in my computer, but sometimes I use Windows for Photoshop, I&#8217;m programming using NetBeans, but sometimes I do it in Visual Studio (I&#8217;m preparing for C# and ASP.NET work).
When I started programming in C# some months [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not fan of <a href="http://www.microsoft.com">Microsoft</a>, but I&#8217;m not hater too. I stand in neutral position &#8211; I use Linux in my computer, but sometimes I use Windows for Photoshop, I&#8217;m programming using <a href="http://dev.juokaz.com/programming/netbeans-7-hands-on-experience">NetBeans</a>, but sometimes I do it in Visual Studio (I&#8217;m preparing for C# and ASP.NET work).</p>
<p>When I started programming in C# some months ago, I chose Express edition tool-kit, because it&#8217;s free and quite lightweight. But absolutely accidentally I ran into <a href="https://www.dreamspark.com/default.aspx">Microsoft DreamSpark</a> program. They introduce it by:</p>
<blockquote><p><span id="ctl00_ContentPlaceHolder1_Span_Default_Content">DreamSpark is simple, it&#8217;s all about giving students Microsoft professional-level developer and design tools <strong>at no charge</strong> so you can chase your dreams and create the next big breakthrough in technology &#8211; or just get a head start on your career.</span></p></blockquote>
<p><a href="http://www.dreamspark.com"><img class="alignnone size-full wp-image-309" title="DreamSpark" src="http://dev.juokaz.com/wp-content/uploads/2009/03/dreamspark_header.jpg" alt="DreamSpark" width="506" height="292" /></a></p>
<p>At <strong>no charge</strong>? Yes, that&#8217;s right &#8211; all students can download various developer tools for free. List includes:</p>
<ul>
<li>Visual Studio 2008 Professional Edition</li>
<li>Windows Server 2008 Standard</li>
<li>SQL Server 2008 Developer</li>
<li>Expression Studio 2</li>
<li>and more</li>
</ul>
<p>I don&#8217;t know how student verification works in other universities, but my university  (<a href="http://www.ed.ac.uk">The University of Edinburgh</a>) has special login system which works as authentication gateway. Somehow Microsoft arranged it and I verified myself by using that system &#8211; process similar to using <a href="http://openid.net/">OpenID</a>. Process took only some minutes and downloads where going.</p>
<p>It used to be that Microsoft programming languages were criticized because they where only available to use with IDE&#8217;s which cost a lot. And for starters, high-school or university students it was almost impossible to start programming in for example C#. They are not cheap now too, but now everyone can use Express versions and students can even get profession-level tools.</p>
<p>Isn&#8217;t it great? It may sound as Microsoft advertisement, but as student I really like this initiative. I&#8217;m not going to start using Windows again because of that, but the fact that Microsoft is thinking about students as a potential Microsoft technologies users and investing in them is really great.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/programming/free-microsoft-products-yes/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NetBeans 7.0 hands-on experience</title>
		<link>http://dev.juokaz.com/programming/netbeans-7-hands-on-experience</link>
		<comments>http://dev.juokaz.com/programming/netbeans-7-hands-on-experience#comments</comments>
		<pubDate>Fri, 13 Feb 2009 17:54:21 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[artifacts]]></category>
		<category><![CDATA[beta]]></category>
		<category><![CDATA[code-completion]]></category>
		<category><![CDATA[crashes]]></category>
		<category><![CDATA[fast]]></category>
		<category><![CDATA[line size]]></category>
		<category><![CDATA[milestone]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[slow]]></category>
		<category><![CDATA[trying]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=131</guid>
		<description><![CDATA[NetBeans 7.0 Milestone 1 was released about two months ago, and final version should reach us (maybe) April or May. Because with previous, 6.5, version I had some problems, I decided to try new version. Compared to NetBeans 6.5 (version 20081110) I find some differences, which today I&#8217;m going to show you.
To start with, 7th [...]]]></description>
			<content:encoded><![CDATA[<p>NetBeans 7.0 Milestone 1 was released about two months ago, and final version should reach us (<a href="http://www.netbeans.org/community/releases/roadmap.html">maybe</a>) April or May. Because with previous, 6.5, version I had <a href="http://dev.juokaz.com/programming/netbeans-65-my-number-one-php-ide">some problems</a>, I decided to try new version. Compared to NetBeans 6.5 (version 20081110) I find some differences, which today I&#8217;m going to show you.</p>
<p>To start with, 7th version feels much more solid -<strong> speed has increased</strong>. Code-completion, moving between files, file opening, etc. is now much more fluent. I use both version with standard configuration, so NetBeans must have made some major improvements. IDE now feels lighter and better to use.</p>
<p><strong>Code indentation</strong> seems to be <strong>fixed</strong>/changed. 6.5 version somehow does it wrong and even simple text editor like gedit (default for Ubuntu) cannot understand it. Upcoming version fixed this problem and now NetBeans edited files are displayed correctly in all cases. Also, automatic source formatting seems to work just fine (haven&#8217;t used it in 6.5).</p>
<p><strong>Editor</strong> now <strong>correctly</strong> works with lower than default <strong>line size</strong>. When I installed 6.5 version it was using double line size and decreasing it to normal size revealed a lot of problems. It&#8217;s very hard to explain these bugs, but briefly said, editor started to create <a href="http://en.wikipedia.org/wiki/Artifact_(error)">artifacts</a> when typing text.</p>
<p>Because it&#8217;s only M1 version, sometimes it <strong>crashes</strong>. Most of the times it have frozen-up when trying to show code completion. But in two days I had only 3 crashes (for more than 20 hours of work) so it&#8217;s not big issue. Stable version will probably work very stable (it should be, if they call it stable :).</p>
<p>To finish with, I think it&#8217;s ready enough. Now I have 3 versions installed (6.1, 6.5, 7.0 M1) but most of the times I use newest version. Crashes annoys, but if you are not very nervous, they doesn&#8217;t create a lot of problems.<em> Probably, problems I had, have happened only for me.</em></p>
<p>Download it <a href="http://bits.netbeans.org/netbeans/7.0/m1/">here</a> and let me know what problems or pleasures you&#8217;ve encountered. In my opinion, NetBeans team haven&#8217;t stopped working after very good 6.5 release.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/programming/netbeans-7-hands-on-experience/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>NetBeans 6.5 &#8211; my number one PHP IDE</title>
		<link>http://dev.juokaz.com/programming/netbeans-65-my-number-one-php-ide</link>
		<comments>http://dev.juokaz.com/programming/netbeans-65-my-number-one-php-ide#comments</comments>
		<pubDate>Sun, 08 Feb 2009 18:11:36 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mike]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[versioning]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=48</guid>
		<description><![CDATA[I find more more articles about NetBeans 6.5 IDE for PHP, for example this Mike&#8217;s article. Why people like it so much? After some weeks spent reading all these articles, I finally decided to throw away all other editors and try new-and-shiny NetBeans.
Initial configuration (keyboard shortcuts, SVN config, project setup, etc.) took only 5 min [...]]]></description>
			<content:encoded><![CDATA[<p>I find more more articles about <a href="http://www.netbeans.org/">NetBeans</a> 6.5 IDE for PHP, for example this <a href="http://www.mikeborozdin.com/post/NetBeans-65-as-a-Cute-and-free-IDE-for-PHP.aspx">Mike&#8217;s article</a>. Why people like it so much? After some weeks spent reading all these articles, I finally decided to throw away all other editors and try new-and-shiny NetBeans.</p>
<p>Initial configuration (keyboard shortcuts, SVN config, project setup, etc.) took only 5 min or so, and IDE was ready. As other people, I like it more more after each work hour spent. It just feels good to work.</p>
<p><strong>What I like about NetBeans</strong>?</p>
<ol>
<li>Free</li>
<li>Integrated SVN support</li>
<li>Projects (with changed files upload)</li>
<li>Debugging and in-line syntax checking</li>
<li>Code completion (PHP, JavaScript (with jQuery!!!), HTML, etc.)</li>
<li>Works in Windows and Linux</li>
</ol>
<p>What I dislike about NetBeans?</p>
<ol>
<li>No integrated project synchronization over FTP (s/FTP)</li>
<li>Overall performance not very good*</li>
<li>haven&#8217;t found more yet</li>
</ol>
<p><em>* Largest project (100k lines) really slows down IDE and switching between files/code completion creates delays</em></p>
<p>Overall it&#8217;s just a wonderful tool &#8211; stable, helpfull and being actively developed. Give it a try!</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/programming/netbeans-65-my-number-one-php-ide/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
