<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.7.3">Jekyll</generator><link href="http://localhost:4000/feed.xml" rel="self" type="application/atom+xml" /><link href="http://localhost:4000/" rel="alternate" type="text/html" /><updated>2018-04-05T20:26:21+02:00</updated><id>http://localhost:4000/</id><title type="html">tomislav erić</title><subtitle>A place to share experience about newest technologies for software developers. You will find different topics like mobile development, web development,  game development, artificial intelligence, chatbots and many more</subtitle><entry><title type="html">Xamarin.Forms constructor injection with unity</title><link href="http://localhost:4000/recipes/xamarin/dependency-injection/2018/03/24/unity-container.html" rel="alternate" type="text/html" title="Xamarin.Forms constructor injection with unity" /><published>2018-03-24T00:00:00+01:00</published><updated>2018-03-24T00:00:00+01:00</updated><id>http://localhost:4000/recipes/xamarin/dependency-injection/2018/03/24/unity-container</id><content type="html" xml:base="http://localhost:4000/recipes/xamarin/dependency-injection/2018/03/24/unity-container.html">&lt;p&gt;A simple and straight forwared approach to get dependency injection, especially constructor injection, to work in Xamarin.Forms via Unity.&lt;/p&gt;

&lt;h3 id=&quot;project-setup&quot;&gt;Project setup&lt;/h3&gt;
&lt;p&gt;Open Visual Studio for Mac and create a &lt;code class=&quot;highlighter-rouge&quot;&gt;Multiplatform -&amp;gt; Blank Forms App&lt;/code&gt;. Choose any name check &lt;code class=&quot;highlighter-rouge&quot;&gt;Android&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;iOS&lt;/code&gt; for the target platforms, choose for Shared Code the &lt;code class=&quot;highlighter-rouge&quot;&gt;Portable Class Library&lt;/code&gt; and check &lt;code class=&quot;highlighter-rouge&quot;&gt;use XAML for user interface files&lt;/code&gt;. Click next for the next step and now lets jump right into it.&lt;/p&gt;

&lt;h3 id=&quot;install-unity-container&quot;&gt;Install unity container&lt;/h3&gt;
&lt;p&gt;One simple and easy way to use constructor injection in Xamarin.Forms is via unity. Do a right click on packages in your solution folder of the shared project and select &lt;code class=&quot;highlighter-rouge&quot;&gt;Add packages&lt;/code&gt; and type &lt;code class=&quot;highlighter-rouge&quot;&gt;Unity&lt;/code&gt;. NuGet will show you the unity package from author &lt;code class=&quot;highlighter-rouge&quot;&gt;Microsoft.Practices.Unity&lt;/code&gt;, choose this one and select &lt;code class=&quot;highlighter-rouge&quot;&gt;Add Package&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;create-the-service-to-be-injected&quot;&gt;Create the service to be injected&lt;/h3&gt;
&lt;p&gt;You have to create a &lt;code class=&quot;highlighter-rouge&quot;&gt;interface&lt;/code&gt; and the corresponding &lt;code class=&quot;highlighter-rouge&quot;&gt;service&lt;/code&gt;. Let’s add a simple &lt;code class=&quot;highlighter-rouge&quot;&gt;DataService&lt;/code&gt; to demonstrate the injection. First, create the interface &lt;code class=&quot;highlighter-rouge&quot;&gt;IDataService&lt;/code&gt; and add the method &lt;code class=&quot;highlighter-rouge&quot;&gt;GetCities()&lt;/code&gt; to it.&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;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IDataService&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetCities&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 let us create the DataService class with it’s implemented interface.&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;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DataService&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDataService&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;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetCities&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;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;{&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;&quot;Karlsruhe&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;&quot;Split&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;&quot;Dubrovnik&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;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;inject-the-service-via-construction-injection&quot;&gt;Inject the service via construction injection&lt;/h3&gt;
&lt;p&gt;To consume the data fetched by the dataservice we have to inject the service in a viewmodel to demonstrate it. Create the &lt;code class=&quot;highlighter-rouge&quot;&gt;CityListViewModel&lt;/code&gt; with a uninitialized private field of type &lt;code class=&quot;highlighter-rouge&quot;&gt;IDataService&lt;/code&gt;. Now inject the service in the constructor. With this implementaion we will get a &lt;code class=&quot;highlighter-rouge&quot;&gt;NullReferenceException&lt;/code&gt; because the DataService was not instantiated.&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;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CityListViewModel&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;IDataService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataservice&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;CityListViewModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDataService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataService&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;dataservice&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataService&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;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AllCities&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataservice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetCities&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;h3 id=&quot;setup-the-unity-container&quot;&gt;Setup the unity container&lt;/h3&gt;
&lt;p&gt;To prevent throwing NullReferenceException from last step, we have to setup a container to be able to use constructor injection in our shared code files. A good place to do this, is right after Xamarin.Forms initializes it’s components. The initialization takes place at &lt;code class=&quot;highlighter-rouge&quot;&gt;App.xaml.cs&lt;/code&gt; file. We just have to instatiate a &lt;code class=&quot;highlighter-rouge&quot;&gt;new UnityContainer&lt;/code&gt; and register our types, next we have to tell the &lt;code class=&quot;highlighter-rouge&quot;&gt;ServiceLocator&lt;/code&gt; to use our newly created container as the current &lt;code class=&quot;highlighter-rouge&quot;&gt;LocatorProvicer&lt;/code&gt;. Finally we have to call our method from &lt;code class=&quot;highlighter-rouge&quot;&gt;App.xaml.cs&lt;/code&gt;s constructor. And thats it!&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;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;RegisterService&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;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unityContainer&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;UnityContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;unityContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisterType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDataService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// register any service you like with the scheme interface, service&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ServiceLocator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;SetLocatorProvider&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; 
        &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;UnityServiceLocator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unityContainer&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 our &lt;code class=&quot;highlighter-rouge&quot;&gt;CityListViewModel&lt;/code&gt; will be able to get the list of cities without a NullRefereneException. This is how you can achieve a constructor injection in Xamarin.Forms.&lt;/p&gt;</content><author><name></name></author><summary type="html">A simple and straight forwared approach to get dependency injection, especially constructor injection, to work in Xamarin.Forms via Unity.</summary></entry></feed>