<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="http://www.stefanoricciardi.com/feed.xml" rel="self" type="application/atom+xml" /><link href="http://www.stefanoricciardi.com/" rel="alternate" type="text/html" /><updated>2022-06-27T12:54:20+00:00</updated><id>http://www.stefanoricciardi.com/feed.xml</id><title type="html">Stefano Ricciardi</title><subtitle>Stefano Ricciardi on Software Development</subtitle><author><name>Stefano Ricciardi</name></author><entry><title type="html">Book Review: Version Control With Git</title><link href="http://www.stefanoricciardi.com/2014/08/10/book-review-version-control-with-git/" rel="alternate" type="text/html" title="Book Review: Version Control With Git" /><published>2014-08-10T12:55:13+00:00</published><updated>2014-08-10T12:55:13+00:00</updated><id>http://www.stefanoricciardi.com/2014/08/10/book-review-version-control-with-git</id><content type="html" xml:base="http://www.stefanoricciardi.com/2014/08/10/book-review-version-control-with-git/">&lt;p&gt;&lt;em&gt;Your first encounter with GIT as a version control tool may be quite intimidating, especially if all you had been using so far was something simpler like Subversion, TFS or, God forbid, Visual Source Safe. &quot;&lt;a title=&quot;Version Control with GIT&quot; href=&quot;http://shop.oreilly.com/product/0636920022862.do&quot;&gt;Version Control with GIT&lt;/a&gt;&quot; by Jon Loeliger, Matthew McCullough (published by O'Reilly) leads you on a journey to become a master of the tool. But the path is not short and easy. If you need quick gratification, then you might have to look elsewhere.&lt;br /&gt;
&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://shop.oreilly.com/product/0636920022862.do&quot;&gt;&lt;img src=&quot;/assets/cat.gif&quot; alt=&quot;Version Control with GIT&quot; width=&quot;180&quot; height=&quot;236&quot; /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;h3&gt;Do I Need a Book on GIT?&lt;/h3&gt;
&lt;p&gt;There a plenty of GIT related resources online, and more get added the more it is adopted even outside of the open source world. If your use of GIT is just only to share your own pet projects on GitHub or to occasionally fork an open source repository, make a few changes and submit a pull request, then probably a brief list of commands with some basic concepts will get you covered.&lt;/p&gt;
&lt;p&gt;On the other hand, if to use GIT on a daily basis at work, or work on a complex open source project with dozens of contributors and hundreds of commits, then you need to go a bit deeper and understand the nuts and bolts.&lt;/p&gt;
&lt;h3&gt;What's In The Book&lt;/h3&gt;
&lt;p&gt;After touching briefly on the installation process, the first dozen of chapters deal exclusively with one (local) repository: the object model, the index file, commits, branches, diffs and the all important concept of merging. Sometimes the description is so accurate that you can almost visualize the software data structures underlying the design concepts the author is guiding you through; but this is not just a technical curiosity to entertain the reader: everything that is discussed has got practical implications on your usage of the tool (e.g. which merge algorithm you should choose, or whether to opt for a 'rebase' versus a 'cherry-pick').&lt;/p&gt;
&lt;p&gt;The book then devotes a couple of chapters covering how to interact with remote repositories, and then tackles more advanced topics like repository management,  patching, hooks, submodules, interacting with SVN and a few other tip and tricks. It finally mentions GitHub in the last chapter.&lt;/p&gt;
&lt;h3&gt;The Good and the Bad&lt;/h3&gt;
&lt;p&gt;If you need detailed information about GIT, then this is the deal (short of reading the source code I guess). Every aspect of the tool is explained (sometimes in excruciating detail) and the authors go to a great length to provide tips and gotchas on commands (especially when you might easily shoot yourself in the foot). However this is not a quick read and it will take time to digest it.&lt;/p&gt;
&lt;p&gt;What's missing, maybe, it's a 'quick start' chapter at the beginning to get you going if you need to be up and running in a short amount of time (say you have landed a new job where GIT is in use and you have little or no experience). That would help with the transition while giving you more time to go through the details. Unfortunately, the reader will have to look for such a compendium elsewhere.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</content><author><name>Stefano Ricciardi</name></author><category term="Reviews" /><summary type="html">Your first encounter with GIT as a version control tool may be quite intimidating, especially if all you had been using so far was something simpler like Subversion, TFS or, God forbid, Visual Source Safe. &quot;Version Control with GIT&quot; by Jon Loeliger, Matthew McCullough (published by O'Reilly) leads you on a journey to become a master of the tool. But the path is not short and easy. If you need quick gratification, then you might have to look elsewhere. Do I Need a Book on GIT? There a plenty of GIT related resources online, and more get added the more it is adopted even outside of the open source world. If your use of GIT is just only to share your own pet projects on GitHub or to occasionally fork an open source repository, make a few changes and submit a pull request, then probably a brief list of commands with some basic concepts will get you covered. On the other hand, if to use GIT on a daily basis at work, or work on a complex open source project with dozens of contributors and hundreds of commits, then you need to go a bit deeper and understand the nuts and bolts. What's In The Book After touching briefly on the installation process, the first dozen of chapters deal exclusively with one (local) repository: the object model, the index file, commits, branches, diffs and the all important concept of merging. Sometimes the description is so accurate that you can almost visualize the software data structures underlying the design concepts the author is guiding you through; but this is not just a technical curiosity to entertain the reader: everything that is discussed has got practical implications on your usage of the tool (e.g. which merge algorithm you should choose, or whether to opt for a 'rebase' versus a 'cherry-pick'). The book then devotes a couple of chapters covering how to interact with remote repositories, and then tackles more advanced topics like repository management,  patching, hooks, submodules, interacting with SVN and a few other tip and tricks. It finally mentions GitHub in the last chapter. The Good and the Bad If you need detailed information about GIT, then this is the deal (short of reading the source code I guess). Every aspect of the tool is explained (sometimes in excruciating detail) and the authors go to a great length to provide tips and gotchas on commands (especially when you might easily shoot yourself in the foot). However this is not a quick read and it will take time to digest it. What's missing, maybe, it's a 'quick start' chapter at the beginning to get you going if you need to be up and running in a short amount of time (say you have landed a new job where GIT is in use and you have little or no experience). That would help with the transition while giving you more time to go through the details. Unfortunately, the reader will have to look for such a compendium elsewhere. &amp;nbsp; &amp;nbsp;</summary></entry><entry><title type="html">Firefox, Fiddler and HTTPS</title><link href="http://www.stefanoricciardi.com/2014/01/17/firefox-fiddler-and-https/" rel="alternate" type="text/html" title="Firefox, Fiddler and HTTPS" /><published>2014-01-17T09:39:23+00:00</published><updated>2014-01-17T09:39:23+00:00</updated><id>http://www.stefanoricciardi.com/2014/01/17/firefox-fiddler-and-https</id><content type="html" xml:base="http://www.stefanoricciardi.com/2014/01/17/firefox-fiddler-and-https/">&lt;p&gt;Today I was debugging some interaction between one of our products and Twitter 1.1 API protected by OAuth 1.0a . As it happens, I was using Fiddler to have a look at tokens being passed back and forth.&lt;/p&gt;
&lt;p&gt;As I finished my debug session, I forgot to turn off Fiddler session and wanted to check my emails on gmail. I was getting warnings by Firefox about google SSL certificate chain not being valid. Strange. Then I moved to Facebook and there I also had all kind of problems. After adding an exception for Facebook.com to the SSL trust (!) I was able to open the site, but as text only without any additional resource (images, css): you can imagine what Facebook site looked like. I could not even browse to support.mozilla.org because Firefox did not even trust that side (ironically enough...)!&lt;/p&gt;
&lt;p&gt;After a white it dawned on my that Fiddler might be the culprit: surely enough, after turning off Fiddler everything went back to normal.&lt;/p&gt;
&lt;p&gt;So the moral of the story is: &lt;strong&gt;if you see sudden problems with Firefox and HTTPS, be sure that you don't have Fiddler running in the background causing havoc&lt;/strong&gt;. :) &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: Eric in the comments suggests how to configure Firefox to accept Fiddler's certificate (see &lt;a href=&quot;http://fiddler2.com/blog/blog/2013/04/01/configuring-firefox-for-fiddlerhttp://&quot; title=&quot;http://fiddler2.com/blog/blog/2013/04/01/configuring-firefox-for-fiddler&quot;&gt;http://fiddler2.com/blog/blog/2013/04/01/configuring-firefox-for-fiddler&lt;/a&gt;)&lt;/p&gt;</content><author><name>Stefano Ricciardi</name></author><category term="Misc" /><category term="Tools" /><category term="Fiddler" /><category term="Firefox" /><category term="HTTPS" /><category term="Tools" /><summary type="html">Today I was debugging some interaction between one of our products and Twitter 1.1 API protected by OAuth 1.0a . As it happens, I was using Fiddler to have a look at tokens being passed back and forth. As I finished my debug session, I forgot to turn off Fiddler session and wanted to check my emails on gmail. I was getting warnings by Firefox about google SSL certificate chain not being valid. Strange. Then I moved to Facebook and there I also had all kind of problems. After adding an exception for Facebook.com to the SSL trust (!) I was able to open the site, but as text only without any additional resource (images, css): you can imagine what Facebook site looked like. I could not even browse to support.mozilla.org because Firefox did not even trust that side (ironically enough...)! After a white it dawned on my that Fiddler might be the culprit: surely enough, after turning off Fiddler everything went back to normal. So the moral of the story is: if you see sudden problems with Firefox and HTTPS, be sure that you don't have Fiddler running in the background causing havoc. :) UPDATE: Eric in the comments suggests how to configure Firefox to accept Fiddler's certificate (see http://fiddler2.com/blog/blog/2013/04/01/configuring-firefox-for-fiddler)</summary></entry><entry><title type="html">Dependency Injection for Dummies</title><link href="http://www.stefanoricciardi.com/2011/11/08/dependency-injection-for-dummies/" rel="alternate" type="text/html" title="Dependency Injection for Dummies" /><published>2011-11-08T10:10:31+00:00</published><updated>2011-11-08T10:10:31+00:00</updated><id>http://www.stefanoricciardi.com/2011/11/08/dependency-injection-for-dummies</id><content type="html" xml:base="http://www.stefanoricciardi.com/2011/11/08/dependency-injection-for-dummies/">&lt;p&gt;&lt;em&gt;&lt;a href=&quot;http://codecriticon.com/&quot;&gt;Antonio Vidal&lt;/a&gt; has translated this post into Spanish: you can find it 
&lt;a href=&quot;http://codecriticon.com/dependencia-inyeccion-dummies/&quot;&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Dependency injection is a very simple concept: if you have an object that interacts with other 
  objects the responsibility of finding a reference to those objects at run time is moved outside of the object itself.&lt;/p&gt;
&lt;p&gt;What does it mean for an object to &amp;quot;interact&amp;quot; with other objects? Generally it means 
  invoking methods or reading properties from those objects. So if we have a class &lt;code&gt;A&lt;/code&gt; that invokes method &lt;code&gt;Calculate&lt;/code&gt; on class &lt;code&gt;B&lt;/code&gt;, we can say that &lt;code&gt;A&lt;/code&gt; interacts with &lt;code&gt;B&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the following example we show class &lt;code&gt;A&lt;/code&gt; interacting with class &lt;code&gt;B&lt;/code&gt;. 
We can equally say that &lt;class&gt;A&lt;/class&gt; &lt;i&gt;depends&lt;/i&gt; on class &lt;code&gt;B&lt;/code&gt; to fulfill a responsibility. 
In this case, it not only invokes its method &lt;code&gt;Calculate&lt;/code&gt; but it also creates a new instance of that class.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
   
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SomeMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Calculate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt; 

&lt;p&gt;In the following example, on the other side, the responsibility of getting a reference to an implementation of 
  a class of type &lt;code&gt;B&lt;/code&gt; is moved &lt;i&gt;outside&lt;/i&gt; of &lt;code&gt;A&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SomeMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Calculate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In this case we say that a dependency (&lt;code&gt;B&lt;/code&gt;) has been &lt;i&gt;injected&lt;/i&gt; into &lt;code&gt;A&lt;/code&gt;, via the constructor. 
Of course, you can also inject dependencies via a property (or even a regular method), like in the following example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt; 
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
     &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SomeMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;RetrieveValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; 
        &lt;span class=&quot;c1&quot;&gt;// HANDLE THIS ERROR CASE&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So this is all there is about dependency injection. Everything else just builds on this core concept.&lt;/p&gt;
&lt;p&gt;Like for example Inversion Of Control (IoC) tools which helps you wiring together your objects at run time, injecting all dependencies as needed. So what exactly is Inversion of Control and how does it relate to Dependency Injection (DI)?&lt;/p&gt;
&lt;p&gt;I like to associate IoC to the &lt;a href=&quot;http://en.wikipedia.org/wiki/Hollywood_Principle&quot;&gt;Hollywood Principle&lt;/a&gt;: &amp;quot;&lt;em&gt;Don&amp;#39;t call us, we&amp;#39;ll call you&lt;/em&gt;&amp;quot;. IoC is a design principle where &lt;quote&gt;reusable generic code controls the execution of problem-specific code&lt;/quote&gt;: it is a characteristic of many frameworks, where the application is built extending or customizing a common skeleton; you put your own classes at specific points and the framework will call you when needed.&lt;/p&gt;
&lt;p&gt;You can use an IoC container as a framework to perform Dependency Injection on your behalf: you tell the container which are the concrete implementation classes for your dependencies and the container will make sure that your constructors or setters will be called with the right objects.&lt;/p&gt;
&lt;p&gt;Therefore, IoC containers are just a &lt;i&gt;convenience&lt;/i&gt; to simplify how dependency injection is handled. But even if you don&amp;#39;t use one you could still manually perform dependency injection.&lt;/p&gt;
&lt;p&gt;(If you want to have a look at how an IoC container works you can jump to my &lt;a href=&quot;/2011/01/21/ninject-mini-tutorial-part-1/&quot;&gt;mini tutorial on Ninject&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Why is the concept of dependency injection important? Because by applying it, you simplify your design (separating the responsibility of using an object from the responsibility of retrieving that object) and your code becomes much easier to test, since you can mock out the dependencies substituting them with fake (stub) objects. But that is the subject for another post.&lt;/p&gt;</content><author><name>Stefano Ricciardi</name></author><category term=".NET" /><category term="C#" /><category term="Design and Patterns" /><category term="agile" /><category term="C#" /><category term="Design and Patterns" /><category term="IoC" /><summary type="html">Antonio Vidal has translated this post into Spanish: you can find it here. Dependency injection is a very simple concept: if you have an object that interacts with other objects the responsibility of finding a reference to those objects at run time is moved outside of the object itself. What does it mean for an object to &amp;quot;interact&amp;quot; with other objects? Generally it means invoking methods or reading properties from those objects. So if we have a class A that invokes method Calculate on class B, we can say that A interacts with B. In the following example we show class A interacting with class B. We can equally say that A depends on class B to fulfill a responsibility. In this case, it not only invokes its method Calculate but it also creates a new instance of that class.</summary></entry><entry><title type="html">Why Functional Programming Matters in F#</title><link href="http://www.stefanoricciardi.com/2011/07/16/whyfunctionalprogrammingmattersinfsharp/" rel="alternate" type="text/html" title="Why Functional Programming Matters in F#" /><published>2011-07-16T18:46:12+00:00</published><updated>2011-07-16T18:46:12+00:00</updated><id>http://www.stefanoricciardi.com/2011/07/16/whyfunctionalprogrammingmattersinfsharp</id><content type="html" xml:base="http://www.stefanoricciardi.com/2011/07/16/whyfunctionalprogrammingmattersinfsharp/">&lt;p&gt;&lt;title&gt;Building Reduce and Map from the Ground Up with F#&lt;/title&gt;&lt;/p&gt;
&lt;p&gt;Today I've found myself reading again the excellent paper 
  &lt;a href=&quot;/blog/wp-content/uploads/2011/07/WhyFunctionalProgrammingMatters.pdf&quot;&gt;Why Functional Programming 
  Matters&lt;/a&gt;, where the author describes the core activity of programming in a functional fashion in terms 
  of &amp;quot;glueing functions together&amp;quot;.&lt;/p&gt;
&lt;p&gt;It's been a while since I last played around with F#. So I decided this would be a good time 
  to refresh some of the core concepts by translating some of the examples from his paper into F#.&lt;/p&gt;
&lt;h2&gt;Reduce&lt;/h2&gt;
&lt;p&gt;The paper starts off describing how a &lt;code&gt;reduce&lt;/code&gt; function could be devised, 
extrapolating a common behavior from a certain set of functions. Let's first consider a function 
to sum all the items in a list:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-f#&quot; data-lang=&quot;f#&quot;&gt;let add a b = a + b
  
let rec sum l =
    match l with
    | [] -&amp;gt; 0
    | h::t -&amp;gt; add h (sum t)&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;Nothing too fancy going on here. Let's now consider a similar function that 
  instead multiplies together all the items in a list:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-f#&quot; data-lang=&quot;f#&quot;&gt;let mul a b = a * b 
    let rec product l =
        match l with
        | [] -&amp;gt; 1
        | h::t -&amp;gt; mul h (product t)&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If you compare the structure of the two functions &lt;code&gt;sum&lt;/code&gt; and &lt;code&gt;product&lt;/code&gt; 
you can see that the two are very similar. The are only two differences between the two:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value being returned when an empty list is matched (0 vs 1).&lt;/li&gt;
&lt;li&gt;The function being applied between the head of the list and the application 
  of the recursive function to the remaining of the list (&lt;code&gt;add&lt;/code&gt; vs &lt;code&gt;mul&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It's easy, then, to extrapolate a more general &lt;code&gt;reduce&lt;/code&gt; function, as follows:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-f#&quot; data-lang=&quot;f#&quot;&gt;let rec reduce f a l =
        match l with 
        | [] -&amp;gt; a
        | h::t -&amp;gt; f h ((reduce f a) t)&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Let's consider all the pieces step by step:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;reduce&lt;/code&gt; is a (recursive) function accepting a function &lt;code&gt;f&lt;/code&gt;, a value &lt;code&gt;a&lt;/code&gt; and a list &lt;code&gt;l&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;f&lt;/code&gt; is the function that we want to apply between two elements of the list (&lt;code&gt;sum&lt;/code&gt; or &lt;code&gt;mul&lt;/code&gt; in the two examples above)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;a&lt;/code&gt; is the value that we want to return when &lt;code&gt;f&lt;/code&gt; is applied to the empty list (0 or 1 respectively), and&lt;/li&gt;
&lt;li&gt;&lt;code&gt;l&lt;/code&gt; is the actual list we want to operate on.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let's try this new function in the interactive window:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-f#&quot; data-lang=&quot;f#&quot;&gt;&amp;gt; reduce;;
val it : (('a -&amp;gt; 'b -&amp;gt; 'b) -&amp;gt; 'b -&amp;gt; 'a list -&amp;gt; 'b)
&amp;gt; reduce add 0 [1;2;3];;
val it : int = 6
&amp;gt; reduce mul 1 [1;2;3];;
val it : int = 6&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you see, the compiler infers the types involved in &lt;code&gt;reduce&lt;/code&gt; as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;('a -&gt; 'b -&gt; 'b)&lt;/code&gt; is the description of f: a function that takes two arguments of type 'a and 'b and 
returns one item of type 'b (note that 'a and 'b may in some cases represent the same type, as with &lt;code&gt;add&lt;/code&gt; 
and &lt;code&gt;mul&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;'b&lt;/code&gt; is a parameter of the same type as the one returned by &lt;code&gt;f&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;'a list&lt;/code&gt;is a list of items the same type as the first argument of &lt;code&gt;f&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;'b&lt;/code&gt; is finally the return type of the whole reduction process&lt;/li&gt;
&lt;p&gt;	.&lt;/ul&gt;
&lt;p&gt;There's an interesting special case: when &lt;code&gt;'b&lt;/code&gt; is &lt;code&gt;'a list&lt;/code&gt;. 
That is, when &lt;code&gt;f&lt;/code&gt; has the following form: ('a -&gt; a' list -&gt; a' list). 
A function receiving an item and a list and returning another list. Can you think of any function like that? 
What about concatenating an item and a list?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-f#&quot; data-lang=&quot;f#&quot;&gt;let cons a l = a::l&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Let's see how the F# interactive interprets the new function we've just created and an example of usage:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-f#&quot; data-lang=&quot;f#&quot;&gt;&amp;gt; cons;;
val it : ('a -&amp;gt; 'a list -&amp;gt; 'a list)
&amp;gt; cons 1 [2; 3];;
val it : int list = [1; 2; 3]
&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And we now consider &lt;code&gt;cons&lt;/code&gt; as our &lt;code&gt;f&lt;/code&gt; function, 
first parameter of the &lt;code&gt;reduce&lt;/code&gt;, we can write for example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-f#&quot; data-lang=&quot;f#&quot;&gt;let append a b = reduce cons b a
    let copy l = reduce cons l [] &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;where &lt;code&gt;append&lt;/code&gt; concatenates two lists and &lt;code&gt;copy&lt;/code&gt; 
simply replicates any given list:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-f#&quot; data-lang=&quot;f#&quot;&gt;&amp;gt; append;;
val it : ('a list -&amp;gt; 'a list -&amp;gt; 'a list)
&amp;gt; append [1;2] [3;4];;
val it : int list = [1; 2; 3; 4]
&amp;gt;  copy;;
val it : ('a list -&amp;gt; 'a list)
&amp;gt; copy [1;2;3];;
val it : int list = [1; 2; 3]
&amp;gt;   	  &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;</content><author><name>Stefano Ricciardi</name></author><category term="F#" /><category term="F#" /><category term="functional" /><summary type="html">Building Reduce and Map from the Ground Up with F# Today I've found myself reading again the excellent paper Why Functional Programming Matters, where the author describes the core activity of programming in a functional fashion in terms of &amp;quot;glueing functions together&amp;quot;. It's been a while since I last played around with F#. So I decided this would be a good time to refresh some of the core concepts by translating some of the examples from his paper into F#. Reduce The paper starts off describing how a reduce function could be devised, extrapolating a common behavior from a certain set of functions. Let's first consider a function to sum all the items in a list:</summary></entry><entry><title type="html">Code Snippets for the Dispose Pattern</title><link href="http://www.stefanoricciardi.com/2011/05/25/code-snippets-for-the-dispose-pattern/" rel="alternate" type="text/html" title="Code Snippets for the Dispose Pattern" /><published>2011-05-25T15:08:34+00:00</published><updated>2011-05-25T15:08:34+00:00</updated><id>http://www.stefanoricciardi.com/2011/05/25/code-snippets-for-the-dispose-pattern</id><content type="html" xml:base="http://www.stefanoricciardi.com/2011/05/25/code-snippets-for-the-dispose-pattern/">&lt;p&gt;I thougth I'd just share a couple of Visual Studio C# code snippets to implement disposable classes, 
  based on the well known .NET &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=VS.100).aspx&quot;&gt;dispose pattern&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can download it from &lt;a href=&quot;/blog/wp-content/uploads/2011/05/DisposePattern.zip&quot;&gt;here&lt;/a&gt; or 
simply copy and past it from the listing below. Feel free to rename the shortcut to whatever you like :)&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;CodeSnippets&lt;/span&gt;  &lt;span class=&quot;na&quot;&gt;xmlns=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;CodeSnippet&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Format=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1.0.0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;Header&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Title&amp;gt;&lt;/span&gt;Base class disposable implementation.&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Title&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Shortcut&amp;gt;&lt;/span&gt;bdisp&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Shortcut&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Description&amp;gt;&lt;/span&gt;
        Code snippet to create a scheleton implementation
        of the IDisposable pattern for a base class.
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Description&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Author&amp;gt;&lt;/span&gt;Stefano Ricciardi&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Author&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;SnippetTypes&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;SnippetType&amp;gt;&lt;/span&gt;Expansion&lt;span class=&quot;nt&quot;&gt;&amp;lt;/SnippetType&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/SnippetTypes&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Header&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;Snippet&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Declarations&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;Literal&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;&amp;lt;ID&amp;gt;&lt;/span&gt;ClassName&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ID&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;&amp;lt;ToolTip&amp;gt;&lt;/span&gt;Name of the class.&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ToolTip&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;&amp;lt;Default&amp;gt;&lt;/span&gt;ClassName&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Default&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Literal&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Declarations&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Code&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Language=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;csharp&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;cp&quot;&gt;&amp;lt;![CDATA[
    public class $ClassName$: IDisposable
    {
        private bool _disposed = false;
            
        //Implement IDisposable.
        public void Dispose()
        {
          Dispose(true);
          GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
          if (!_disposed)
          {
            if (disposing)
            {
              // Free other state (managed objects).
            }
            // Free your own state (unmanaged objects).
            // Set large fields to null.
            _disposed = true;
          }
        }

        // Use C# destructor syntax for finalization code.
        ~$ClassName$()
        {
          // Simply call Dispose(false).
          Dispose(false);
        }
    }            ]]&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Code&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Snippet&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/CodeSnippet&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;CodeSnippet&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Format=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1.0.0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;Header&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Title&amp;gt;&lt;/span&gt;Base class disposable implementation.&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Title&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Shortcut&amp;gt;&lt;/span&gt;ddisp&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Shortcut&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Description&amp;gt;&lt;/span&gt;
        Code snippet to create a scheleton implementation of the
        IDisposable pattern	for a derived class.
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Description&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Author&amp;gt;&lt;/span&gt;Stefano Ricciardi&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Author&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;SnippetTypes&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;SnippetType&amp;gt;&lt;/span&gt;Expansion&lt;span class=&quot;nt&quot;&gt;&amp;lt;/SnippetType&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/SnippetTypes&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Header&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;Snippet&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Declarations&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;Literal&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;&amp;lt;ID&amp;gt;&lt;/span&gt;DerivedClassName&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ID&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;&amp;lt;ToolTip&amp;gt;&lt;/span&gt;Name of the derived class.&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ToolTip&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;&amp;lt;Default&amp;gt;&lt;/span&gt;Derived&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Default&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Literal&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;Literal&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;&amp;lt;ID&amp;gt;&lt;/span&gt;BaseClassName&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ID&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;&amp;lt;ToolTip&amp;gt;&lt;/span&gt;Name of the base class.&lt;span class=&quot;nt&quot;&gt;&amp;lt;/ToolTip&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;&amp;lt;Default&amp;gt;&lt;/span&gt;Base&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Default&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Literal&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Declarations&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Code&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Language=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;csharp&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;cp&quot;&gt;&amp;lt;![CDATA[
  public class $DerivedClassName$: $BaseClassName$
  {
      private bool _disposed = false;

      protected override void Dispose(bool disposing)
      {
          if (!_disposed)
          {
              if (disposing)
              {
                  // Release managed resources.
              }
              // Release unmanaged resources.
              // Set large fields to null.
              // Call Dispose on your base class.
              
              _disposed = true;
          }
            
          base.Dispose(disposing);
      }
      // The derived class does not have a Finalize method
      // or a Dispose method without parameters because it inherits
      // them from the base class.
  }
]]&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Code&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Snippet&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/CodeSnippet&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/CodeSnippets&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;</content><author><name>Stefano Ricciardi</name></author><category term=".NET" /><category term="C#" /><category term="C#" /><category term="snippet" /><category term="tips" /><category term="visual studio 2010" /><summary type="html">I thougth I'd just share a couple of Visual Studio C# code snippets to implement disposable classes, based on the well known .NET dispose pattern. You can download it from here or simply copy and past it from the listing below. Feel free to rename the shortcut to whatever you like :) &amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&amp;gt; &amp;lt;CodeSnippets xmlns=&quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;&amp;gt; &amp;lt;CodeSnippet Format=&quot;1.0.0&quot;&amp;gt; &amp;lt;Header&amp;gt; &amp;lt;Title&amp;gt;Base class disposable implementation.&amp;lt;/Title&amp;gt; &amp;lt;Shortcut&amp;gt;bdisp&amp;lt;/Shortcut&amp;gt; &amp;lt;Description&amp;gt; Code snippet to create a scheleton implementation of the IDisposable pattern for a base class. &amp;lt;/Description&amp;gt; &amp;lt;Author&amp;gt;Stefano Ricciardi&amp;lt;/Author&amp;gt; &amp;lt;SnippetTypes&amp;gt; &amp;lt;SnippetType&amp;gt;Expansion&amp;lt;/SnippetType&amp;gt; &amp;lt;/SnippetTypes&amp;gt; &amp;lt;/Header&amp;gt; &amp;lt;Snippet&amp;gt; &amp;lt;Declarations&amp;gt; &amp;lt;Literal&amp;gt; &amp;lt;ID&amp;gt;ClassName&amp;lt;/ID&amp;gt; &amp;lt;ToolTip&amp;gt;Name of the class.&amp;lt;/ToolTip&amp;gt; &amp;lt;Default&amp;gt;ClassName&amp;lt;/Default&amp;gt; &amp;lt;/Literal&amp;gt; &amp;lt;/Declarations&amp;gt; &amp;lt;Code Language=&quot;csharp&quot;&amp;gt; &amp;lt;![CDATA[ public class $ClassName$: IDisposable { private bool _disposed = false; //Implement IDisposable. public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Free other state (managed objects). } // Free your own state (unmanaged objects). // Set large fields to null. _disposed = true; } } // Use C# destructor syntax for finalization code. ~$ClassName$() { // Simply call Dispose(false). Dispose(false); } } ]]&amp;gt; &amp;lt;/Code&amp;gt; &amp;lt;/Snippet&amp;gt; &amp;lt;/CodeSnippet&amp;gt; &amp;lt;CodeSnippet Format=&quot;1.0.0&quot;&amp;gt; &amp;lt;Header&amp;gt; &amp;lt;Title&amp;gt;Base class disposable implementation.&amp;lt;/Title&amp;gt; &amp;lt;Shortcut&amp;gt;ddisp&amp;lt;/Shortcut&amp;gt; &amp;lt;Description&amp;gt; Code snippet to create a scheleton implementation of the IDisposable pattern for a derived class. &amp;lt;/Description&amp;gt; &amp;lt;Author&amp;gt;Stefano Ricciardi&amp;lt;/Author&amp;gt; &amp;lt;SnippetTypes&amp;gt; &amp;lt;SnippetType&amp;gt;Expansion&amp;lt;/SnippetType&amp;gt; &amp;lt;/SnippetTypes&amp;gt; &amp;lt;/Header&amp;gt; &amp;lt;Snippet&amp;gt; &amp;lt;Declarations&amp;gt; &amp;lt;Literal&amp;gt; &amp;lt;ID&amp;gt;DerivedClassName&amp;lt;/ID&amp;gt; &amp;lt;ToolTip&amp;gt;Name of the derived class.&amp;lt;/ToolTip&amp;gt; &amp;lt;Default&amp;gt;Derived&amp;lt;/Default&amp;gt; &amp;lt;/Literal&amp;gt; &amp;lt;Literal&amp;gt; &amp;lt;ID&amp;gt;BaseClassName&amp;lt;/ID&amp;gt; &amp;lt;ToolTip&amp;gt;Name of the base class.&amp;lt;/ToolTip&amp;gt; &amp;lt;Default&amp;gt;Base&amp;lt;/Default&amp;gt; &amp;lt;/Literal&amp;gt; &amp;lt;/Declarations&amp;gt; &amp;lt;Code Language=&quot;csharp&quot;&amp;gt; &amp;lt;![CDATA[ public class $DerivedClassName$: $BaseClassName$ { private bool _disposed = false; protected override void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Release managed resources. } // Release unmanaged resources. // Set large fields to null. // Call Dispose on your base class. _disposed = true; } base.Dispose(disposing); } // The derived class does not have a Finalize method // or a Dispose method without parameters because it inherits // them from the base class. } ]]&amp;gt; &amp;lt;/Code&amp;gt; &amp;lt;/Snippet&amp;gt; &amp;lt;/CodeSnippet&amp;gt; &amp;lt;/CodeSnippets&amp;gt;</summary></entry><entry><title type="html">Reading Technical Books with Kindle DX</title><link href="http://www.stefanoricciardi.com/2011/04/26/reading-technical-books-with-kindle-dx-2/" rel="alternate" type="text/html" title="Reading Technical Books with Kindle DX" /><published>2011-04-26T19:50:00+00:00</published><updated>2011-04-26T19:50:00+00:00</updated><id>http://www.stefanoricciardi.com/2011/04/26/reading-technical-books-with-kindle-dx-2</id><content type="html" xml:base="http://www.stefanoricciardi.com/2011/04/26/reading-technical-books-with-kindle-dx-2/">&lt;h2&gt;&lt;a href=&quot;http://stefanoricciardi.com/blog/wp-content/uploads/2011/04/KindleDx1.jpg&quot;&gt;&lt;img style=&quot;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&quot; title=&quot;KindleDx&quot; border=&quot;0&quot; alt=&quot;KindleDx&quot; align=&quot;right&quot; src=&quot;/assets/KindleDx_thumb.jpg&quot; width=&quot;180&quot; height=&quot;180&quot; /&gt;&lt;/a&gt;Take-Away&lt;/h2&gt;
&lt;p&gt;If you are in a hurry, here's the take-away:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You can carry lots of technical books with you, without breaking your shoulder. &lt;/li&gt;
&lt;li&gt;The e-ink is easy on your eyes, pretty much like real paper. &lt;/li&gt;
&lt;li&gt;Most books in native (&lt;em&gt;mobi&lt;/em&gt;) format show code listings nicely and are fully searchable. &lt;/li&gt;
&lt;li&gt;Getting a new book is just one click away. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Kindle DX does not reflow PDF content, therefore you cannot increase the font size of all those technical papers. You can only zoom in and then use navigation keys to move around. &lt;/li&gt;
&lt;li&gt;No Wi-Fi means you need a cable to upload a book or paper you just downloaded from the net outside Amazon Store (or pay a fee to get it delivered via e-mail). &lt;/li&gt;
&lt;li&gt;Only black and white (a few charts might benefit from being rendered in their original color). &lt;/li&gt;
&lt;li&gt;Getting a new book is just one link away. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Overall&lt;/strong&gt;: it's a pleasure to read from it, you can stop printing all those 4-6 pages white papers and fit them in your Kindle. PDF rendering is excellent and images are crisp. Too bad that the prices for many e-books are pretty much the same as their paper equivalent.&lt;/p&gt;
&lt;h2&gt;A Few Pictures Are Worth A Thousand Words&lt;/h2&gt;
&lt;p&gt;If you are still with me, then let’s see a few examples of how some native (mobi) and PDF books and technical papers are rendered. A word of caution: the quality of the photos themselves is barely tolerable and I apologize for it. On some photos you can see a distinct barrel distortion due to the point and shoot camera I used. You should be able to get an idea anyway.&lt;/p&gt;
&lt;p&gt;The first book I am showing is &lt;a href=&quot;http://www.amazon.co.uk/gp/product/0321344758/ref=as_li_tf_tl?ie=UTF8&amp;amp;tag=stefaricci-21&amp;amp;linkCode=as2&amp;amp;camp=1634&amp;amp;creative=6738&amp;amp;creativeASIN=0321344758&quot;&gt;Don’t Make Me Think!&lt;/a&gt;. This is a typical example of technical book with mixed graphic and text. As you can see image is pretty readable.&lt;/p&gt;
&lt;div style=&quot;padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px&quot; id=&quot;scid:8747F07C-CDE8-481f-B0DF-C6CFD074BF67:f1996f6f-c113-4ef7-995f-e11404521f1a&quot; class=&quot;wlWriterEditableSmartContent&quot;&gt;&lt;a href=&quot;http://stefanoricciardi.com/blog/wp-content/uploads/2011/04/IMG_1775-8x6.jpg&quot; title=&quot;Don't Make Me Think&quot; rel=&quot;thumbnail&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;/assets/IMG_1775.png&quot; width=&quot;266&quot; height=&quot;364&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;The second image is from a free PDF version of the famous book &lt;a href=&quot;http://www.amazon.co.uk/gp/product/0262510871/ref=as_li_tf_tl?ie=UTF8&amp;amp;tag=stefaricci-http://www.cs.uiowa.edu/~slonnegr/plf/Book/&quot;&gt;Syntax and Semantics of Programming Languages&lt;/a&gt;. This is one of the best PDF experience you can get, since the font size and margins are pretty about perfect for the dimensions of the DX.&lt;/p&gt;
&lt;div style=&quot;padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px&quot; id=&quot;scid:8747F07C-CDE8-481f-B0DF-C6CFD074BF67:f9c27fb2-1187-45f5-99a7-01cf38e3cf3c&quot; class=&quot;wlWriterEditableSmartContent&quot;&gt;&lt;a href=&quot;http://stefanoricciardi.com/blog/wp-content/uploads/2011/04/IMG_1769-8x61.jpg&quot; title=&quot;Syntax and Semantics of Programming Languages&quot; rel=&quot;thumbnail&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;/assets/IMG_17691.png&quot; width=&quot;266&quot; height=&quot;382&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Compare instead to the smaller font used in &lt;a href=&quot;http://www.amazon.co.uk/gp/product/1935182471/ref=as_li_tf_tl?ie=UTF8&amp;amp;tag=stefaricci-21&amp;amp;linkCode=as2&amp;amp;camp=1634&amp;amp;creative=6738&amp;amp;creativeASIN=1935182471&quot;&gt;C# in Depth&lt;/a&gt; (great book by the way): this are just a bit too small for my taste, even though I managed to read almost half the book this way.&lt;/p&gt;
&lt;div style=&quot;padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px&quot; id=&quot;scid:8747F07C-CDE8-481f-B0DF-C6CFD074BF67:4fabb4c1-1038-4e28-bff8-6159564ed07a&quot; class=&quot;wlWriterEditableSmartContent&quot;&gt;&lt;a href=&quot;http://stefanoricciardi.com/blog/wp-content/uploads/2011/04/IMG_1777-8x6.jpg&quot; title=&quot;C# in Depth (PDF)&quot; rel=&quot;thumbnail&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;/assets/IMG_1777.png&quot; width=&quot;266&quot; height=&quot;364&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Eventually the editor finally released a mobi version, where I could set a bigger font size, as shown in the following picture. Note that I am showing exactly the same portion as above, so that you can get a feeling of the difference between PDF and mobi: PDF is usually better looking, while mobi is usually easier to read. In the mobi version you can also see that the two code snippets are rendered differently: the Listing 9.9 is “normal” text with a different font than regular text, while the Listing 9.10 is actually a&lt;em&gt; picture. &lt;/em&gt;Probably that was the only way to insert those arrows to highlight specific code sections, but unfortunately the listing itself is much less readable (and zooming into the picture doesn’t seem to help).&amp;#160; &lt;/p&gt;
&lt;div style=&quot;padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px&quot; id=&quot;scid:8747F07C-CDE8-481f-B0DF-C6CFD074BF67:e106af41-767a-4787-8aa0-cc88b3e44f01&quot; class=&quot;wlWriterEditableSmartContent&quot;&gt;&lt;a href=&quot;http://stefanoricciardi.com/blog/wp-content/uploads/2011/04/IMG_1779-8x6.jpg&quot; title=&quot;C# in Depth (mobi)&quot; rel=&quot;thumbnail&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;/assets/IMG_1779.png&quot; width=&quot;266&quot; height=&quot;364&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h2&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;Overall, I can say I am pretty much satisfied with the Kindle DX. I cannot say how it compares to a tablet (be it the iPad2 or any of Android tablets that are popping out recently), since I don’t have one, so this is not yet another “Kindle vs iPad” post (I am sure your favorite search engine can help you there).&lt;/p&gt;
&lt;p&gt;I wanted a device optimized for &lt;em&gt;reading&lt;/em&gt; (with as few distractions as possible) and Kindle DX delivered as expected. The battery lasts weeks and weeks if you turn 3G off when you don’t need it, so you can be assured that even on a long travel you don’t risk to run out of batteries. I only have a few wishes I hope Amazon will consider for next versions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A complete tree-like folder structure. Right now you can only have one level of folders to structure all of your digital content. I am bit compulsive when it comes to organizing my files, so I would like to have the freedom to create any reasonable folder structure as I see fit. &lt;/li&gt;
&lt;li&gt;Wi-fi (as found for example in the Kindle 3) would simplify downloading content from your PC without resorting to the micro-USB cable. This would be especially useful for items downloaded from the net (technical papers, Prag Prog magazine issues, etc.). &lt;/li&gt;
&lt;li&gt;Touch UI. Although I don’t use the internal dictionary too often for technical documents, I surely have to when reading some fiction (Gulliver’s Travels had a lot of weird words! :)) Being able to simply touch a word instead of moving the cursor to it through the page would certainly save seconds. &lt;/li&gt;
&lt;/ul&gt;</content><author><name>Stefano Ricciardi</name></author><category term="Reviews" /><category term="book" /><category term="kindle" /><category term="review" /><summary type="html">Take-Away If you are in a hurry, here's the take-away: Pros: You can carry lots of technical books with you, without breaking your shoulder. The e-ink is easy on your eyes, pretty much like real paper. Most books in native (mobi) format show code listings nicely and are fully searchable. Getting a new book is just one click away. Cons: Kindle DX does not reflow PDF content, therefore you cannot increase the font size of all those technical papers. You can only zoom in and then use navigation keys to move around. No Wi-Fi means you need a cable to upload a book or paper you just downloaded from the net outside Amazon Store (or pay a fee to get it delivered via e-mail). Only black and white (a few charts might benefit from being rendered in their original color). Getting a new book is just one link away. Overall: it's a pleasure to read from it, you can stop printing all those 4-6 pages white papers and fit them in your Kindle. PDF rendering is excellent and images are crisp. Too bad that the prices for many e-books are pretty much the same as their paper equivalent. A Few Pictures Are Worth A Thousand Words If you are still with me, then let’s see a few examples of how some native (mobi) and PDF books and technical papers are rendered. A word of caution: the quality of the photos themselves is barely tolerable and I apologize for it. On some photos you can see a distinct barrel distortion due to the point and shoot camera I used. You should be able to get an idea anyway. The first book I am showing is Don’t Make Me Think!. This is a typical example of technical book with mixed graphic and text. As you can see image is pretty readable. The second image is from a free PDF version of the famous book Syntax and Semantics of Programming Languages. This is one of the best PDF experience you can get, since the font size and margins are pretty about perfect for the dimensions of the DX. Compare instead to the smaller font used in C# in Depth (great book by the way): this are just a bit too small for my taste, even though I managed to read almost half the book this way. Eventually the editor finally released a mobi version, where I could set a bigger font size, as shown in the following picture. Note that I am showing exactly the same portion as above, so that you can get a feeling of the difference between PDF and mobi: PDF is usually better looking, while mobi is usually easier to read. In the mobi version you can also see that the two code snippets are rendered differently: the Listing 9.9 is “normal” text with a different font than regular text, while the Listing 9.10 is actually a picture. Probably that was the only way to insert those arrows to highlight specific code sections, but unfortunately the listing itself is much less readable (and zooming into the picture doesn’t seem to help).&amp;#160; Conclusions Overall, I can say I am pretty much satisfied with the Kindle DX. I cannot say how it compares to a tablet (be it the iPad2 or any of Android tablets that are popping out recently), since I don’t have one, so this is not yet another “Kindle vs iPad” post (I am sure your favorite search engine can help you there). I wanted a device optimized for reading (with as few distractions as possible) and Kindle DX delivered as expected. The battery lasts weeks and weeks if you turn 3G off when you don’t need it, so you can be assured that even on a long travel you don’t risk to run out of batteries. I only have a few wishes I hope Amazon will consider for next versions: A complete tree-like folder structure. Right now you can only have one level of folders to structure all of your digital content. I am bit compulsive when it comes to organizing my files, so I would like to have the freedom to create any reasonable folder structure as I see fit. Wi-fi (as found for example in the Kindle 3) would simplify downloading content from your PC without resorting to the micro-USB cable. This would be especially useful for items downloaded from the net (technical papers, Prag Prog magazine issues, etc.). Touch UI. Although I don’t use the internal dictionary too often for technical documents, I surely have to when reading some fiction (Gulliver’s Travels had a lot of weird words! :)) Being able to simply touch a word instead of moving the cursor to it through the page would certainly save seconds.</summary></entry><entry><title type="html">A New Journey</title><link href="http://www.stefanoricciardi.com/2011/03/08/a-new-journey/" rel="alternate" type="text/html" title="A New Journey" /><published>2011-03-08T21:40:00+00:00</published><updated>2011-03-08T21:40:00+00:00</updated><id>http://www.stefanoricciardi.com/2011/03/08/a-new-journey</id><content type="html" xml:base="http://www.stefanoricciardi.com/2011/03/08/a-new-journey/">&lt;p&gt;&lt;a href=&quot;http://www.flickr.com/photos/chiaralily/5064193260/&quot; title=&quot;Travellers by chiaralily, on Flickr&quot;&gt;&lt;img align=&quot;textTop&quot; alt=&quot;A new journey&quot; height=&quot;328&quot; hspace=&quot;5&quot; src=&quot;/assets/5064193260_e45e51d8d8.jpg&quot; vspace=&quot;5&quot; width=&quot;500&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It was exactly 2 years ago.&lt;/p&gt;
&lt;p&gt;I was embarking on the biggest change in my career as a software developer. Coming from about 10 years of development for Motorola, in an embedded environment, almost exclusively based on a closed proprietary OS, almost exclusively in a procedural language (C), I was moving to a modern VM-based environment (.NET),&amp;nbsp; using an object oriented programming language (C#) always on the move (C# 3.0 had just been released), in a small sized company.&amp;nbsp;I have learned a lot in these past two years in &lt;a href=&quot;http://www.ors.it&quot;&gt;ORS&lt;/a&gt;, having the pleasure to work with a bunch of very talented developers, where I could practice &lt;a href=&quot;http://apprenticeship-patterns.labs.oreilly.com/ch04.html#be_the_worst&quot;&gt;being the worst&lt;/a&gt; on several occasions.&lt;/p&gt;
&lt;p&gt;Now, after two years, it's time for a new journey: I have recently joined&amp;nbsp;&lt;a href=&quot;http://www.deltatre.com&quot;&gt;Deltatre&lt;/a&gt;,&amp;nbsp;a fast growing company in the sport IT business that is behind many big sport web sites (think &lt;a href=&quot;http://www.uefa.com&quot;&gt;uefa.com&lt;/a&gt;&amp;nbsp;or&amp;nbsp;&lt;a href=&quot;http://www.fifa.com&quot;&gt;fifa.com&lt;/a&gt;). I will be part of the core infrastructure team, reporting to the chief software architect, helping shaping the engine that delivers match events to a group of diverse client applications. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Not only is this a very exciting professional opportunity, but luckily enough the office is located at a walking distance from my home (I have literally reduced my commuting time by more than one order of magnitude!). This will leave me more time for my family, and I especially look forward to spending more time with my two kids.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Photo by &lt;a href=&quot;http://www.flickr.com/photos/chiaralily/&quot;&gt;chiaralily&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;</content><author><name>Stefano Ricciardi</name></author><category term="Misc" /><summary type="html">It was exactly 2 years ago. I was embarking on the biggest change in my career as a software developer. Coming from about 10 years of development for Motorola, in an embedded environment, almost exclusively based on a closed proprietary OS, almost exclusively in a procedural language (C), I was moving to a modern VM-based environment (.NET),&amp;nbsp; using an object oriented programming language (C#) always on the move (C# 3.0 had just been released), in a small sized company.&amp;nbsp;I have learned a lot in these past two years in ORS, having the pleasure to work with a bunch of very talented developers, where I could practice being the worst on several occasions. Now, after two years, it's time for a new journey: I have recently joined&amp;nbsp;Deltatre,&amp;nbsp;a fast growing company in the sport IT business that is behind many big sport web sites (think uefa.com&amp;nbsp;or&amp;nbsp;fifa.com). I will be part of the core infrastructure team, reporting to the chief software architect, helping shaping the engine that delivers match events to a group of diverse client applications. &amp;nbsp; Not only is this a very exciting professional opportunity, but luckily enough the office is located at a walking distance from my home (I have literally reduced my commuting time by more than one order of magnitude!). This will leave me more time for my family, and I especially look forward to spending more time with my two kids. Photo by chiaralily.</summary></entry><entry><title type="html">Ninject Mini Tutorial - Part 2</title><link href="http://www.stefanoricciardi.com/2011/02/04/ninject-mini-tutorial-part-2/" rel="alternate" type="text/html" title="Ninject Mini Tutorial - Part 2" /><published>2011-02-04T14:17:43+00:00</published><updated>2011-02-04T14:17:43+00:00</updated><id>http://www.stefanoricciardi.com/2011/02/04/ninject-mini-tutorial-part-2</id><content type="html" xml:base="http://www.stefanoricciardi.com/2011/02/04/ninject-mini-tutorial-part-2/">&lt;p&gt;&lt;em&gt;Go to &lt;a href=&quot;/2011/01/21/ninject-mini-tutorial-part-1/&quot;&gt;Part 1&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;Controlling the Life Cycle of your Objects&lt;/h2&gt;
&lt;p&gt;In the previous post we did not concern ourselves with the lifecycle of 
    the object returned from Ninject kernel. Ninject provides the following 4 built-in 
    lifecycles (&lt;em&gt;scopes&lt;/em&gt;):&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Transient (default)&lt;/li&gt;
&lt;li&gt;Singleton (only one instance)&lt;/li&gt;
&lt;li&gt;Thread (one instance per thread)&lt;/li&gt;
&lt;li&gt;Request (one instance per web request).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You can create &lt;em&gt;custom scopes&lt;/em&gt; if needed.&lt;/p&gt;
&lt;h4&gt;Singleton:&lt;/h4&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IKernel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StandardKernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;To&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;InSingletonScope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WithConstructorArgument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tc1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tc2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;Assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Same&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tc1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tc2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4&gt;Transient:&lt;/h4&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IKernel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StandardKernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;To&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;InTransientScope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WithConstructorArgument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tc1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tc2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;Assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;NotSame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tc1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tc2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2&gt;More Details on Injection Patterns&lt;/h2&gt;
&lt;p&gt;With Ninject you can inject:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Constructor parameters&lt;/li&gt;
&lt;li&gt;Properties&lt;/li&gt;
&lt;li&gt;Methods&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Before considering each one in turn, we just need to introduce the &lt;code&gt;[Inject]&lt;/code&gt; attribute which may be 
used to tag constructors, properties and methods requiring injection. 
Obviously, by tagging constructors, properties or methods, your objects cease 
to be &lt;a href=&quot;http://en.wikipedia.org/wiki/Plain_Old_CLR_Object&quot;&gt;POCO&lt;/a&gt;s.&lt;/p&gt;
&lt;h3&gt;Constructor Injection&lt;/h3&gt;
&lt;p&gt;We have already seen an example of constructor injection in &lt;a href=&quot;/2011/01/21/ninject-mini-tutorial-part-1/&quot;&gt;Part I&lt;/a&gt; 
when the kernel auto-magically injected an implementation of &lt;code&gt;ITaxCalculator&lt;/code&gt; to the &lt;code&gt;Sale Area&lt;/code&gt; constructor. 
In that case, even if we didn't tag the constructor with the &lt;code&gt;[Inject]&lt;/code&gt; attribute, the kernel was able to 
perform the required binding. How?&lt;/p&gt;
&lt;p&gt;That was actually a special case: when there is &lt;em&gt;only one&lt;/em&gt; constructor available, 
the tagging is not needed. On the other hand, if there's more than one constructor defined, 
then then kernel can inject a dependency to only one constructor that needs to have the &lt;code&gt;[Inject]&lt;/code&gt; attribute:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Sale3&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sale3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sale3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// other stuff&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3&gt;Properties Injection&lt;/h3&gt;
&lt;p&gt;Instead of passing in the dependencies through the constructor, you can also inject them as properties. 
    Injecting properties is pretty straightforward:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Sale2&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TaxCalculator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
    
    &lt;span class=&quot;c1&quot;&gt;// implicit default constructor and other stuff... &lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetTotal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
        &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
        &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineItems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
            &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CalculateTax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotalPrice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
                    &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotalPrice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
        
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Usage (note that we never explicitely set the &lt;code&gt;TaxCalculator&lt;/code&gt;):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IKernel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StandardKernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
                  &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;To&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TaxcCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
                  &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WithConstructorArgument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineItem1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SaleLineItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Gone with the wind&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineItem2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SaleLineItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Casablanca&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sale2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// property injection!&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AddItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lineItem1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AddItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lineItem2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;Assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Equal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetTotal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There's an important caveat: if you have 2 or more properties injected, the order in which each dependency is injected is not predictable. 
    This might complicate your design, if those dependencies are coupled somehow (e.g. dependency A needs dependency B). 
    For this kind of situations, constructor or method injection is usually preferred.&lt;/p&gt;
&lt;h3&gt;Methods Injection&lt;/h3&gt;
&lt;p&gt;Finally, it's also possible to tag methods for injection. As with constructor parameters, its possible to inject more than one value at once.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Sale4&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// other stuff&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// method injection, will be called by the kernel&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SetTaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetTotal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineItems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CalculateTax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotalPrice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
                  &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TotalPrice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Usage (note that we never explicitely call the &lt;code&gt;SetTaxCalculator&lt;/code&gt;):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IKernel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StandardKernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;To&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WithConstructorArgument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineItem1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SaleLineItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Gone with the wind&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineItem2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SaleLineItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Casablanca&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sale4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// method injection!&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AddItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lineItem1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AddItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lineItem2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;Assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Equal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetTotal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;em&gt;Go to &lt;a href=&quot;/2011/01/21/ninject-mini-tutorial-part-1/&quot;&gt;Part 1&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;</content><author><name>Stefano Ricciardi</name></author><category term=".NET" /><category term="C#" /><category term="Design and Patterns" /><category term="C#" /><category term="Design and Patterns" /><category term="IoC" /><category term="ninject" /><category term="OOP" /><summary type="html">Go to Part 1 Controlling the Life Cycle of your Objects In the previous post we did not concern ourselves with the lifecycle of the object returned from Ninject kernel. Ninject provides the following 4 built-in lifecycles (scopes): Transient (default) Singleton (only one instance) Thread (one instance per thread) Request (one instance per web request). You can create custom scopes if needed. Singleton: using (IKernel kernel = new StandardKernel()) { kernel.Bind&amp;lt;ITaxCalculator&amp;gt;() .To&amp;lt;TaxCalculator&amp;gt;() .InSingletonScope() .WithConstructorArgument(&quot;rate&quot;, .2M); var tc1 = kernel.Get&amp;lt;ITaxCalculator&amp;gt;(); var tc2 = kernel.Get&amp;lt;ITaxCalculator&amp;gt;(); Assert.Same(tc1, tc2); } Transient: using (IKernel kernel = new StandardKernel()) { kernel.Bind&amp;lt;ITaxCalculator&amp;gt;() .To&amp;lt;TaxCalculator&amp;gt;() .InTransientScope() .WithConstructorArgument(&quot;rate&quot;, .2M); var tc1 = kernel.Get&amp;lt;ITaxCalculator&amp;gt;(); var tc2 = kernel.Get&amp;lt;ITaxCalculator&amp;gt;(); Assert.NotSame(tc1, tc2); } More Details on Injection Patterns With Ninject you can inject: Constructor parameters Properties Methods Before considering each one in turn, we just need to introduce the [Inject] attribute which may be used to tag constructors, properties and methods requiring injection. Obviously, by tagging constructors, properties or methods, your objects cease to be POCOs. Constructor Injection We have already seen an example of constructor injection in Part I when the kernel auto-magically injected an implementation of ITaxCalculator to the Sale Area constructor. In that case, even if we didn't tag the constructor with the [Inject] attribute, the kernel was able to perform the required binding. How? That was actually a special case: when there is only one constructor available, the tagging is not needed. On the other hand, if there's more than one constructor defined, then then kernel can inject a dependency to only one constructor that needs to have the [Inject] attribute: public class Sale3 { private readonly ITaxCalculator taxCalculator; public Sale3() { } [Inject] public Sale3(ITaxCalculator taxCalculator) { this.taxCalculator = taxCalculator; } // other stuff } Properties Injection Instead of passing in the dependencies through the constructor, you can also inject them as properties. Injecting properties is pretty straightforward: public class Sale2 { [Inject] public ITaxCalculator TaxCalculator { get; set; } // implicit default constructor and other stuff... public decimal GetTotal() { decimal total = 0M; foreach (var item in lineItems) { total += TaxCalculator.CalculateTax(item.TotalPrice) + item.TotalPrice; } return total; } } Usage (note that we never explicitely set the TaxCalculator): using (IKernel kernel = new StandardKernel()) { kernel.Bind&amp;lt;ITaxCalculator&amp;gt;() .To&amp;lt;TaxcCalculator&amp;gt;() .WithConstructorArgument(&quot;rate&quot;, .2M); var lineItem1 = new SaleLineItem(&quot;Gone with the wind&quot;, 10M, 1); var lineItem2 = new SaleLineItem(&quot;Casablanca&quot;, 5M, 2); var sale = kernel.Get&amp;lt;Sale2&amp;gt;(); // property injection! sale.AddItem(lineItem1); sale.AddItem(lineItem2); Assert.Equal(24M, sale.GetTotal()); } There's an important caveat: if you have 2 or more properties injected, the order in which each dependency is injected is not predictable. This might complicate your design, if those dependencies are coupled somehow (e.g. dependency A needs dependency B). For this kind of situations, constructor or method injection is usually preferred. Methods Injection Finally, it's also possible to tag methods for injection. As with constructor parameters, its possible to inject more than one value at once. public class Sale4 { private ITaxCalculator taxCalculator; // other stuff // method injection, will be called by the kernel [Inject] public void SetTaxCalculator(ITaxCalculator taxCalculator) { this.taxCalculator = taxCalculator; } public decimal GetTotal() { decimal total = 0M; foreach (var item in lineItems) { total += taxCalculator.CalculateTax(item.TotalPrice) + item.TotalPrice; } return total; } } Usage (note that we never explicitely call the SetTaxCalculator): using (IKernel kernel = new StandardKernel()) { kernel.Bind&amp;lt;ITaxCalculator&amp;gt;() .To&amp;lt;TaxCalculator&amp;gt;() .WithConstructorArgument(&quot;rate&quot;, .2M); var lineItem1 = new SaleLineItem(&quot;Gone with the wind&quot;, 10M, 1); var lineItem2 = new SaleLineItem(&quot;Casablanca&quot;, 5M, 2); var sale = kernel.Get&amp;lt;Sale4&amp;gt;(); // method injection! sale.AddItem(lineItem1); sale.AddItem(lineItem2); Assert.Equal(24M, sale.GetTotal()); } Go to Part 1</summary></entry><entry><title type="html">Ninject Mini Tutorial - Part 1</title><link href="http://www.stefanoricciardi.com/2011/01/21/ninject-mini-tutorial-part-1/" rel="alternate" type="text/html" title="Ninject Mini Tutorial - Part 1" /><published>2011-01-21T14:12:23+00:00</published><updated>2011-01-21T14:12:23+00:00</updated><id>http://www.stefanoricciardi.com/2011/01/21/ninject-mini-tutorial-part-1</id><content type="html" xml:base="http://www.stefanoricciardi.com/2011/01/21/ninject-mini-tutorial-part-1/">&lt;h2&gt;What Is Ninject&lt;/h2&gt;
&lt;p&gt;There are several Inversion of Control (IoC) containers for .NET to pick from (such as &lt;a href=&quot;http://stw.castleproject.org/Windsor.MainPage.ashx&quot;&gt;Castle Windsor&lt;/a&gt;, &lt;a href=&quot;http://structuremap.net/structuremap/index.html&quot;&gt;Structure Map&lt;/a&gt; and &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ff663144.aspx&quot;&gt;Microsoft Unity&lt;/a&gt;, just to name just a few).&amp;nbsp; &lt;a href=&quot;http://ninject.org/&quot;&gt;Ninject&lt;/a&gt; is one of the newest entries in the arena, but it&amp;rsquo;s now sufficiently stable at version 2.0.&lt;/p&gt;
&lt;p&gt;Ninject tries to focus on &amp;ldquo;&lt;em&gt;simplicity and ease of use&lt;/em&gt;&amp;rdquo;, removing features that are not deemed necessary (to the point that XML configuration is not offered out-of-the box).&lt;/p&gt;
&lt;p&gt;In this and following posts we&amp;rsquo;ll explore some example of how to use Ninject. I assume that you are somehow familiar with the basic concepts of &lt;em&gt;Inversion of Control&lt;/em&gt; and &lt;em&gt;Dependency Injection&lt;/em&gt;; if that&amp;rsquo;s not the case, you should consider having a look at this &lt;a href=&quot;http://en.wikipedia.org/wiki/Inversion_of_control&quot;&gt;wikipedia entry&lt;/a&gt; (better yet, 
take some time and read Martin Fowler &lt;a href=&quot;http://martinfowler.com/articles/injection.html&quot;&gt;famous post&lt;/a&gt; on the subject).&lt;/p&gt;

&lt;h2&gt;Setup&lt;/h2&gt;
&lt;p&gt;Installing Ninject is quite easy: you simply download the pre-built DLLs it from &lt;a href=&quot;http://ninject.org/download&quot;&gt;here&lt;/a&gt;. Since Ninject is open source, you can also get the sources from &lt;a href=&quot;https://github.com/ninject/ninject&quot;&gt;github&lt;/a&gt; and build it on your own.&lt;/p&gt;
&lt;p&gt;On my Windows machine, I have copied the DLLs on &lt;em&gt;C:\Ninject&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;UPDATE&lt;/b&gt;: &lt;i&gt;nowadays you would obviosly use NuGet.&lt;/i&gt;&lt;/p&gt;

&lt;h2&gt;Getting your Feet Wet With Ninject&lt;/h2&gt;
&lt;p&gt;Once you have Ninject DLLs somewhere on your hard-drive, in order to get started you only need to 
    reference them (typically you only need to reference &lt;tt&gt;NInject.dll&lt;/tt&gt; ). 
    As we said, no XML configuration is required.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see a few basic examples (the complete source code with unit tests I 
    present in this series are available on &lt;a href=&quot;https://github.com/stefanoric/NINJECTTUTORIAL&quot;&gt;github&lt;/a&gt;).
    &lt;/p&gt;


&lt;h3&gt;&quot;Hello, Ninject&quot;&lt;/h3&gt;
&lt;p&gt;Suppose we have a fairly simple service to calculate the taxes for a given amount, 
    defined in an &lt;code&gt;ITaxCalculator&lt;/code&gt; interface:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CalculateTax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gross&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;and a trivial implementation &lt;code&gt;TaxCalculator&lt;/code&gt; as follows:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TaxCalculator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;TaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;              
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_rate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CalculateTax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_rate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now, one or more classes might need to use an &lt;code&gt;ITaxCalculator&lt;/code&gt; implementation to fulfill their responsibility (such as calculating the total price for a shopping cart). We can say that an implementation of &lt;code&gt;ITaxCalculator&lt;/code&gt; is a &lt;em&gt;dependency&lt;/em&gt; to them.&lt;/p&gt;
&lt;p&gt;Like many IoC containers, Ninject uses a central object (which it calls the &lt;em&gt;kernel&lt;/em&gt;) to provide concrete implementations of dependencies at run-time. The &lt;code&gt;Standard Kernel&lt;/code&gt; is the default implementation of such an object. Let's see it in action:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IKernel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StandardKernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;To&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WithConstructorArgument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Equal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CalculateTax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see, through a fluent interface we are instructing the kernel how to &lt;em&gt;bind&lt;/em&gt; 
(resolve) requests for a &lt;code&gt;ITaxCalculator&lt;/code&gt; to a &lt;code&gt;TaxCalculator&lt;/code&gt; class (a concrete implementation), 
passing to its constructor a given tax rate (20% in this case).&lt;/p&gt;
&lt;p&gt;The example continues showing how a client can retrieve an implementation of the service through the kernel 
    (via the &lt;code&gt;Get()&lt;/code&gt; method) and use it.&lt;/p&gt;

&lt;h3&gt;Some Magic&lt;/h3&gt;
&lt;p&gt;You might argue that the little example above is far from impressing. So let's now see Ninject performing something more clever.&lt;/p&gt;
&lt;p&gt;Suppose we have a &lt;code&gt;Sale&lt;/code&gt; class modeling a ongoing transaction on a ecommerce site. 
Such a class in our example depends on a &lt;code&gt;ITaxCalculator&lt;/code&gt;to compute the final price of the shopping cart.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Sale&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;taxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// more stuff....&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetTotal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	     &lt;span class=&quot;c1&quot;&gt;// use the tax calculator to calculate the total&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We might create the sale in the obvious way, based on the preceding example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;To&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WithConstructorArgument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;());&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;More interestingly, it's possible to let Ninject to find out how a &lt;code&gt;Sale&lt;/code&gt; should be built 
based on the binding information it has received:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c#&quot; data-lang=&quot;c#&quot;&gt;&lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;To&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TaxCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;()&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;WithConstructorArgument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Ninject is smart enough to build a Sale class for us taking care of fulfilling the dependencies behind the scenes. 
    This an example of &lt;em&gt;autowiring&lt;/em&gt;, a most convenient feature of many IoC containers.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Go to &lt;a href=&quot;/2011/02/04/ninject-mini-tutorial-part-2//&quot;&gt;Part 2&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;</content><author><name>Stefano Ricciardi</name></author><category term=".NET" /><category term="C#" /><category term="Design and Patterns" /><category term="C#" /><category term="Design and Patterns" /><category term="IoC" /><category term="ninject" /><category term="OOP" /><summary type="html">What Is Ninject There are several Inversion of Control (IoC) containers for .NET to pick from (such as Castle Windsor, Structure Map and Microsoft Unity, just to name just a few).&amp;nbsp; Ninject is one of the newest entries in the arena, but it&amp;rsquo;s now sufficiently stable at version 2.0. Ninject tries to focus on &amp;ldquo;simplicity and ease of use&amp;rdquo;, removing features that are not deemed necessary (to the point that XML configuration is not offered out-of-the box). In this and following posts we&amp;rsquo;ll explore some example of how to use Ninject. I assume that you are somehow familiar with the basic concepts of Inversion of Control and Dependency Injection; if that&amp;rsquo;s not the case, you should consider having a look at this wikipedia entry (better yet, take some time and read Martin Fowler famous post on the subject).</summary></entry><entry><title type="html">Project Euler Problem 18 in F#</title><link href="http://www.stefanoricciardi.com/2011/01/10/project-euler-problem-18-in-f/" rel="alternate" type="text/html" title="Project Euler Problem 18 in F#" /><published>2011-01-10T13:18:39+00:00</published><updated>2011-01-10T13:18:39+00:00</updated><id>http://www.stefanoricciardi.com/2011/01/10/project-euler-problem-18-in-f</id><content type="html" xml:base="http://www.stefanoricciardi.com/2011/01/10/project-euler-problem-18-in-f/">&lt;p&gt;2011 brings us yet another Project Euler problem to tackle: this time is 
	&lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;amp;id=18&quot;&gt;Problem 18&lt;/a&gt; one 
	of the most interesting that I have solved so far:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;By starting at the top of the triangle below and moving to adjacent numbers on the row below, 
	the maximum total from top to bottom is 23.&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;&lt;b&gt;3&lt;/b&gt; &lt;br /&gt;
		&lt;b&gt;7&lt;/b&gt; 4 &lt;br /&gt;
		2 &lt;b&gt;4&lt;/b&gt; 6 &lt;br /&gt;
		8 5 &lt;b&gt;9&lt;/b&gt; 3&lt;/p&gt;
&lt;p&gt;That is, 3 + 7 + 4 + 9 = 23.&lt;/p&gt;
&lt;p&gt;Find the maximum total from top to bottom of the triangle below:&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;75 &lt;br /&gt;
		95 64 &lt;br /&gt;
		17 47 82 &lt;br /&gt;
		18 35 87 10 &lt;br /&gt;
		20 04 82 47 65 &lt;br /&gt;
		19 01 23 75 03 34 &lt;br /&gt;
		88 02 77 73 07 63 67 &lt;br /&gt;
		99 65 04 28 06 16 70 92 &lt;br /&gt;
		41 41 26 56 83 40 80 70 33 &lt;br /&gt;
		41 48 72 33 47 32 37 16 94 29 &lt;br /&gt;
		53 71 44 65 25 43 91 52 97 51 14 &lt;br /&gt;
		70 11 33 28 77 73 17 78 39 68 17 57 &lt;br /&gt;
		91 71 52 38 17 14 91 43 58 50 27 29 48 &lt;br /&gt;
		63 66 04 68 89 53 67 30 73 16 69 87 40 31 &lt;br /&gt;
		04 62 98 27 23 09 70 98 73 93 38 53 60 04 23&lt;/p&gt;
&lt;p&gt;&lt;small&gt;&lt;b&gt;NOTE:&lt;/b&gt; As there are only 16384 routes, it is possible to solve this problem by 
trying every route. However, &lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;amp;id=67&quot;&gt;Problem 67&lt;/a&gt;, 
is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o) &lt;/small&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Analysis&lt;/h2&gt;
&lt;p&gt;This problem is easily solved resorting to &lt;a href=&quot;http://en.wikipedia.org/wiki/Dynamic_programming&quot;&gt;dynamic programming&lt;/a&gt;, 
which we can loosely describe as decomposing the overall problem into easier sub-problems and combining their solutions to find the final answer.&lt;/p&gt;
&lt;p&gt;In our case, if we consider the number associated with each node of the triangle as a cost, the overall problem can be expressed as &lt;em&gt; 
	finding the path with maximum cost from the upper vertex to the bottom row&lt;/em&gt;. 
	A generic sub-problem would be the cost to reach the bottom row from a generic node at position \(i,j\).&lt;/p&gt;
&lt;p&gt;Given the cost $$c_{i,j}$$ of passing through a given node and the cost \(C_{i,j}\) of reaching the bottom row from that particular node, 
	we can define the following truth:&lt;/p&gt;
&lt;p&gt;$$C_{i,j} = c_{i,j} + max(C_{i+1,j}, C_{i+1,j+1})$$&lt;/p&gt;
&lt;p&gt;or, in other words, from each node \(i,j\) the cost of reaching the bottom is equal to the cost of the node itself 
	plus the maximum path between one of the two adjacent nodes from the row below.&lt;/p&gt;
	
&lt;p&gt;Let's confirm why this is the case with the help of the following figure: consider for example the 
	first node from the left in the fourteenth (last-but-one) row (having cost &lt;strong&gt;63&lt;/strong&gt;): 
	from there you can move to the bottom either going to node &lt;strong&gt;04&lt;/strong&gt; or node &lt;strong&gt;62&lt;/strong&gt;. 
	Clearly, if we want to pick the highest cost, we need to pick &lt;strong&gt;62 &lt;/strong&gt;(and we mark this choice with a grey line). 
	The overall cost to reach the bottom from that node is thus 63 + max (04, 62) = &lt;strong&gt;125&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's consider now the second node from that row (&lt;strong&gt;66&lt;/strong&gt;).
 From there, we have to choose between node &lt;strong&gt;62&lt;/strong&gt; and &lt;strong&gt;98&lt;/strong&gt;: the highest cost path from 
 there is thus 66 + max(62, 98) = &lt;strong&gt;164&lt;/strong&gt;. We can continue for all the nodes in the row to find 
 the highest cost path from each node; the solution to such sub-problems is indicated in gray close to each node.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://stefanoricciardi.com/blog/wp-content/uploads/2010/12/ProjectEuler18_2rows.png&quot;&gt;
&lt;img alt=&quot;ProjectEuler18_2rows&quot; border=&quot;0&quot; height=&quot;89&quot; src=&quot;/assets/ProjectEuler18_2rows_thumb.png&quot; 
style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; 
title=&quot;ProjectEuler18_2rows&quot; width=&quot;454&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Having solved all sub-problems for row 14, we can now consider the row immediately above. In the following figure, 
	I have replaced row 14 with the solutions of the sub-problems that we had just solved; 
	the row above is row 13. Please note how we dont care about row 15 anymore: its contribution to the 
	overall problem is already captured in each sub-problems' solution for row 14! Hence, we proceed in 
	a similar way to solve the sub-problems for the row 13 just as we did for row 14:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://stefanoricciardi.com/blog/wp-content/uploads/2010/12/ProjectEuler18_2rows1.png&quot;&gt;
&lt;img alt=&quot;ProjectEuler18_2rows&quot; border=&quot;0&quot; height=&quot;68&quot; src=&quot;/assets/ProjectEuler18_2rows_thumb1.png&quot;
 style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;ProjectEuler18_2rows&quot; width=&quot;529&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We continue to follow a similar pattern moving upwards row after row, until we reach the tip of the triangle, 
	always considering two rows at a time.&lt;/p&gt;

&lt;h2&gt;Solution&lt;/h2&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-f#&quot; data-lang=&quot;f#&quot;&gt;let a = Array2D.zeroCreate&amp;lt;int&amp;gt; 15 15

a.[0,0] &amp;lt;- 75
a.[1,0] &amp;lt;- 95
a.[1,1] &amp;lt;- 64
a.[2,0] &amp;lt;- 17
a.[2,1] &amp;lt;- 47
a.[2,2] &amp;lt;- 82
a.[3,0] &amp;lt;- 18
// other initializations omitted for clarity
// full listing available on Github
// (https://github.com/stefanoric/YAPEFSHARP)
a.[14,10] &amp;lt;- 38
a.[14,11] &amp;lt;- 53
a.[14,12] &amp;lt;- 60
a.[14,13] &amp;lt;- 04
a.[14,14] &amp;lt;- 23 

let costs = Array2D.zeroCreate&amp;lt;int&amp;gt; 15 15

// costs for bottom row is the same as bottom row values
Array2D.blit a 14 0 costs 14 0 1 15 |&amp;gt; ignore

let max a b = if a &amp;gt; b then a else b

for i in 13 .. -1 .. 0 do
    for j in 0 .. i do
        costs.[i,j] &amp;lt;- a.[i,j] + max costs.[i+1,j] costs.[i+1, j+1]
        
let result () =
    costs.[0,0]&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;Most of the code for this solution is needed just to initialize the two dimensional array containing the triangle. For reading convenience, I have skipped most of the uninteresting rows, but you can find the complete solution on &lt;a href=&quot;https://github.com/stefanoric/YAPEFSHARP&quot;&gt;Github&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So, we have a matrix (2D array) for the cost of each nodes and another matrix for the solutions of the sub-problems associated to each node. Trivially, the costs and the solutions coincide for the bottom row (note the handy &lt;code&gt;Array2D.blit&lt;/code&gt; function to copy a whole row from one matrix to the other).&lt;/p&gt;
&lt;p&gt;Then, with a loop we move backward from the last-but-one row up to the tip of the triangle calculating the sub-problems solutions as explained above.&lt;/p&gt;</content><author><name>Stefano Ricciardi</name></author><category term="F#" /><category term="algorithm" /><category term="F#" /><category term="functional" /><summary type="html">2011 brings us yet another Project Euler problem to tackle: this time is Problem 18 one of the most interesting that I have solved so far: By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below: 75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 32 37 16 94 29 53 71 44 65 25 43 91 52 97 51 14 70 11 33 28 77 73 17 78 39 68 17 57 91 71 52 38 17 14 91 43 58 50 27 29 48 63 66 04 68 89 53 67 30 73 16 69 87 40 31 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)</summary></entry></feed>